In this chapter, Mutable and Immutable Data Types in Python, we will understand the concept of immutability and mutability in Python with simple examples. You will learn what mutable and immutable data types are, how they differ, and the different types of mutable and immutable data types used in Python. Everything is explained in simple, easy-to-understand language with practical examples to make the concepts clear.
Before going further, we should first understand what immutability means. Without this concept, it is hard to grasp why some data types behave differently from others. Once you understand what immutability means at its core, the difference between mutable and immutable types will feel natural and logical, rather than just a rule to memorize.
What Does “Immutable” Mean?
- An immutable object cannot be modified in the same memory location.
- When the value appears to change, Python actually creates a new object and makes the variable refer to that new object.
Variables Store References, Not Values
- In Python, variables do not directly store values.
- Instead, a variable stores a reference (address) of an object. Lets try to understand it with an example.
Example: Let’s take three variables as given below:
var1 = 5
var2 = var1
var3 = 5
Here:
- var1, var2 and var3 all refer to the same integer object 5.
- Only one object containing value 5 exists in memory.
- Multiple variables can refer to the same object.
Checking Memory Addresses
print(id(p))
print(id(q))
print(id(r))
Output (addresses may vary):
1457662208
1457662208
1457662208
All three variables have the same address because they refer to the same object.
Changing an Immutable Value
var1 = 10
var3 = 7
var2 = var3
Now:
- var1 refers to object 10.
- var2 refers to object 7.
- var3 also refers to object 7.
Checking Memory Addresses
print(id(p))
print(id(q))
print(id(r))
Output (addresses may vary):
1457662288
1457662240
1457662240
- Python does not modify the old object 5. Instead, it makes the variables point to different objects.
- var1 has a different address because it now refers to 10.
- var2 and var3 have the same address because both refer to 7.
The Original Object Remains Unchanged
Even after changing var1 from 5 to 10, the object 5 still exists.
print(id(5))
The memory address of object 5 remains the same. Only the variable reference changes.
What are Mutable and Immutable Types?
Python data objects are divided into two categories:
- Immutable types
- Mutable types
Immutable Data Types
- Immutable means not changeable.
- These are data types whose values cannot be changed after creation.
- If we try to change the value, Python creates a new object in memory instead of modifying the old one.
- The following data types are immutable:
- Integers (int)
- Floating Point Numbers (float)
- Booleans (bool)
- Strings (str)
- Tuples (tuple)
Example (conceptual):
num1 = 300
num2 = num1
num1 = num2 + 100
num1 = 300 → Python creates an object with value 300
num2 = num1 → both refer to the same object (300)
num1 = num2 + 100 → Python creates a new object (400) and assigns it to num1
So, num1 now points to a new memory location.
IMAGE OF MUTABLE AND IMMUTABLE
Mutable Data Types
- Mutable means changeable.
- These are data types whose values can be changed after creation.
- The memory location remains the same, but the content inside it can be updated.
- List, Dictionary, and set are example of mutable data type
Example (conceptual):
list1 = [10, 20, 30]
list1[0] = 100 # value is changed
So, lists (and some other types) are mutable.
What Does “Mutable” Mean?
A mutable object allows its contents to be modified without creating a new object.
The memory address of the object remains the same even after changes.
Example: Modifying a List
Chk = [2, 4, 6]
Chk[1] = 40
print(Chk)
Output:
[2, 40, 6]
Checking Memory Address Using id()
Chk = [2, 4, 6]
print(id(Chk))
Chk[1] = 40
print(id(Chk))
Output (addresses may vary):
150195536
150195536
Notice that the memory address remains the same before and after the modification. This shows that the list was modified in place.
Mutability vs Immutability
| Mutable Types | Immutable Types |
| Value can be modified in the same memory location. | Value cannot be modified in the same memory location. |
| Memory address usually remains the same after modification. | A new object is created when the value changes. |
| Examples: List, Dictionary, Set | Examples: Integer, Float, Boolean, String, Tuple |


