Question 9
Back to Exercise
What is the output of the following Python program?
a = [1]
b = a
a += [2]
print(b)
a = a + [3]
print(a, b)
- A [1, 2] [1, 2, 3] [1, 2] Correct
- B [1] [1, 2, 3] [1]
- C [1, 2] [1, 2, 3] [1, 2, 3]
- D [1, 2] [1, 3] [1, 2]
Correct Answer: Option A
+= extends the shared list in place, so b sees 2. a + [3] creates a new list and rebinds only a.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!