python even numbers in a list code example

Example 1: program to find even numbers in python

m = int(input("Enter number"))
if m % 2 == 0:
    print(m,"is an even number")
else:
    print(m,"is an odd number")

Example 2: recursive python program to print numbers from n to 1

# Read the input
n = int(input())

def rec(n):
    if n==0:
        print(0)
    else:
        print(-n)
        rec(n-1)
        print(n)

rec(n)

Example 3: 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 4: python 3 numbers of a range is even

def check(element):
    return all(ord(i)%2 == 0 for i in element)  # all returns True if all digits i is even in element

lst = [str(i) for i in range(1000,3001)]        # creates list of all given numbers with string data type
lst = list(filter(check,lst))                   # filter removes element from list if check condition fails
print(",".join(lst))