KnowledgeBoat Logo

Computer Science

What will the following code produce ?

Tup1 = (1,) * 3
Tup1[0] = 2 
print(Tup1)

Python Tuples

5 Likes

Answer

Output
TypeError: 'tuple' object does not support item assignment  
Explanation

(1,) is a single element tuple. * operator repeats (1,) three times to form (1, 1, 1) that is stored in Tup1.
Tup1[0] = 2 will throw an error, since tuples are immutable. They cannot be modified in place.

Answered By

3 Likes


Related Questions