Python Programming - Variables - Discussion

Discussion Forum : Variables - General Questions (Q.No. 36)
36.
What is the output of the following code:
my_tuple = (1, 2, 3)
my_tuple[0] = 4
print(my_tuple)
(1, 2, 3)
(4, 2, 3)
An error is raised
None of the above
Answer: Option
Explanation:

In the given code, the tuple my_tuple is created with the values (1, 2, 3). However, the subsequent attempt to modify the first element of the tuple using the assignment my_tuple[0] = 4 will result in an error.

Tuples in Python are immutable, meaning their elements cannot be modified after the tuple is created. Therefore, the code will raise a TypeError when trying to modify the tuple.

The output of the code will be:

TypeError: 'tuple' object does not support item assignment
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.