Understanding Mutable and Immutable Data Types in Python

Rasmi Ranjan Swain
3 min readJun 6, 2023

--

Python, as a dynamically typed language, offers various data types to handle different kinds of information. Among these data types, a crucial distinction is made between mutable and immutable types. Understanding this distinction is essential as it affects how variables are stored, passed, and modified in Python. In this blog post, we will explore the concept of mutable and immutable data types, their characteristics, and the implications they have on programming in Python.

Understanding Mutable and Immutable Data Types in Python

Immutable Datatypes:

Immutable datatypes are those whose values cannot be changed after they are created. Any attempt to modify an immutable object will result in the creation of a new object.

Here are some common examples of immutable datatypes in Python:

Numbers (int, float, complex)

Once a number is assigned to a variable, it cannot be changed. For example:

x = 5
x = x + 1 # A new object is created with the value 6

Strings

Strings are immutable in Python. If you try to modify a string, a new string object will be created instead. For example:

s = "Hello"
s = s + " World" # A new string object is created with the value "Hello World"p

Tuples

Tuples are ordered collections of elements and are immutable. Once a tuple is created, you cannot modify its elements. For example:

t = (1, 2, 3)
# t[0] = 4 # This will raise a TypeError since tuples are immutable

Immutable data types are beneficial in scenarios where data integrity and consistency are crucial. Since their values cannot be changed, they are reliable for use as keys in dictionaries or elements in sets.

Mutable Datatypes

Mutable datatypes, in contrast to immutable datatypes, can be modified after creation. These objects can have their values updated, added, or removed without creating a new object.

Let’s explore some commonly used mutable datatypes in Python:

Lists

Lists are ordered collections of elements enclosed in square brackets. They can be modified by adding, removing, or changing elements. For example:

l = [1, 2, 3]
l.append(4) # [1, 2, 3, 4]
l[0] = 5 # [5, 2, 3, 4]

Dictionaries

Dictionaries are key-value pairs enclosed in curly braces. The values associated with keys in a dictionary can be modified or new key-value pairs can be added. For example:

d = {"name": "John", "age": 25}
d["age"] = 26 # {"name": "John", "age": 26}
d["city"] = "London" # {"name": "John", "age": 26, "city": "London"}

Sets

Sets are unordered collections of unique elements. Elements can be added or removed from a set. For example:

s = {1, 2, 3}
s.add(4) # {1, 2, 3, 4}
s.remove(1) # {2, 3, 4}

Mutable datatypes provide flexibility when you need to modify or update data. They are useful for scenarios that involve frequent changes or dynamic data structures

Impact of Mutability

Understanding the mutability concept is essential because it affects how data is passed and shared between variables and functions. When a mutable object is assigned to a new variable, changes made to one variable affect all other variables referencing the same object. This behavior is known as aliasing.

For example:

a = [1, 2, 3]
b = a
b.append(4)
print(a) # [1, 2, 3, 4]

On the other hand, with immutable objects, a new object is created when assigning it to a different variable or passing it as an argument to a function. Any modifications made to one variable do not affect others.

For example:

x = 5
y = x
y += 1
print(x) # 5

Conclusion

In Python, understanding the concepts of mutable and immutable datatypes is crucial for writing efficient and bug-free code. Immutable datatypes ensure data consistency and can be used as keys in dictionaries or elements in sets. Mutable datatypes allow for flexibility and modifications to data structures. Knowing the characteristics and behavior of these datatypes enables you to make informed decisions while designing algorithms and managing data in your Python programs.

Level Up Coding

Thanks for being a part of our community! Before you go:

  • 👏 Clap for the story and follow the author 👉
  • 🔔 Follow us: LinkedIn

--

--

Rasmi Ranjan Swain
Rasmi Ranjan Swain

Written by Rasmi Ranjan Swain

Python and AI Lover ,Digital Entrepreneur

No responses yet