Examveda

What will be the output of the following code:
my_tuple = (1, 2, 3)
my_tuple[1] = 4
print(my_tuple)

A. (1, 2, 3)

B. (4, 2, 3)

C. (1, 4, 3)

D. None of these

Answer: Option D

Solution (By Examveda Team)

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."

This Question Belongs to Python Program >> Tuples In Python

Join The Discussion

Comments (1)

  1. Karthika N
    Karthika N:
    7 months ago

    Tuples in Python are immutable, meaning their elements cannot be changed after assignment.. So your answer is wrong.

Related Questions on Tuples in Python