Junior — Middle
Is it possible to modify the contents of a tuple after its creation?
sobes.tech AI
Answer from AI
No, the contents of a Python tuple cannot be changed after creation — tuples are immutable objects. This means you cannot modify, add, or delete elements.
Example:
t = (1, 2, 3)
# Attempting to modify an element will raise an error
# t[0] = 10 # TypeError: 'tuple' object does not support item assignment
However, if the tuple contains mutable objects (like lists), their contents can be changed:
t = (1, [2, 3], 4)
t[1].append(5)
print(t) # (1, [2, 3, 5], 4)
But the tuple itself cannot be replaced or modified directly.