Python Programming

Mutable and Immutable in Python

Mutable and Immutable in Python

Introduction

In Python, every variable stores an instance of an object. in python there are two types of objects:

  • Mutable
  • Immutable

When we instantiate an object in Python, we assign, that object is assigned with some unique ID. At the runtime, that object is defined and can’t be changed after. But if it is a mutable object its state can be changed.

Immutable Objects

These objects are in-built types for example int, bool, float, string, and tuple. simple, these objects can not be changed once created.

Code

tup = (0, 1, 2, 3)
tup[0] = 4
print(tup)

Output

Traceback (most recent call last):
  File "e0eaddff843a8695575daec34506f126.py", line 3, in
    tup[0]=4
TypeError: 'tuple' object does not support item assignment

In the above code, we tried to change the tuple, and because of it, it gave a Type Error. A tuple is an immutable object, once created can not be altered.

write your code here: Coding Playground

Mutable Objects

Mutable Objects: These are the objects that can be changed after creation. Custom classes generally are mutable. Some mutable types are list, set, dictionary etc.

Code

alpha = ['A', 'B', 'C']
print(alpha)
alpha[0] = 'D'
alpha[-1] = 'E'
print(alpha)

Output

['A', 'B', 'C']
['D', 'B', 'E']

write your code here: Coding Playground

Conclusion

  • In Python, objects are handled differently when it comes to their type. When we talk about immutable objects, they are quick to access but with great cost as they can not be changed because it involves the creation of a copy. On the other hand, mutable objects can be changed easily.
  • Whenever a change is required in data, mutable objects should be used.
  • Tuple itself is not mutable but its content or elements are mutable.
  • Generally, Primitive types are mostly immutable and Customized types are probably mutable.

Code

tup = ([1,2,3,4], "Hello")
print("Before Updating: ", tup)
l = tup[0]
l[0] = 5
print("After Updating: ", tup)

Output

Before Updating:  ([1, 2, 3, 4], 'Hello')
After Updating:  ([5, 2, 3, 4], 'Hello')

In the code, we can see that the first element of the tuple is a list that is mutable and is changed. However, the tuple itself is not mutable and cannot be changed.

write your code here: Coding Playground

Some examples explaining the exception of tuple

Code

tup = (1,2,3,4,[1,2,3,4],5,6,7,8)
print("Before Update: ", tup)
a = tup[4]
a[2] = 5
print("After Update: ", tup)

Output

Before Update:  (1, 2, 3, 4, [1, 2, 3, 4], 5, 6, 7, 8)
After Update:  (1, 2, 3, 4, [1, 2, 5, 4], 5, 6, 7, 8)

Explanation

In the code above, we created a tuple named tup in which there is an element that is a list and we know that list is a mutable object, so we changed the list and as we can see the output. The tuple itself is immutable and can not be updated but if there is a mutable object in the tuple then in some way the tuple can be changed but that does not make it mutable.

write your code here: Coding Playground