Question discussion

What is the output of the following Python code?a = [1, 2, 3] b = a a = a + [4] print(b, len(a))

1534Medium DSSSB TGT Computer Science 0 replies

What is the output of the following Python code?

a = [1, 2, 3]
b = a
a = a + [4]
print(b, len(a))
  • A[1, 2, 3, 4] 4
  • B[1, 2, 3] 3
  • C[1, 2, 3] 4
  • D[1, 2, 3, 4] 3
Correct answer: C

Explanation

a + [4] builds a new list and rebinds a, so b still refers to the original [1, 2, 3] while len(a) is 4.

Discussion 0

Nobody has replied yet. Start the discussion!