🐍 Introduction
In Python, everything is an object, and objects have data stored in them. Some objects can be changed after they are created , while others cannot be changed once created . These two categories are known as mutable and immutable datatypes. Knowing the difference is crucial because it determines how your variables behave when you attempt to update them.
🔄 What is a Mutable Datatype?
A mutable datatype is an object that you can change even after creating it. This means you can update, add, or remove elements inside it without creating a new object.
Examples of Mutable Datatypes in Python
Lists ( list
) → Used to store multiple items.
Dictionaries ( dict
) → Store data in key-value pairs.
Sets ( set
) → Store unique items without order.
Byte Arrays ( bytearray
) → Store binary data.
Example with a List
numbers = [1, 2, 3]
print(numbers) # [1, 2, 3]
numbers.append(4)
print(numbers) # [1, 2, 3, 4]
👉 Here, the list numbers
was changed by adding 4
. The same object was updated, which means lists are mutable.
🔒 What is an Immutable Datatype?
An immutable datatype is an object that cannot be changed after it is created. If you try to modify it, Python creates a new object instead of updating the old one.
Examples of Immutable Datatypes in Python:
Strings ( str
) → Text data.
Tuples ( tuple
) → Ordered collection of items.
Integers ( int
) → Whole numbers.
Floats ( float
) → Decimal numbers.
Booleans ( bool
) → True
or False
values.
Frozensets ( frozenset
) → Immutable version of a set.
Example with a String
name = "Python"
print(id(name)) # Memory address of the object
name = name + " Programming"
print(name) # Python Programming
print(id(name)) # New memory address
👉 The memory address changes because a new string object is created. Strings cannot be directly modified, so they are immutable.
⚖️ Key Differences Between Mutable and Immutable Datatypes
Here is a detailed table that shows the main differences between mutable and immutable datatypes in Python:
Feature | Mutable Datatypes | Immutable Datatypes |
---|
Changeability | You can change or update the object after it is created. For example, you can add, remove, or modify elements inside a list or dictionary. | Once created, the object cannot be changed. If you try to modify it, Python creates a new object instead. |
Memory Usage | The same object in memory is updated when changes are made, so memory is reused. | Every modification creates a new object with a new memory location, which can use more memory. |
Examples | Lists, Dictionaries, Sets, Bytearrays. | Strings, Tuples, Integers, Floats, Booleans, Frozensets. |
Performance Consideration | More efficient when frequent changes are needed because the same object is updated. | Safer because data cannot be changed accidentally, but may be slower and use more memory when modified repeatedly. |
🧩 Example: List vs Tuple
# Mutable List
my_list = [1, 2, 3]
my_list[0] = 10
print(my_list) # [10, 2, 3]
# Immutable Tuple
my_tuple = (1, 2, 3)
# my_tuple[0] = 10 # ❌ This will cause an error
print(my_tuple) # (1, 2, 3)
👉 The list can be updated, but trying to change a tuple gives an error, proving that tuples are immutable.
📌 Summary
In Python, mutable datatypes (like lists and dictionaries) can be changed after creation, while immutable datatypes (like strings and tuples) cannot be changed once created. When you try to modify an immutable object, Python creates a new one instead. Understanding this difference helps you write better code because it affects memory usage, performance, and how variables behave when passed around in your program.