Lesson 06
Mutable & Immutable — Practice Programs
Practice id(), item assignment, aliasing, copying, and object identity through real Python programs.
What you'll master
| Program | Skill |
|---|---|
| Program 1 | Prove list mutability |
| Program 2 | Prove string immutability |
| Program 3 | Understand alias bugs |
| Program 4 | Create safe independent copies |
| Program 5 | Classify Python types |
You have covered the theory in lessons 1–5. This lesson turns Aliasing and Copying into runnable programs.
Learning roadmap
Before running any code, predict what happens to the memory address (id()).
Program 1: Prove a list is mutable
Lists allow in-place modification, so the memory address stays the same.
lst = [10, 20, 30]
before = id(lst)
lst[0] = 99
after = id(lst)
print(lst)
print(before == after)Output:
[99, 20, 30]
TrueMemory trick:
| Change | Object | id() |
|---|---|---|
| Content changes | Same object | Same id() |
Program 2: Prove a string is immutable
Strings do not allow item assignment. Any modification creates a new object.
s = "python"
try:
s[0] = "P"
except TypeError as e:
print(e)
s = s.upper()
print(s)Output:
'str' object does not support item assignment
PYTHONWhy?
s[0] = "P"— tries to edit the existing object (fails withTypeError)s = s.upper()— creates a new string and rebindss
Program 3: The alias bug
Two variables can point to the same list. Changing one changes both.
original = [1, 2, 3]
backup = original
backup.append(4)
print(original)
print(original is backup)Output:
[1, 2, 3, 4]
TrueRule:
backup = originaldoes not copy the list. It only creates another reference.
Program 4: Safe copy
Create a new container using .copy() so mutations stay isolated.
original = [1, 2, 3]
clone = original.copy()
clone.append(4)
print(original)
print(clone)
print(original is clone)Output:
[1, 2, 3]
[1, 2, 3, 4]
FalseRemember the difference
| Statement | Meaning |
|---|---|
b = a |
Same object (alias) |
b = a.copy() |
New object (copy) |
Program 5: Classify Python types
This program helps you instantly recognize whether a built-in type is mutable or immutable.
samples = [
10,
"hi",
(1, 2),
[1, 2],
{1, 2},
{"a": 1},
frozenset({1}),
None,
]
for obj in samples:
kind = (
"mutable"
if type(obj) in (list, set, dict, bytearray)
else "immutable"
)
print(type(obj).__name__, "→", kind)Expected output:
int → immutable
str → immutable
tuple → immutable
list → mutable
set → mutable
dict → mutable
frozenset → immutable
NoneType → immutableComplete revision mind map
Use this one diagram before interviews or coding rounds.
Review the full module: Mutable vs Immutable Overview.
Interview cheat sheet
Mutable — same object · same id() · supports item assignment
list · set · dict · bytearray
Immutable — new object · new id() · no item assignment
str · tuple · int · frozenset
Three rules to remember forever
b = a→ alias (same object)b = a.copy()→ independent copyobj[i] = valueworks only on mutable types
Quick knowledge check
Q1: After lst.append(1), does id(lst) usually change?
Answer: No — lists mutate in place, so the object identity stays the same.
Q2: What error does t[0] = 1 raise?
Answer: TypeError — tuples are immutable.
Q3: How do you avoid alias bugs when passing a list to a function?
Answer: Pass a copy: func(data.copy()).
Module complete
You can now predict whether any Python built-in type will mutate in place, create a new object, or accidentally share state through aliasing.
Next module: Collections Deep Dive — list, tuple, set, and dict patterns (coming soon).
Loading playground…