KnowledgeBoat Logo

Computer Science

What will be the output of the following code ?

tuple1 = (1, 2, 3)
tuple2 = tuple1
tuple1 += (4, )
print(tuple1 == tuple2)
  1. True
  2. False
  3. tuple1
  4. Error

Python Tuples

2 Likes

Answer

False

Reason — In the above code, tuple1 is initially assigned the tuple (1, 2, 3), and tuple2 references the same tuple. When the line tuple1 += (4, ) is executed, it modifies tuple1 to (1, 2, 3, 4) because the += operator creates a new tuple rather than modifying the existing one. Thus, tuple1 now contains (1, 2, 3, 4), while tuple2 remains (1, 2, 3). Therefore, the expression tuple1 == tuple2 evaluates to False.

Answered By

2 Likes


Related Questions