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

Mutable vs. Immutable Objects in Python: Learn The Real Difference

Updated on 25 November, 2022

6.99K+ views
7 min read

The hype around functional programming and immutable objects are often confused. But, if you want a better resume with Python knowledge, the objects are all you need to concentrate on. The importance of immutable objects is a heavily debated topic among developers, and yet it remains inconclusive.

Python has become quite popular among developers. According to a JetBrains survey, 93% of software testing professionals prefer Python as a primary language. There is no doubt that Python has been a forefront programming language, not just in machine learning or artificial intelligence, but also in application development lately. 

The most confusing element of Python is the object. Whether to choose a mutable or immutable object in Python remains a point of discussion. So, let’s understand the difference. If you want to learn more about data science, check out our data science programs

Read: Career Opportunities in Python

Mutable and Immutable in Python

Any object in Python whose internal state is changeable can be called a mutable. Similarly, the objects with a static state or ones that can’t be changed are immutable. The difference may seem simple, but the effect of both these objects is different in programming. 

To understand the type of objects in Python, we need to understand the structure of language. Let’s consider an example, 

age = 62

print(id(age)) # id

print(type(age)) # type

print(age) # value

[Out:]

25356154

<class ‘int’>

62

The example states that Python has some key elements in the structure. These elements are:

  • An ID(Identity)
  • A type
  • A value

Whether the value changes or not determines the type of object in python- “mutable or immutable.” It is also essential for programmers to learn that the identity and the class never change. The ID is a unique identifier that helps recognize the object in Python. 

Let’s take another example to understand:

age = 62

print(id(age))

print(type(age))

print(age)

age = 63

print(age)

print(id(age))

[Out:]

25356154

<class ‘int’>

62

63

25356166

Here, in the first instance, you may wonder whether the change in age indicates a mutable object. But it is just an integer. The ‘int’ class is immutable and co-related to the name ‘age.’ So, when we change the age from 62 to 63, it points towards the ‘int’ class. 

When you type 63, another object is created differently from 62. It is an object that points to ‘age’ in a different location with another unique ID. 

Now that you know the primary difference, the first thing that will come to your mind is: which type of objects are mutable and immutable in Python?

Also Read: Python Developer Salary in India

Mutable Types

We already know by now that mutable values can change after creation. Types of mutable values in Python are:

  • Lists
  • Byte arrays
  • Sets
  • Dictionaries

Lists

List data in Python are mutable. Let’s take an example,

my_list = [1,2,3]
print(my_list)
print(id(my_list))
>>> [1,2,3]
>>> 1863010210504
my_list.append(4)
print(my_list)
print(id(my_list))
>>> [1,2,3,4]
>>> 1863010210504

Here, you can observe the value of the list changes from [1,2,3] to [1,2,3,4]. The append() method used here adds a single item ‘4’. It means that the process does not return with an entirely new list and instead changes the original one. You can observe the increase in the size of the list after executing append().

Byte arrays

Python supports many different data objects. The most popular of them are byte arrays and bytes. Here, we are looking at the bytearray() function that returns with an array of bytes. It is an object that can be changed, that too in the range of integers 0 to 256. 

At the same time, there is bytes() function in Python. It is immutable and can’t be changed. The object supports values ranging from 0 to 255. Here is an example for the bytearray() object.

b = bytearray(b’python’)

print(id(b))

b.replace(b’p’, b’P’)

print(id(b))

[Out:]

139963525979808

139963525979808

upGrad’s Exclusive Data Science Webinar for you –

Watch our Webinar on How to Build Digital & Data Mindset?

Sets

A set is a random collection of the different data types which are changeable and devoid of duplication. It represents the mathematical side of the data set. The most significant benefit of using a set in Python for the programmers is checking how an element is contained. At the same time, it is not the same in a list. As the data is random and lacks indexing, access is not as simple as it may be in an indexed list. 

Here is an example for the set in Python:

# Python program to
# demonstrate sets
 # Same as {“a”, “b”, “c”}
myset = set([“a”, “b”, “c”])
print(myset)
# Adding element to the set

myset.add(“d”)

print(myset)

 

Dictionaries

Dictionaries are placing the data in a map-like sequence. Developers can use a Dictionary through a curly {} tag set apart with a comma. It has two pairs of values: one is the key, and the other is key values. The data type in dictionaries varies and also can be duplicated. The key should be immutable and a key value to be mutable.

Hee is an example for the dictionaries in Python:

# Creating a Dictionary 
# with Integer Keys
Dict = {1: ‘meeks’, 2: ‘For’, 3: ‘meeks’}
print(“\nDictionary with the use of Integer Keys: “)
print(Dict)
# Creating a Dictionary 
# with Mixed keys
Dict = {‘Name’: ‘meeks’, 1: [1, 2, 3, 4]}
print(“\nDictionary with the use of Mixed Keys: “)
print(Dict)

Immutables in Python

Immutables are rigid and can’t be changed. Some of the immutable data types used in Python are:

  • Numeric data types
  • String
  • Bytes
  • Frozen sets
  • Tuples

Numeric data types

We already saw in the first example of this article how integers are immutable. Apart from the numbers, decimals, fractions, booleans, and complex numbers, all are immutable.

String

Unicode code points are a unique character in Python. They are a representation of the textual data type in Python. Here, such immutable sequences are used for str objects, also known as String. 

Immutable Bytes

If you want to store textual data, you need to encode it. Such encoding creates bytes objects that are immutable. The structure of immutable bytes is similar to Strings.

Frozen Sets

Frozen sets are not so different from their mutable counterparts. But, they are quite limited and not used as much as mutable sets. Frozen sets are equally crucial for performance-related tests and operations.

Tuples

Tuples are sequences of random Python objects. It can be integers, frozen sets, and any other object. Here is an example:

g = (1, 3, 5)
print(id(g))
g = (42, )
print(id(g))
[Out:]
139952252343784
139952253457184

Must Read: Important Python Developer Skills

Conclusion

Python is one of the most innovative programming languages, and its fundamental knowledge can help you boost your career. We saw differences in mutable and immutable in Python. The difference between these objects is quite essential for developers to understand, as it is one of the most asked questions in interviews.

If you are curious about learning python, data science to be in the front of fast-paced technological advancements, check out upGrad & IIIT-B’s Executive PG Programme in Data Science and upskill yourself for the future.

Frequently Asked Questions (FAQs)

1. What are some examples of Mutable Objects?

When something is mutable, it is alterable or has the capacity to change. 'Mutable' in Python refers to an object's capability to modify its values. These are the items that contain a collection of data. Lists, Sets, and Dictionaries are some examples of objects which are a part of Python's Mutable objects.

2. What are some examples of Immutable objects?

When there is no possibility of change through time, it is said to be immutable. In Python, an object is said to be immutable if its value cannot be modified over time. The value of these items is fixed once they are created. The examples of Immutable objects in Python are Numbers (Integer, Rational, Float, Decimal, Complex & Booleans), Strings, Tuples, and Frozen Sets.

3. What are objects in Python?

An object is a basic building component in an object-oriented language. Objects include integers, texts, floating-point numbers, arrays, and dictionaries. An object is, more specifically, any single integer or text. The number 12 is an object, as is the phrase 'hello, world,' and a list is an object that may store additional objects. Every object has a type, which determines what you can do with the object.