ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
print([y for x in range(5) if (y := x * x) > 5])
  1. A. [0, 1, 4, 9, 16]
  2. B. [16]
  3. C. [9, 16]
  4. D. [4, 9, 16]

Correct answer: C. [9, 16]

The walrus operator assigns y to x*x in the condition. Only squares greater than 5, which are 9 and 16, are kept by the comprehension.

More Python Programming questions

Exams that ask this topic