?
What will be the output of the following code:
What will be the output of the following code:
my_tuple = (1, 2, 3)
my_tuple[1] = 4
print(my_tuple)
my_tuple[1] = 4
print(my_tuple)
Answer & Solution
Correct Answer:
Option
D
Tuples in Python are immutable, meaning their elements cannot be changed after creation.
In the given code, my_tuple = (1, 2, 3) initializes a tuple with three elements.
The line my_tuple[1] = 4 attempts to modify the second element of the tuple, which is not allowed.
As a result, Python raises a TypeError stating that 'tuple' object does not support item assignment.
Since the code causes an error and does not produce any valid tuple as output, the correct answer is "None of these."
In the given code, my_tuple = (1, 2, 3) initializes a tuple with three elements.
The line my_tuple[1] = 4 attempts to modify the second element of the tuple, which is not allowed.
As a result, Python raises a TypeError stating that 'tuple' object does not support item assignment.
Since the code causes an error and does not produce any valid tuple as output, the correct answer is "None of these."
Join the Discussion
Login to post a comment or share your explanation.
Login