What are the Mutable and Immutable objects in Python?

Everything in Python is an object. These objects are classified into two types. Those are mutable and immutable objects in Python.

Mutable and Immutable Objects in Python

In this Python tutorial, we will learn what are mutable and immutable objects in Python and the differences between them.

Mutable Objects in Python

First, we will learn the definition of mutable objects. The mutable objects are the objects that are able to modify(i.e., delete, update, change etc.,)even after the creation of the object. The following are some of the examples of mutable objects.

You can also read,

Ex:-list,set,dictionary

Below code is how the above examples are mutable objects.

 

List in Python – Mutable object

# Create a list

list = [1,2,3]

# To add a item in list

list.append(4)
print(list)

Output:-[1, 2, 3, 4]

# To delete a item in list

list.pop() # delete the last item in a list
print(list)

Output:- [1,2,3]

del list[2] # to delete a particular element in a set
print(list)

Output:- [1,2]



# To change a item in list

list = ['hello','hi','are','you']
list[1] = 'how' # to modify a particular item in a list
print(list)

Output:-['hello','how','are','you']

 

Set in Python – Mutable object

# Create a set

set = {"red","blue","green"}
print(set)

Output:- {'red', 'green', 'blue'}


# To add an item in the set

set.add("yellow")
print(set)

Output:- {'red', 'green', 'blue', 'yellow'}

# To delete an item in the set

set.remove("red")
print(set)

Output:- {'green', 'blue', 'yellow'}


# To change or modify the set

set.update(["white","black"])
print(set)

Output:- {'green', 'blue', 'yellow','white', 'black'}






 

Dictionary in Python – Mutable Object

# Create a dictionary

dict = {'one': 1, 'two': '2', 'three': 3.0, 'four': 4.0}
print(dict)

Output:- {'one': 1, 'two': '2', 'three': 3.0, 'four': 4.0}


# To add an item in dictionary


dict["five"] = 5
print(dict)

Output:-{'one': 1, 'two': '2', 'three': 3.0, 'four': 4.0, 'five': 5}


# To delete an item in dictionary

dict.pop("three")
print(dict)

Output:- {'one': 1, 'two': '2', 'four': 4.0, 'five': 5}

# To change or update the dictionary

dict1 = {"six": 6}
dict.update(dict1)
print(dict)

Output:-{'one': 1, 'two': '2', 'four': 4.0, 'five': 5, 'six': 6}

From the above operations, we can tell that the above examples are mutable objects.

 

Immutable Objects in Python

Immutable Objects:-These are the objects that cannot be changed after created. In simple words, we cannot add or delete or update these objects even after created.

 

Ex:-Tuple, String etc.,

Tuple in Python – Immutable object

# Create a tuple

tuple=(1,2,3,4)
print(tuple)

Output:-(1,2,3,4)


tuple[0]=0 #To update an item in the tuple
print(tuple)

Output:-'tuple' object does not support item assignment

tuple[4]=5 # To add an item to the tuple
print(tuple)

Output:-'tuple' object does not support item assignment

String in python – Immutable object

# Create a String

str = " Welcome to CodeSpeedy"
print(str)

Output:- Welcome to CodeSpeedy

str[0]="H" # To update a letter in string
print(str)

Output:-'str' object does not support item assignment

From the above operations, the examples which are present above are immutable objects.

One response to “What are the Mutable and Immutable objects in Python?”

  1. Abhilash Bandla says:

    Nice article!

Leave a Reply

Your email address will not be published. Required fields are marked *