hamburger

Difference Between Mutable and Immutable in Python

By BYJU'S Exam Prep

Updated on: September 25th, 2023

Difference Between Mutable and Immutable in Python: In Python programming language, whenever the internal state of the is chargeable, that object is known as a mutable object. So whenever the object is gone through an internal state change, it is called mutable, and if not then it is called an immutable object. The main difference between mutable and immutable objects in Python is based on their acceptance of internal change.

The mutable and immutable states in Python are internal. If the changes in the internal state of the object occur with time if they are mutual else immutable. We will see what is mutable and immutable in detail along with it we will discuss the difference between mutable and immutable in various aspects.

Download Formulas for GATE Computer Science Engineering – Programming & Data Structures

Difference Between Mutable and Immutable in Python

Apart from being different based on the susceptibility to the changes, there exists other difference between mutable and immutable objects in Python which are explained in the table provided below:

Key Differences Between Mutable and Immutable in Python

Mutable Immutable
The objects can be modified after the creation as well. Objects can not be modified after the creation of the objects.
Classes that are mutable are not considered final. Classes that are immutable are considered final.
Thread unsafe. Thread-safe.
Classes are not made final for the mutable objects. Classes are made final for the immutable objects.
Example: Lists, Dicts, Sets, User-Defined Classes, Dictionaries, etc. Example: int, float, bool, string, Unicode, tuple, Numbers, etc.

Formulas for GATE Computer Science Engineering – Digital Logic

What is Mutable in Python?

In Python programming language, whenever an object is susceptible to internal change or has the ability to change its values is known as a mutable object. Mutable objects in Python are generally changed after creation. 

For mutable objects in Python, their values can be changed but the identity of the object remains intact. The objects in Python which are mutable are provided below:

  • Lists
  • Dicts
  • Sets
  • User-Defined Classes 
  • Dictionaries

Formulas for GATE Computer Science Engineering – Computer Organization & Architecture

What is Immutable in Python?

Similarly, when the objects in the Python are not susceptible to the internal change are known as the immutable objects. They can not be modified once they are created. For example, int in Python is immutable hence they can not be modified once they are created.

Objects in Python which are immutable are shown below:

  • int,
  • float,
  • bool,
  • string,
  • Unicode,
  • tuple
  • Numbers
  • Frozen Sets
  • User-Defined Classes

Check out some important topics related to the difference between mutable and immutable objects in Python in the table provided below:

Difference Between actual and formal parameters Difference Between Python and C++
Difference Between Algorithm and Pseudocode Difference between Metals and Non-Metals
Difference Between Array and List in Python Difference Between Hard Copy and Soft Copy
Our Apps Playstore
POPULAR EXAMS
SSC and Bank
Other Exams
GradeStack Learning Pvt. Ltd.Windsor IT Park, Tower - A, 2nd Floor, Sector 125, Noida, Uttar Pradesh 201303 help@byjusexamprep.com
Home Practice Test Series Premium