Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

What Is Mutable And Immutable In Python?

Updated on 14 February, 2024

905.04K+ views
8 min read

In Python, every variable holds an instance of any object in two kinds, i.e. mutable and immutable. A unique object id is assigned to an object whenever it is instantiated. The runtime defines the object type and cannot be changed later. 

However, the changeable one is a mutable object. When we say mutable, it means the internal object state can be mutated. Simultaneously, the object state incapable of changing after creation is an immutable object. Both mutable and immutable states are integral to the data structure of Python.

The feature of object mutability in Python makes it a dynamically typed language. Mutable and Immutable in Python is quite essential as a concept but are often confused due to their intransitive nature of immutability. 

Let’s find out all about them and the key differences.

What in Python are mutable and immutable?

What is mutable and immutable in Python? This is a common query, especially among beginners. An object that can be changed after creation is referred to as mutable in Python. Immutable, on the other hand, in Python refers to an object that cannot be changed after it is created. Let’s examine each category in greater detail to gain a better understanding of this idea.

Variables in Python can store several sorts of data, and these data types can be divided into mutable and immutable categories. Python programming is most effective when the idea of mutability is understood. In this review, we’ll look at immutable and mutable meaning in Python, how they differ, and some examples of both.

Check out our data science online courses to upskill yourself

Mutable State In Python

The word ‘Mutable’ directly translates to ‘changeable’ or something that can be ‘mutated’. It defines an object open to changes, and in Python, ‘mutable’ relates to an object’s ability to change values. These objects often store data collection and contain built-in type lists, sets, dictionaries and user-defined classes.  

Python’s mutable data types

In Python, mutable data types permit changes after initialization. Lists, sets, and dictionaries are just a few examples of changeable data structures that are frequently used. These data types support alterations including element addition, removal, and change.

Let’s take a Python list as an example:

numbers = [1, 2, 3, 4]

The list ‘numbers’ in this case can be altered by including, deleting, or replacing entries. By directly accessing an existing element’s index, we can edit it or insert a new element using the append() method.

A dictionary is another illustration:

student = {‘name’: ‘John’, ‘age’: 20}

In this situation, we can change the values connected to the keys or expand the dictionary by adding new key-value pairs.

Python’s immutable data types:

Immutable data types in Python are those that cannot be modified after creation, as the name implies. Strings, tuples, and numbers (including integers, floats, and booleans) are examples of immutable data types.

Let’s think about a string:

message = “Hello, World!”

The string ‘message’ cannot be changed directly in this situation. We can generate a new string with the needed adjustment, but we cannot change a character at a certain index.

Tuples are also immutable.

coordinates = (10, 20)

The tuple’s values are fixed once they are generated. A new tuple must be made if the values need to be changed.

Immutable Definition State In Python

Immutable in Python is when you cannot change the object type over time. If it is not possible to alter the value of an object in Python, it is known as an immutable object. Once an immutable object is created, its value remains permanent and unchangeable. Immutable built-in type objects are numbers, strings, tuples, frozen sets, and user-defined classes.

Objects in Python

Before we delve deep into mutability and immutability in Python, let us first learn what objects are. In Python, everything is considered an object, and each object has three attributes:

  • Identity: This refers to the object’s address in the computer’s memory.
  • Type: This refers to the object type that is made. For instance, integer, string, list and the like. 
  • Value: This refers to the value that the object stores. For example, List=[5,6,7] would store the numbers 5, 6 and 7.

Even if the ID and Type of an object are not open to change after creation, the values are open to alterations for Mutable objects.

Mutable Objects in Python

Mutable objects are the objects in Python that can be mutated or changed. Codes serve better in teaching about the implementation of these objects. Therefore, let us look at a few codes and try to understand mutable objects better:

  • For making a list containing the names of fruits
fruits= [‘Mango’, ‘Papaya’, ‘Orange’]
  • For printing elements from the list of fruits, divided by a comma & space

for fruit in fruits:

print(fruit, end=’, ’)

Output [1]: Mango, Papaya, Orange

  • For printing the object location that is created in the memory address in a hexadecimal format
print(hex(id(fruits)))

Output [2]: 0x1691d7de8c8

  • Adding a new fruit to the list ‘fruits’

