Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 06

Mutable & Immutable — Practice Programs

Practice id(), item assignment, aliasing, copying, and object identity through real Python programs.

Beginner15 min

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()).

Predict → Run → Check id() → Classify mutable or immutable

Program 1: Prove a list is mutable

Lists allow in-place modification, so the memory address stays the same.

Before [10,20,30] After [99,20,30] — id(lst) remains the same
python
lst = [10, 20, 30]

before = id(lst)

lst[0] = 99

after = id(lst)

print(lst)
print(before == after)

Output:

text
[99, 20, 30]
True

Memory 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.

python id A → PYTHON id B — s=s.upper() creates NEW object
python
s = "python"

try:
    s[0] = "P"
except TypeError as e:
    print(e)

s = s.upper()
print(s)

Output:

text
'str' object does not support item assignment
PYTHON

Why?

  • s[0] = "P" — tries to edit the existing object (fails with TypeError)
  • s = s.upper() — creates a new string and rebinds s

Program 3: The alias bug

Two variables can point to the same list. Changing one changes both.

original and backup alias same [1,2,3] — append affects BOTH
python
original = [1, 2, 3]

backup = original

backup.append(4)

print(original)
print(original is backup)

Output:

text
[1, 2, 3, 4]
True

Rule: backup = original does not copy the list. It only creates another reference.

Program 4: Safe copy

Create a new container using .copy() so mutations stay isolated.

original and clone separate objects — Different ids Independent objects
python
original = [1, 2, 3]

clone = original.copy()

clone.append(4)

print(original)
print(clone)
print(original is clone)

Output:

text
[1, 2, 3]
[1, 2, 3, 4]
False

Remember 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.

python
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:

text
int → immutable
str → immutable
tuple → immutable
list → mutable
set → mutable
dict → mutable
frozenset → immutable
NoneType → immutable

Complete revision mind map

Use this one diagram before interviews or coding rounds.

Python Objects — Mutable vs Immutable full classification mind map

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

  1. b = a → alias (same object)
  2. b = a.copy() → independent copy
  3. obj[i] = value works 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…