Python · Question 7 · Medium

What will be the output of the following Python code?

a = [1, 2, 3]
b = a
b.append(4)
print(a)
  1. [1, 2, 3]
  2. [4]
  3. None
  4. [1, 2, 3, 4]
Answer and explanation

Answer: D. [1, 2, 3, 4]

Explanation: b = a does not copy the list; both names refer to the same list object, so appending through b is visible through a.

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