fruits.append(‘Grapes’)

  • For printing the elements present in the list ‘fruits’, separated by a comma & space

for fruit in fruits:

print(fruit, end=’, ’)

Output [3]: Mango, Papaya, Orange, Grapes

  • For printing the object location created in the memory address in a hexadecimal format
print(hex(id(fruits)))

Output [4]: 0x1691d7de8c8

The example shows that the object’s internal state ‘fruits’ can easily change by adding one more fruit, ‘Grapes’ to it. However, the object’s memory address remains the same, proving that a new object does not need to be created. Rather, the same object could be changed or mutated. This example shows that the object with the reference variable name ‘fruits’, a list type, is mutable.

Immutable Objects in Python

Immutable objects in Python are the objects whose values cannot be changed. Let us follow a simple code to grasp better what immutable objects are and how they work. The code is given below:-

  • For creating a Tuple containing the English name of months

months= ‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’

  • For printing the elements of tuple months
print(months)

Output [1]:  (‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’)

  • For printing the object location, which is created in the memory address in a hexadecimal format
print(hex(id(months)))

Output [2]: 0x1691cc35090

  • It is important to note that tuples are immutable; therefore, they fail to include more new elements. Hence, you need to use the merge of tuples with the # + operator to add a new imaginary month in the tuple ‘months’.

months +=  ‘Pythonuary’

  • For printing the elements of tuple ‘months’.
print(months)

Output [3]: (‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘Pythonuary’)

  • For printing the object location created in the memory address in a hexadecimal format
print(hex(id(months)))

Output [4]: 0x1691cc8ad68

This example proves variable names can be easily used for referencing an object, a kind of tuple consisting of eight elements. However, the memory location ID of the old & new tuple is not the same. Hence, the object’s internal state ‘months’ could not be changed. Therefore, the Python program manager created a new object in the memory address. Additionally, the variable name ‘months’ also referenced the new object with nine elements. Hence, proving that the tuple, the object with the reference variable name ‘months’, is an immutable object.

What separates mutable from immutable?

The way mutable and immutable data types handle modifications is their main distinction. Mutable data types alter an existing item in place, whereas immutable data types produce a new object when changed. This divergence has effects on how variables behave in Python programmes and how memory is managed.

Since they avoid unintentional changes, immutable objects are regarded as safer. Particularly when handling concurrent operations or sending data between functions, they provide data integrity. On the other side, mutable objects offer adaptability and permit effective adjustments.

Writing effective and error-free code in Python requires an understanding of the concept of mutability. Lists, sets, and dictionaries are examples of mutable data types that can be changed after creation, whereas strings, tuples, and numbers are examples of immutable data types that cannot be altered. Understanding the distinctions between mutable and immutable data types enables developers to choose data types carefully and steer clear of any problems brought on by unintentional changes. Keep in mind that the performance and dependability of your Python programmes can be significantly impacted by selecting the correct data type based on its mutability.

Check out all Trending Python Tutorial Concepts.

Conclusion

Mutable objects are primarily used for allowing any future updates. On the other hand, immutability also offers many effective and practical applications for various sensitive tasks in a network-centred place, enabling parallel processing. Immutable objects, seal the values and ensure that none of the threads invokes overwrite/update to the data. It is great for writing permanent codes that won’t need changes in future.

If you are a budding programmer or a data science enthusiast, learning about mutable and immutable objects in Python will prove to help you choose this field as your career. The best way to learn about these is by signing up for the Executive Post Graduate Programme in Data Science on upGrad.

The course is created under expert faculty guidance, delivering in-depth knowledge of trending topics to prepare thousands of learners for a future-proof career.

Frequently Asked Questions (FAQs)

1. What are the significant differences between mutable vs immutable in Python?

An object’s state or value is open to modification after creation in mutable An object’s state or value is not open to modification after creation in immutable Mutable objects aren’t thread-safe. Immutable objects are completely thread-safe Mutable classes aren’t final. Before creating an immutable object, you must make the class final.

2. In Python, what are mutable and immutable data types?

Mutable data types in Python are list, set, dictionary and user-defined classes and immutable data types are int, decimal, float, bool, tuple, string, and range.

3. Are lists mutable in Python?

In Python, lists are the mutable data types with easily modified elements. Additionally, you can replace the individual elements and even change the order of elements after creating the list.