Step 1
t = (1, 2, 3)A tuple t is created with values:
(1, 2, 3)
๐น Step 2
t[0] = 10Here you are trying to change the first element of the tuple.
❗ Problem:
Tuples are immutable, meaning once created, their elements cannot be changed.
So Python raises an error:
TypeError: 'tuple' object does not support item assignment๐น Step 3
print(t)This line is never executed because the program stops when the error occurs in the previous line.
✅ Final Result
The code produces an error, not output:
TypeError: 'tuple' object does not support item assignmentKey Concept
| Data Type | Mutable? |
|---|---|
| List ([]) | Yes |
| Tuple (()) | ❌ No |
✔️ Correct Way (if you want to modify it)
Convert tuple to list, modify, then convert back:
Output:
(10, 2, 3)


0 Comments:
Post a Comment