Sobes.tech
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 tuple in Python cannot be changed after its creation, as tuples are immutable objects.

However, if the tuple contains mutable objects (for example, lists), then the contents of these objects can be modified:

t = (1, [2, 3], 4)
t[1].append(5)  # Modifies the list inside the tuple
print(t)  # (1, [2, 3, 5], 4)

But attempting to change the tuple element itself, for example t[0] = 10, will result in a TypeError.