What is the output of the following? print([x for x in range(5) if x % 2 == 0])

Python Developer Medium

Python Developer — Medium

What is the output of the following? print([x for x in range(5) if x % 2 == 0])

Key points

  • List comprehension filters numbers based on the condition x % 2 == 0.
  • Only even numbers from the range 0 to 4 are included in the output.
  • The syntax [x for x in range(5) if x % 2 == 0] generates the list.

Ready to go further?

Related questions