Explanation:
๐น Step 1: Create Tuple
a = (1, 2, [3, 4])
A tuple is created
Tuples are immutable (cannot be changed directly)
But inside it, there is a list [3, 4], which is mutable
๐ Memory:
a → (1, 2, [3, 4])
๐น Step 2: Perform += Operation
a[2] += [5]
This is where the trick happens ๐
๐ Internally, Python does two things:
Modify the list:
[3, 4] → [3, 4, 5]
Try to assign it back:
a[2] = updated_list
⚠️ Important Twist
Step 1 (list modification) ✅ happens successfully
Step 2 (tuple assignment) ❌ fails
๐ Because:
Tuples do not allow item assignment
๐ฅ Result
Python throws an error:
TypeError: 'tuple' object does not support item assignment
Final Output:
Error

0 Comments:
Post a Comment