Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 2 — Mutable & Immutable Objects

Lesson 26 of 50 in the free Python Programming notes on Siksha Sarovar, written by Rohit Jangra.

Mutable and Immutable Objects

An object's mutability determines whether its contents can be changed after creation.

Mutable (can change in place)Immutable (cannot change — a new object is created)
listint, float, complex
dictstr
settuple
bytearraybool, frozenset, bytes

Immutable example — id() stays same only for the same object

x = 10
print(id(x))
x = x + 5      # creates a NEW int object; x now points elsewhere
print(id(x))   # different id
s = "hello"
print(id(s))
s = s + " world"   # a brand-new string object is created
print(id(s))       # different id — original "hello" is unchanged in memory

Mutable example — id() stays the SAME after in-place change

lst = [1, 2, 3]
print(id(lst))
lst.append(4)        # modifies the SAME list object in place
print(id(lst))       # same id
print(lst)            # [1, 2, 3, 4]

Why it matters — function arguments and aliasing

def modify_list(lst):
    lst.append(100)     # mutates the caller's list too!

a = [1, 2, 3]
modify_list(a)
print(a)   # [1, 2, 3, 100] -- the original list changed

def modify_number(n):
    n += 100             # rebinds the local n; caller's variable unaffected

x = 5
modify_number(x)
print(x)   # 5 -- unchanged, int is immutable

Aliasing pitfall with mutable objects

a = [1, 2, 3]
b = a            # b is an ALIAS, not a copy — both point to the same list
b.append(4)
print(a)         # [1, 2, 3, 4] -- a changed too!

c = a.copy()     # creates an independent copy
c.append(99)
print(a)         # [1, 2, 3, 4] -- unaffected by changes to c

Summary

  • Immutable objects are safe to share; mutable objects require care (copy explicitly to avoid unintended side effects).
  • Tuples are immutable, but a tuple containing a list can still have that inner list mutated.