A Beginner's Guide to Mutable vs Immutable Data Types in Python

If we go by the literal meaning:
Immutable means something that cannot be changed.
Mutable means something that can be changed.
In Python, everything is an object, and variables do not store values directly.
Instead, variables store references (addresses) to objects in memory.
Important note:
First, an object is created in memory → then a variable references it.
Immutable Data Types
Common immutable data types in Python include:
Integers
Floats
Strings
Tuples
Booleans
Let’s understand this using integers.
Example 1: Single reference change
x = 5
x = 10
What happens internally:
Initially, x points to the object 5 in memory.
When we write x = 10, Python does not change 5.
Instead, a new object 10 is created, and x now points to 10.
➡️ The integer 5 still exists in memory (if referenced elsewhere).
If not referenced elsewhere, it will automatically get deleted by the python garbage collector after some time.
So:
Object is unchanged
Reference is changed
This is what immutability means.
Example 2: Multiple references to the same object
x = 5
y = x
Now:
Both x and y point to the same object 5.
x = 15
After this:
x now points to a new object 15
y still points to 5
➡️ The original object 5 remains unchanged.
This clearly shows:
Changing an immutable object actually means changing the reference
The original object is never modified
Strings are also immutable
name = "rohit"
You cannot change "rohit" into "mohit" by modifying a character.
❌ Not allowed:
name[0] = "m"
✔️ What Python allows:
name = "mohit"
This creates a new string object, and name now points to it.
➡️ You can change where the variable points,
➡️ But you cannot change the string object itself.
That’s immutability.
Mutable Data Types
Common mutable data types include:
List
Set
Dictionary
Bytearray
Let’s take a list example.
my_list = [24, 56, 78, 90]
Now, if we do:
my_list[1] = 6
The list becomes:
[24, 6, 78, 90]
Here:
The same list object is modified
The reference does not change
The object itself is updated in memory
➡️ This is what we mean by mutable.
📌Final Difference (In One Line)
Immutable objects → object cannot change, only the reference can
Mutable objects → object itself can be modified in memory
📌Key Takeaway (Very Important)
In Python:
Variables do not store values
Variables store references to objects
Immutability and mutability depend on whether the object can change, not the variable
You can refer to the below 👇 video from chai and code which I referred to understand this concept
https://youtu.be/MDZ4y-GgZ8k?si=iZnqrbEwIsruIiLO





