Ch. 4: References and None

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/13

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 4:23 AM on 10/18/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

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.
3
New cards
[References] Predict: `x = [1, 2]; y = x; x.append(3); print(y)`
`[1, 2, 3]`. `x` and `y` refer to the same list.
4
New cards
[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)

data = [1, 2]
add_zero(data)
print(data) # → [1, 2, 0]
```
7
New cards
[References] None
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.
```python
n1 = Node(10)
n2 = Node(20)
n3 = Node(30)
n1.next = n2
n2.next = n3

current = n1
while current is not None:
print(current.value)
current = current.next # follow the reference
```
13
New cards
[References] Code: Write chain_length(head) that counts the nodes in a chain.
```python
def chain_length(head):
count = 0
current = head
while current is not None:
count += 1
current = current.next
return count
```
14
New cards
[References] SettingWithCopyWarning (pandas)
pandas can't tell whether your variable is a new copy or another reference to the original data. It's the same reference idea as `b =