list comprehension python even numbers code example
Example 1: get a list of even numbers in python
num_range = 10
even_numbers = [num for num in range(num_range) if num % 2 == 0]
# returns: [0, 2, 4, 6, 8]
# to include 10 in the output do
even_numbers = [num for num in range(num_range + 1) if num % 2 == 0]
Example 2: list comprehension odd numbers python
L=([i%2 for i in range(10)])
print(L)