Sep 18: Prac2, Objects and Types
Learning Objectives
After today's class, you should be able to:
- Create a list and access a specific element using an index.
- Describe operations that can be applied to a set of strings.
- Create a dictionary of strings and look up values by key.
Reminders¶
- Read Week 5 Reading and Activities– ideally:
- 1st half before Friday
- 2nd half before Monday
- Submit HW 5 – ideally:
- 1 and 2 before Sunday
- 3 and 4 before Tuesday
HW4 Debrief¶
[5 min]
Some highlights:
- Functions can eliminate any duplicate code
- Docstrings are painful to write but really help
- The
__main__
block can be used for testing - Complex math can be broken down into steps
- Division/modulo can be used to extract digits
Practice Quiz 2¶
[25 min]
Instructions:
- Log in as
student
- Only two windows:
- Thonny
- Web browser
- Don't use any existing files during the quiz
- Web traffic monitored
- Log out when finished
Containers¶
POGIL Activity¶
- Containers
- If you are absent today, complete this activity at home
- Bring your completed activity to class or office hours
Example Code¶
Model 1¶
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Run each line one at a time; don't paste the entire block of code!
odd = [1, 3, 5, 7]
odd
odd[2]
odd[4]
len(odd)
number = odd[1]
number
odd[1] = 2
odd
number
Model 2¶
va_schools = {"JMU", "GMU", "VCU", "VT", "ODU", "WM", "UVA"}
two_letter = {"MU", "VT", "ND", "WM", "BC"}
Run each line one at a time; don't paste the entire block of code!
type(va_schools)
len(two_letter)
"ND" in va_schools
"JMU" not in two_letter
len(va_schools.union(two_letter))
va_schools.intersection(two_letter)
two_letter.difference(va_schools)
va_schools.remove("GMU")
va_schools.remove("ODU")
va_schools.remove("VCU")
print(va_schools)
va_schools.add("JMU")
va_schools.add("JMU")
va_schools.add("JMU")
Model 3¶
elements = {'C': 'carbon', 'H': 'hydrogen', 'O': 'oxygen', 'N': 'nitrogen'}
Run each line one at a time; don't paste the entire block of code!
type(elements)
elements.keys()
elements.values()
elements['C']
atom = 'N'
elements[atom]
elements[N]
elements['nitrogen']
elements[1]
len(elements)
elements['B'] = 'Boron'
elements.items()