ProgrammingHardIBM · Deloitte pattern

What is printed?

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

Correct answer: B. [2, 4]

The generator expression first yields the even values 0, 2, 4; converting to a list and then slicing [1:3] keeps the second and third elements, 2 and 4.

More Python Programming questions

Exams that ask this topic