Write a python program to print even numbers from given range using while loop

Use a while loop with if statement condition i % 2 == 0 then only print the first 10 even numbers in Python.

Simple example code print even numbers of user input value using a while loop in Python. You can use list objects to store value, here we are printing the value using the end keyword.

x = int(input("Enter a number: "))
i = 1

while i <= x:
    if i % 2 == 0:
        print(i, end=" ")
    i = i + 1

Output:

Write a python program to print even numbers from given range using while loop

Without if statement

num = 2

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

Output:

2
4
6
8
10
12
14
16
18
20

Do comment if you have any doubts or suggestions on this Python even number topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Write a python program to print even numbers from given range using while loop

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

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 all even numbers in Python while loop?

    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.

    How do you print only even numbers in a while loop?

    Use a while loop with if statement condition i % 2 == 0 then only print the first 10 even numbers in Python.

    How do you print 10 even numbers in Python?

    To write a program to print the first 10 even numbers in Python, you have a set range of for loop. In this example, you have to use max loop 10. Then if the current number % 2 == 0 matches the condition in the if statement then prints it.