Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · Python · 04 Jul 2026 · Hindi + English

Mutable and Immutable in Python: Complete List with Examples

Lists, dicts and sets are mutable; int, float, str, tuple are immutable. See what this means in memory, with id() proof and the classic function-argument trap.

Definition

Mutable objects can be changed after creation (in the same memory). Immutable objects can never change — any "modification" actually creates a brand-new object.
Mutable (changeable)Immutable (locked)
list, dict, set, bytearrayint, float, bool, str, tuple, frozenset, bytes

Proof with id()

# MUTABLE: list changes in the SAME memory
nums = [1, 2, 3]
print(id(nums))       # e.g. 140023...
nums.append(4)
print(id(nums))       # SAME id - object modified in place

# IMMUTABLE: string "change" creates a NEW object
name = "Aman"
print(id(name))       # e.g. 140055...
name = name + " Kumar"
print(id(name))       # DIFFERENT id - new object created
140023011 140023011 <- same 140055123 140055987 <- different

Why strings seem changeable but aren't

s = "hello"
s[0] = "H"
# TypeError: 'str' object does not support item assignment

s = "Hello"    # this is NOT modification - it's REBINDING
               # the name s to a completely new string object
Assignment like s = "Hello" never modifies the old object — it just points the name to a new one. This distinction between modifying an object and rebinding a name is the heart of this whole topic.

The classic trap: mutable function argument

def add_student(name, students=[]):     # DANGER: mutable default!
    students.append(name)
    return students

print(add_student("Aman"))
print(add_student("Priya"))     # expected ['Priya'] ...but!
['Aman'] ['Aman', 'Priya'] <- Priya joined Aman's list!

The default list is created once at function definition and shared across all calls. Correct pattern:

def add_student(name, students=None):
    if students is None:
        students = []
    students.append(name)
    return students

Why immutability matters (interview points)

  • Dictionary keys: only immutable (hashable) objects can be dict keys or set members — that's why tuples work but lists don't.
  • Safety: immutable objects can be shared between functions/threads with zero risk of accidental modification.
  • Aliasing bug: b = a for a mutable object means both names share one object — change through b appears in a. With immutable objects this bug is impossible.

Definition

Mutable objects बनने के बाद बदले जा सकते हैं (same memory में). Immutable objects कभी नहीं बदलते — कोई भी "modification" असल में बिल्कुल नया object बनाता है.
Mutable (बदल सकते हैं)Immutable (locked)
list, dict, set, bytearrayint, float, bool, str, tuple, frozenset, bytes

id() से proof

# MUTABLE: list SAME memory में बदलती है
nums = [1, 2, 3]
print(id(nums))       # जैसे 140023...
nums.append(4)
print(id(nums))       # SAME id - object in place modify हुआ

# IMMUTABLE: string "change" NAYA object बनाता है
name = "Aman"
print(id(name))       # जैसे 140055...
name = name + " Kumar"
print(id(name))       # DIFFERENT id - नया object बना
140023011 140023011 <- same 140055123 140055987 <- अलग

Strings बदलती दिखती हैं पर बदलतीं नहीं

s = "hello"
s[0] = "H"
# TypeError: 'str' object does not support item assignment

s = "Hello"    # यह modification NAHI hai - यह REBINDING है
               # name s अब बिल्कुल नए string object को point करता है
s = "Hello" जैसा assignment पुराने object को कभी modify नहीं करता — सिर्फ name को नए object पर point करा देता है. Object modify करने और name rebind करने का यही फर्क इस पूरे topic का दिल है.

Classic trap: mutable function argument

def add_student(name, students=[]):     # खतरा: mutable default!
    students.append(name)
    return students

print(add_student("Aman"))
print(add_student("Priya"))     # expected ['Priya'] ...लेकिन!
['Aman'] ['Aman', 'Priya'] <- Priya Aman की list में आ गई!

Default list function definition पर एक बार बनती है और सभी calls में share होती है. सही pattern:

def add_student(name, students=None):
    if students is None:
        students = []
    students.append(name)
    return students

Immutability क्यों matter करती है (interview points)

  • Dictionary keys: सिर्फ immutable (hashable) objects dict keys या set members बन सकते हैं — इसीलिए tuples चलते हैं, lists नहीं.
  • Safety: immutable objects functions/threads के बीच बिना accidental modification के risk के share हो सकते हैं.
  • Aliasing bug: mutable object के लिए b = a का मतलब दोनों names एक object share करते हैं — b से change a में दिखता है. Immutable objects के साथ यह bug impossible है.

Frequently Asked Questions

Which data types are mutable and immutable in Python?

Mutable: list, dict, set, bytearray. Immutable: int, float, bool, str, tuple, frozenset, bytes.

Is a string mutable in Python?

No. Strings are immutable — s[0] = "H" raises TypeError, and operations like s + "x" create an entirely new string object.

Why is a mutable default argument dangerous?

Because the default object is created once at function definition and shared across all calls, so data leaks between calls. Use None as default and create the list inside.