Mutable vs Immutable

Understanding the difference between mutable and immutable objects is essential for writing predictable and bug-free code.
Mutable and Immutable
You can think of variables in Python as “containers” that store values in memory. But some containers can change their contents, while others cannot.
Immutable Variables
When a variable is immutable, once you create it, the value cannot be changed. Instead, if you modify it, Python will create a new object in memory instead of altering the original one.
Examples include:
- Numbers (
int
,float
) - Strings (
str
) - Tuples (
tuple
) - Booleans(
bool
)
Example 1:
Here is a simple example using a string — which is immutable.
# Strings are immutable
s = "hello"
print(id(s)) # memory address before change
s = s.upper() # creates a NEW string
print(s) # Output: HELLO (is a new string created)
print(id(s)) # memory address changes
When you call .upper()
, Python creates a new string object because strings cannot be modified in place. You can check the id()
— Python uses this to show where an object lives in memory.
Notice how the memory address changes after modification — that’s because Python created a new string object.
Contrast to Mutable Variables :
Mutable Variables
When a variable is mutable, you can change its contents without creating a new object. Python will simply update the same memory location.
Examples include:
- Lists (
list
) - Dictionaries (
dict
) - Sets (
set
)
Example 2:
Now, let’s see how lists behave — since lists are mutable.
# Lists are mutable
lst = [1, 2, 3]
print(id(lst)) # memory address before change
lst.append(4) # modifies the same list
print(lst) # Output: [1, 2, 3, 4] (same list updated)
print(id(lst)) # memory address remains the same
Here, the append()
method modifies the same list rather than creating a new one.Python updates the same object in memory — the id()
doesn’t change.
We can add, remove, or modify elements directly.
This is why mutable objects are often used when you want to update or store multiple values dynamically.
Why It Matters in Python
- For performance, Python uses these behaviors behind the scenes to manage memory efficiently. Mutable objects can be updated without creating new copies.
- Understanding them helps you avoid common bugs — especially when passing variables into functions - for instance, never use mutable objects as default arguments in functions unless you intend to share state.
Let’s look at a common trap :
If you create a function with a mutable default argument, Python will reuse it between calls.
def add_item(item, collection=[]):
collection.append(item)
return collection
print(add_item(1)) # [1]
print(add_item(2)) # [1, 2] — same list reused!
Python doesn’t create a new list each time — it keeps reusing the same one.
Fix:
You can use None
as a default and create a new list only when needed.
def add_item(item, collection=None):
if collection is None:
collection = []
collection.append(item)
return collection
Output:
print(add_item(1)) # [1]
print(add_item(2)) # [2]
Now each call creates its own list safely.
In Summary
-
Immutable objects are safe and predictable — they never change once created.
-
Mutable objects are flexible — they can be changed, but you must handle them carefully.
You can use immutable objects for fixed data, and mutable ones when you need to update data dynamically.
Now that you understand Mutable and Immutable Variables, your next step is to learn about Constant in Python in detail.