Skip to content

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

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:
    1. Thonny
    2. 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() and id() functions
    • is 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
  • Every variable references exactly one object