The true meaning of mutable & Immutable in Python

·

1 min read

  • 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:
  1. Python first stores the value 10 in a memory location. Its reference is then passed to the variable X.

  2. Variable Y is created and refers to the value whose reference is stored in variable X i.e. 10.

  3. Then, a new value ‘20’ is stored in the memory, and X refers to it.

  4. Now, X is referring to ‘20’ and Y is still referring to ‘10’.


I hope this helps.

Keep working on your ability to recall.