Lecture 1.1
Guoliang Ma
The Chow Institute, 2025
If a program is a building, variables are the blocks, and syntax tells us how to put the blocks together to form walls. Different programs are different ways to put the walls together.
Programming puts mathematical ideas into practice. You have already encountered abstraction in mathematics.
In primary school, we counted 1, 2, 3, …. In middle school, we used x to represent a generic number.
So x = 1, x = 2, or x = 100 does not surprise us: a single letter x can be versatile.
In Python, x is a name. We want a variable to represent a value.
Python stores that value in an object in memory, then binds the name to the object. An existing object can also be reused.
Quick verification · names and object identity
# quick verification
a = 1
print(id(a))
b = a
print(id(b))
# not appearing as an address? format it
print(f"{id(b):02X}")
In the previous chunk of code, we saw:
print — displays the supplied content.id — returns an object's identity. In CPython, this is its memory address.f"..." and {...}."Alice".name."Bob".name.
What is special about an object?
Can you summarize some features of an object?
Hint: We have learned that everything is an object. What do they have in common?
An object comprises …
import sys
sys.getsizeof(a)
Types are a large topic: Python's built-in types documentation is long. Let's start with a few examples.
a = 1
b = 2.0
c = 3.14 - 5j
cond = True
Use the type function to find the type of each object.
The Boolean type is special in Python. Let's investigate.
We need a new function, isinstance, to determine whether an object is an instance of a type.
a = 1
isinstance(a, int)
isinstance(a, type(a))
cond = True
isinstance(cond, int)
print(cond + 1)
True in Python is also 1, what about False?isinstance. Do you find anything interesting?isinstance and read it.Lists and tuples are frequently encountered. Here, we create them by enumerating their elements:
l = [1, 2, 3]
t = (1, 2, 3)
append vs. extendpop+ operatorYou know how to get an element from a list or a tuple. Now let's try to set an element's value.
l and t that you just created.l to -1.t to -1.Similarities and comparisons · each column starts afresh. “OK” means a valid operation, not a True result.
l = [1, 2, 3]
t = (4, 5, 6)
print(l[1]) # OK
print(t[2]) # OK
l[1] = "list" # OK
t[2] = "tuple" # TypeError
1 in l # OK
2 in t # OK; False
# Both are sequences.
l = [1, 2, 3]
t = (4, 5, 6)
l[0] = 1 # OK
t[0] = 1 # TypeError
del l[1] # OK
del t[1] # TypeError
l = [1, 2, 3]
t = (4, 5, 6)
l.append(4) # OK
l.reverse() # OK
l.extend((5,)) # OK
l.pop(0) # OK
# AttributeError:
t.append()
t.reverse()
t.extend()
t.pop()
l = [1, 2, 3]
t = (4, 5, 6)
l += [6, 7]
# Same id as before.
t += (8,)
# id changes.
Try the statements individually in your notebook: an intentional error stops a cell.
We can take one element from a list — or many elements, using a slice.
The notation is start:stop[:step].
slice(start, stop, step).l[s]. Colon notation such as l[1:10:2] also creates a slice.s = slice(1, 10, 2)
l[s]
slice a function?l a function?Are these slices?
1:2:12:4:79:1:-1a:b:c1.5:2.3:3.14a:2:36:7:-5:-1::-1These are also sequence types. We will not cover them in detail, but …
array before using it.deque.There are too many functions and methods to cover in class. You have learned the “how to.”