Identify the correct output of the Python code: TUP=([1, 2], [3, 4], [5, 6])…
2023
Identify the correct output of the Python code: TUP=([1, 2], [3, 4], [5, 6]) TUP[2][1]=8 print(TUP)
- A.
([1, 2], [8, 4], [5, 6]) / ([1, 2], [8, 4], [5, 6])
- B.
([1, 2], [3, 4], [8, 6]) / ([1, 2], [3, 4], [8, 6])
- C.
([1, 2], [3, 4], [5, 8]) / ([1, 2], [3, 4], [5, 8])
- D.
will generate an error as a Tuple is immutable
Attempted by 1910 students.
Show answer & explanation
Correct answer: C
Key idea: the tuple contains lists, and lists are mutable, so you can change elements inside those lists.
TUP = ([1, 2], [3, 4], [5, 6]) is a tuple whose elements are three lists.
TUP[2] selects the third element, which is the list [5, 6].
TUP[2][1] = 8 sets the element at index 1 (the second element) of that list to 8, changing [5, 6] to [5, 8].
The tuple object remains the same tuple, but its third element (the list) is now [5, 8].
Final output: ([1, 2], [3, 4], [5, 8])