Mutable & Immutable in Python
According to the Cambridge Dictionary, mutable means able or likely to change, while immutable means not changing, or unable to be changed.
Mutable data types are object which can be changed after being created while immutable object are those which can not be changed after being created.
Mutable data types are list, set, dictionary, bytearray, and array. Immutable data types are integers, floating-point numbers, boolean, string, tuples, frozen set anf bytes etc.
In Python, every data type is an object, such as strings and integers, etc.
Imagine you are creating a game or banking app or website application. You need to store a username. In order to store a username, you require memory space.
username = "Maria"
print(username) # Maria
username = "chaiaurcode"
print(username) # chaiaurcodeIn the above code, there is a variable named username which is a string. Initially, username contains “Maria”. Later, “chaiaurcode” is assigned. Now, username has “chaiaurcode”. As discussed earlier, immutable values cannot be changed, and strings are immutable. Then, how does the username contain “chaiaurcode” rather than “Maria”?
For this, we have to understand the internal working. There are three things to keep in mind: variables, Assignment or variable assignment (the value assigned to the variable), i.e., maria or chaiaurcode, and memory space or RAM, where the variable and assigned value are stored.
In Python, assignment is stored in memory as an object. variable is referenced to that object.
When a new assignment is made to a variable, Python may create a new object, reuse an existing object, or use an object that already exists. Here, a new assignment object is created (an object for “chaiaurcode” is created). Now, that variable is simply referenced to the new assignment.
If an object has no reference, then the garbage collector in Python automatically removes it from memory.
In this example, num1 is assigned 10 while num2 is assigned whatever value is stored in num1. Later, num1 is assigned the value 15.
num1 = 10
num2 = num1 # assigning value stored in num1
print(num1) # 10
print(num2) # 10
num1 = 15
print(num1) # 15
print(num2) # 10An object with the value 10 is created in memory space. Variables num1 and num2 are references to the value 10.
Get Maria Ghulam Rasool’s stories in your inbox
Join Medium for free to get updates from this writer.
Later, an object with assignment 15 is created, and num1 is a reference to this object. As value 10 is referenced, it is not deleted.
If it was not referenced by anything, then it would be deleted by the garbage collector automatically.
Key takeaways
- Every Python variable name refers to an object.
- Objects have types, and their types determine whether their contents can be modified.
- Mutable objects can be modified in place.
- Immutable objects cannot be modified after creation.
- Assignment binds a name to an object; it does not necessarily copy the object.
- Multiple variable names can refer to the same object.
- Reassigning one name does not automatically reassign other names.
- Objects that are no longer reachable can be reclaimed by Python’s memory-management system.





