Skip to main content

Command Palette

Search for a command to run...

Immutable and Mutable in Python

Understand the difference between mutable and immutable data types in Python with real-world examples and clear explanations.

Updated
2 min read
Immutable and Mutable in Python
H

I believe the best way to master a concept is to explain it to someone else. I’m a Full Stack Developer navigating the ecosystems of JavaScript and Python, building everything from responsive frontends to robust backends. I use this space to document my learning journey, break down complex topics, and share practical solutions to the bugs I encounter. Let's learn in public together!

In Python, data types are categorized into two groups based on their ability to be changed after creation:

  • Mutable: Can be changed after creation (e.g., list, set, dict, bytearray, array)

  • Immutable: Cannot be changed after creation (e.g., int, float, bool, str, tuple, frozenset, bytes)

🔒 Understanding Immutable Types

Let’s explore immutability with an example using strings and integers.

Open your Python shell (run python in your command prompt, Git Bash, or VS Code terminal) and try the following:

username = "harsh"
username = "python"

This won't throw any error. But since we said strings are immutable, what does that really mean?

Let’s break it down further using an integer:

x = 10
y = x

print(x) # Output: 10
print(y) # Output: 10

Now change x:

x = 15

#Now

print(x)  # Output: 15
print(y)  # Output: 10

🧠 So, What’s Happening?

In Python, everything is treated as an object. When you assign a value like:

username = "harsh"

You are creating a reference to the object "harsh" in memory.

When you change it to:

username = "python"

You’re actually pointing username to a new reference, because the original string "harsh" is immutable — it cannot be changed in place. So Python creates a new object for "python" and makes username refer to that.

Old objects without references are automatically cleaned up by Python’s garbage collector.

After changing username to “python“

Now no variable is pointing to “harsh“, so it will be garbage collected and memory will be released

This is what immutability means: the object’s memory reference does not change or get modified.

Same goes for the x = 10, y = x example:

  • x and y both pointed to the same integer 10.

  • When x was reassigned to 15, it started pointing to a new object — but y still pointed to the old one.

when we assigned a new value to x as x = 15

there for we get result as x = 15 and y = 10

🔄 Understanding Mutable Types — Example with List

Let’s now look at a mutable data type — a list:

fruits = ["apple", "banana", "cherry"]
basket = fruits

Both fruits and basket now point to the same list object in memory.

Let’s make a change:

fruits.append("mango")
# Now check both:

print(fruits)  # Output: ['apple', 'banana', 'cherry', 'mango']
print(basket)  # Output: ['apple', 'banana', 'cherry', 'mango']

✅ Both reflect the change, because lists are mutable — changes made through one reference are visible through others pointing to the same object.