🐍 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) →TrueorFalsevalues.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.

Join the conversation! Your thoughts help the community grow.