Python · Question 15 · Hard

What will be the output of the following Python code?

t = ([1], 2)
try:
    t[0] += [3]
except TypeError:
    print("error", t)
  1. error ([1, 3], 2)
  2. error ([1], 2)
  3. ([1, 3], 2)
  4. Nothing is printed
Answer and explanation

Answer: A. error ([1, 3], 2)

Explanation: t[0] += [3] first extends the list in place (lists are mutable) and then tries to assign the result back to t[0], which raises TypeError because tuples are immutable. The list was already changed, so the tuple shows [1, 3].

Previous: What will be the output of the following Python code? · Next: What will be the output of the following Python code?

Practise python

Try a fresh set of python questions with an explanation after every answer, free and without signing up: Python practice. Or sit a full placement mock test.

More python questions

More technical & programming practice

Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems · Computer Networks · Software Engineering

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge