Explanation:
๐น Step 1: Create Tuple
x = (1,2,3)
A tuple named x is created.
Value of tuple:
(1,2,3)
๐น Step 2: Access Index 0
x[0]
Index positions:
0 → 1
1 → 2
2 → 3
So:
x[0] → 1
๐น Step 3: Try to Modify Tuple
x[0] = 9
Python tries to replace:
1 → 9
Expected tuple would be:
(9,2,3)
BUT ❌
๐น Step 4: Important Concept — Tuple is Immutable
๐งฉ Immutable Means
cannot be changed after creation
Tuple elements:
cannot be modified ❌
cannot be deleted ❌
cannot be reassigned ❌
So this operation is illegal:
x[0] = 9
๐น Step 5: Python Raises Error
Python immediately raises:
TypeError
Actual error:
'tuple' object does not support item assignment
๐น Step 6: print(x) Never Executes
Since program already crashed ❌
This line:
print(x)
never runs.
๐ฅ Final Output
TypeError

0 Comments:
Post a Comment