ProgrammingHardAccenture pattern

What is printed?

What is printed?
print([x if x % 2 == 0 else -x for x in range(5)])
  1. A. [0, 1, 2, 3, 4]
  2. B. [0, -1, 2, -3, 4]
  3. C. [-0, -1, -2, -3, -4]
  4. D. [0, -1, -2, -3, 4]

Correct answer: B. [0, -1, 2, -3, 4]

This is a conditional expression INSIDE the comprehension's output (not a filter): even x values pass through unchanged, odd x values are negated: 0, -1, 2, -3, 4.

More Python Programming questions

Exams that ask this topic