The true meaning of mutable & Immutable in Python
mutable → Changeable / modifiable
immutable → Unchangeable / unmodifiable
Python works in a way that only a few programming languages do.
X = 10 Y = X X = 20 print(X) # 20 print(Y) # 10
What happened here, right?
- Let’s go, step by step:
Python first stores the value 10 in a memory location. Its reference is then passed to the variable X.
Variable Y is created and refers to the value whose reference is stored in variable X i.e. 10.
Then, a new value ‘20’ is stored in the memory, and X refers to it.
Now, X is referring to ‘20’ and Y is still referring to ‘10’.
I hope this helps.
Keep working on your ability to recall.