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)

  1. A.

    ([1, 2], [8, 4], [5, 6]) / ([1, 2], [8, 4], [5, 6])

  2. B.

    ([1, 2], [3, 4], [8, 6]) / ([1, 2], [3, 4], [8, 6])

  3. C.

    ([1, 2], [3, 4], [5, 8]) / ([1, 2], [3, 4], [5, 8])

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

  1. TUP = ([1, 2], [3, 4], [5, 6]) is a tuple whose elements are three lists.

  2. TUP[2] selects the third element, which is the list [5, 6].

  3. TUP[2][1] = 8 sets the element at index 1 (the second element) of that list to 8, changing [5, 6] to [5, 8].

  4. 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])

Explore the full course: Rssb Senior Computer Instructor