Same variable, different story depending on the type โ traced through id(), and the tricky case of a mutable list living inside an immutable tuple.
This topic covers what mutability means in Python, which data types are mutable vs immutable, and how that distinction plays out in practice โ including tricky edge cases involving nested data structures (a list inside a tuple, mixing + and in-place operations, etc.).
Mutability refers to the ability to change or edit data in its memory location.
Mutability depends on the data type.
0x1000 | 4
0x1001 |
...
0x100n |
If a type is mutable, its value at a given memory address (e.g., 0x1000) can be edited directly. If it's immutable, that memory location's contents can never change โ any "update" requires creating a brand-new object elsewhere in memory.
| Immutable Data Types | Mutable Data Types |
|---|---|
| String | List |
| int | Dictionary |
| Float | Set |
| Bool | |
| Complex | |
| Tuple |
Example: String
>>> a = "Hello"
>>> id(a)
1872263894720
>>> a = a + "world"
>>> a
'Helloworld'
>>> id(a)
1872264002992 # DIFFERENT id โ a brand-new string object was created
Example: Tuple
>>> T = (1, 2, 3)
>>> id(T)
1872263426336
>>> T = T + (5, 6)
>>> T
(1, 2, 3, 5, 6)
>>> id(T)
1872263312136 # DIFFERENT id โ a brand-new tuple object was created
Diagram (tuple concatenation creates a new object):
a = (1,2,3) โโโบ [0x1000 | (1,2,3) ]
a = a + (4,5)
a โโโโโโโโโโโโโบ [0x1010 | (1,2,3,4,5) ]
The old tuple at 0x1000 is left behind; a now points somewhere new entirely.
โ Rule: For immutable types, any "modification" operation (+,+=, concatenation) always builds a new object and rebinds the variable name to it. The address changes.
Example: List with .append()
>>> L = [1, 2, 3]
>>> id(L)
1872263604680
>>> L.append(4)
>>> L
[1, 2, 3, 4]
>>> id(L)
1872263604680 # SAME id โ modified IN PLACE
Example: List Item Assignment
>>> L = [1, 2, 3]
>>> L[2] = 1
>>> L
[1, 2, 1]
The list object itself is mutated in place; its own id() doesn't change even though the value at index 2 changed.
L.append(x) โ mutates the same object; id(L) unchanged.L = L + [x] โ behaves like the immutable case; creates a new list object, and id(L) changes after reassignment.So even though List is a mutable type, using + (rather than an in-place method like .append()) still produces a new object โ mutability determines what's possible, not that every operation is automatically in-place.
A tuple is immutable โ you cannot reassign what object one of its slots points to. But if a slot holds a mutable object (like a list), that inner object can still be changed in place.
>>> a = [1, 2]
>>> b = [3, 4]
>>> c = (a, b)
>>> c
([1, 2], [3, 4])
>>> c[0][0] = 100 # mutating the LIST *inside* the tuple โ this works!
>>> c
([100, 2], [3, 4])
>>> id(c) # the tuple's own id is UNCHANGED โ its structure never changed
Trying to reassign a tuple slot directly, however, fails:
>>> c[0] = [100, 2]
Traceback (most recent call last):
...
TypeError: 'tuple' object does not support item assignment
โ Key insight: A tuple's immutability means you can't change which objects it references (its slots are fixed). It says nothing about whether those referenced objects are themselves mutable. If a tuple contains a list, that list can still be freely modified โ the tuple's own identity/structure is untouched, but its content (as seen when you print it) can appear to change.
.append(), index assignment (L[i] = x), etc. edit the object in place โ same memory address before and after.+ on a list still creates a new object (unlike .append()), even though List is mutable โ the type being mutable just means in-place operations are possible, not that all operations are in-place.tuple[i] = x) always raises TypeError: 'tuple' object does not support item assignment.id().id() stays the same.L.append(x) โ in-place (same id). L = L + [x] โ new object (id changes) โ even for a mutable type like List.id() won't change, but its printed content will reflect the inner mutation.TypeError.