Send a link to your students to track their progress
14 Terms
1
New cards
[References] Reference
A variable doesn't hold an object itself; it holds a reference pointing to where the object lives. Two variables can point to the same object. ```python a = [1, 2, 3] b = a print(a is b) # → True print(id(a) == id(b)) # → True ← same object in memory ```
2
New cards
[References] After `b = a` (a is a list), what happens when you call `b.append(4)`?
`a` changes too. `b = a` copies the reference, not the list, so both names point to the same list.
[References] How do you make a real copy of a list?
`a.copy()`, `list(a)`, or `a[:]`. These are shallow copies: a new outer list, but any objects inside are still shared. ```python a = [1, 2, 3] b = a.copy() b.append(4) print(a, b) # → [1, 2, 3] [1, 2, 3, 4]
grid = [[0], [0]] shallow = grid.copy() shallow[0].append(9) print(grid) # → [[0, 9], [0]] ← the inner lists are still shared ```
5
New cards
[References] Mutating vs. rebinding
`b.append(4)` changes the object `b` points to (everyone sharing it sees the change). `b = b + [4]` builds a new list and points `b` at it (`a` is unaffected). ```python a = [1, 2] b = a b = b + [3] # builds a new list; a is untouched print(a, b) # → [1, 2] [1, 2, 3]
c = a c.append(3) # changes the shared list print(a) # → [1, 2, 3] ```
6
New cards
[References] Passing a list into a function
The function gets a reference to the same list, so changes it makes (append, remove, sort) are visible to the caller. ```python def add_zero(nums): nums.append(0)
Python's "no object here" value. In data structures it acts as the null pointer, e.g., the `.next` of the last node in a chain. ```python class Node: def __init__(self, value): self.value = value self.next = None
n = Node(5) print(n.next) # → None print(n.next is None) # → True ```
8
New cards
[References] How should you compare a value to None?
Use `is`: `if current is None:` (or `is not None`). Not `== None`.
9
New cards
[References] Linked-structure traversal pattern
Start at the first node. Look at the current node, do something with it, move to `current.next`, and stop when current is None.
10
New cards
[References] Why is the traversal pattern so important in CET 2200?
It is the most common pattern in the course: the same "follow the reference until None" loop is used for linked lists, trees, and graphs.
11
New cards
[References] Code: Write a Node class whose next starts as None.
```python class Node: def __init__(self, value): self.value = value self.next = None # points to nothing until linked ```
12
New cards
[References] Code: Write code that links three nodes into a chain and prints every value.