Immutability of Tuple
-
This lesson explains why tuple values cannot be changed after creation in Python.
What is Immutability in Tuple?
Immutability means the data cannot be changed after it is created.
In Python, tuples are immutable, which means:
You cannot add
You cannot remove
You cannot modify elements inside a tuple
Once a tuple is created, it stays the same.
Daily Life Example
Think of:
A roll number of a student — once assigned, it doesn't change
Date of birth — fixed information
Car registration number — permanent
Creating a Tuple
numbers = (10, 20, 30, 40)
print(numbers)
Explanation
Tuple is created using parentheses ()
Values are stored in order
Trying to Modify Tuple (Not Allowed)
numbers = (10, 20, 30)
numbers[1] = 99
Explanation
This gives an error:
TypeError: 'tuple' object does not support item assignment
Because tuples are immutable.
Trying to Add Elements (Not Allowed)
numbers = (1, 2, 3)
numbers.append(4)
Explanation
Error occurs because tuples do not have append() method.
Trying to Remove Elements (Not Allowed)
numbers = (5, 6, 7)
del numbers[1]
Explanation
Error → Tuples cannot delete individual items.
What is Allowed in Tuple?
Accessing elements
Looping
Counting
Indexing
Nested mutable objects can change
Nested Mutable Object
data = (1, 2, [3, 4])
data[2][0] = 99
print(data)
Explanation
Tuple itself is immutable,
but the list inside tuple can change.