ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
total = 0
squares = [total := total + v for v in [1, 2, 3]]
print(squares, total)
  1. A. [1, 2, 3] 0
  2. B. [1, 3, 6] 0
  3. C. [1, 4, 9] 6
  4. D. [1, 3, 6] 6

Correct answer: D. [1, 3, 6] 6

An assignment expression inside a comprehension binds the name in the containing scope, so total accumulates a running sum and keeps its final value 6 after the comprehension.

More Python Programming questions

Exams that ask this topic