Sep 18: Prac2, Objects and Types
Learning Objectives
After today's class, you should be able to:
- Explain the difference between a variable and an object.
- Name and describe three examples of a container type.
Reminders¶
- Due today: HW4 Reflection
- Read Ch 5 – ideally:
- 1st half before Friday
- 2nd half before Monday
- Submit HW 5 – ideally:
- 1 and 2 before Friday
- 3 and 4 before Monday
- This Friday: What is CS Research?
HW4 Debrief¶
[10 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
- Web traffic monitored
- Log out when finished
Ch05 Preview¶
[15 min]
- Variables and objects are two different concepts
Example | Variable (Name) | Object (Value) |
---|---|---|
x = 5 |
x |
the integer 5 |
y = 1.2 |
y |
the float 1.2 |
z = 'A' |
z |
the string 'A' |
p = True |
p |
the boolean True |
q = None |
q |
the None object |
import math |
math |
the math module |
def hi() |
hi |
the hi function |
- Every object has a value, a type, and an identity
type()
andid()
functionsis
keyword compares object id's
- A container is an object that contains (references) other objects
- list:
[1, 2, 3]
in brackets - tuple:
(1, 2, 3)
in parentheses - set:
{1, 2, 3}
in braces - dict:
{"one": 1, "two": 2, "three": 3}
braces + colons
- list:
- Every variable references exactly one object
- In Python Tutor, a reference is shown as an arrow
- See complex example with many types of objects