Python program to print even numbers from 1 to 100 using for loop

In this post, you will learn how to write a Python program to print all even numbers between 1 to 100 using using for loop, while loop and if-else statement. Such a type of question is generally asked in a logical programming interview.

In the given Python program, we have iterated from start 1 to 100 using a loop and checked each value, if num % 2 == 0. If the condition is satisfied, i.e., num % 2 == 0 is true, then only print the number.

# Python program to print Even Numbers in given range
  
start, end = 1, 100
  
# iterating each number in list
for num in range(start, end + 1):
      
    # checking condition
    if num % 2 == 0:
        print(num, end = " ")

Output of the above code:

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 

We started the range from 2 and used the counter value of 2 in the given Python program to print even numbers between 1 to 100.

# Python program to print Even Numbers from 1 to 100

max = 100

for num in range(2, max+1, 2):
    print("{0}".format(num))

Output of the above code:

2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100

In the given Python program, we have applied the same logic as above, we just replaced the for loop with a while loop.

# Python program to print Even Numbers in given range
# using while loop

max = 100
num = 1

while num <= max:
    if(num % 2 == 0):
        print("{0}".format(num))
    num = num + 1

Output of the above code:

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 

In the given Python program, we have used a while loop to check weather the num variable is less than or equal to 100. If the condition satisfies, then only the rest of the code will be executed, else not.

num = 2
while num <= 100:
    print(num)
    num = num + 2

Output of the above code:

2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given starting and end points, write a Python program to print all even numbers in that given range. 

    Example:

    Input: start = 4, end = 15
    Output: 4, 6, 8, 10, 12, 14
    
    Input: start = 8, end = 11
    Output: 8, 10

    Example #1: Print all even numbers from given list using for loop Define start and end limit of range. Iterate from start till the range in the list using for loop and check if num % 2 == 0. If the condition satisfies, then only print the number. 

    Python3

    for num in range(4,15,2):

        print(num)

    Output:

    4 6 8 10 12 14 

      Example #2: Taking range limit from user input 

    Python3

    start = int(input("Enter the start of range: "))

    end = int(input("Enter the end of range: "))

    for num in range(start, end + 1):

        if num % 2 == 0:

            print(num, end=" ")

    Output:

    Enter the start of range: 4
    Enter the end of range: 10
    4 6 8 10 

    Example#3 Taking range limit user input and uses skip sequence number in range function which generates the all-even number. 

    Python3

    start = int(input("Enter the start of range: "))

    end = int(input("Enter the end of range: "))

    start = start+1 if start&1 else start

    [ print( x ) for x in range(start, end + 1, 2)]

    Output:

    Enter the start of range: 4
    Enter the end of range: 10
    4 6 8 10 

    Method: Using recursion 

    Python3

    def even(num1,num2):

        if num1>num2:

            return

        print(num1,end=" ")

        return even(num1+2,num2)

    num1=4;num2=15

    even(num1,num2)

    Method: Using the lambda function 

    Python3

    a=4;b=15

    li=[]

    for i in range(a,b+1):

        li.append(i)

    even_num = list(filter(lambda x: (x%2==0),li)) 

    print(even_num)

    Output

    [4, 6, 8, 10, 12, 14]

    Method: Using list comprehension 

    Python3

    x=[i for i in range(4,15+1) if i%2==0]

    print(*x)

    Method: Using enumerate function 

    Python3

    a=4;b=15;l=[]

    for i in range(a,b+1):

      l.append(i)

    print([a for j,a in enumerate(l) if a%2==0])

    Output

    [4, 6, 8, 10, 12, 14]

    Method: Using pass 

    Python3

    a=4;b=15

    for i in range(a,b+1):

      if i%2!=0:

        pass

      else:

        print(i,end=" ")

    Method: Using Numpy.Array

    Python3

    import numpy as np

    a=4;b=15

    li= np.array(range(a, b+1))

    even_num = li[li%2==0];

    print(even_num)

    Output:

    [ 4  6  8 10 12 14]

    How do you print even numbers from 1 to 100 in Python?

    Print even numbers between 1 to 100 using a for loop In the given Python program, we have iterated from start 1 to 100 using a loop and checked each value, if num % 2 == 0. If the condition is satisfied, i.e., num % 2 == 0 is true, then only print the number.

    How do you print even numbers in a for loop in Python?

    Example #1: Print all even numbers from given list using for loop Define start and end limit of range. Iterate from start till the range in the list using for loop and check if num % 2 == 0. If the condition satisfies, then only print the number.

    What are the even numbers from 1 to 100?

    What are the Even Numbers from 1-100? The list of even numbers from 1-100 is as follows: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70,72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100.

    How do I print only even numbers in Python?

    A simple approach to print even numbers in a list is to Iterate each element in the list using for loop and check if num % 2 == 0, If the condition satisfies, then only print the number. For that, you can use for loop or filter & lambda function or list comprehension with if statements.