Python 3 program to print all even numbers in a range:
In this example, we will learn how to print all the even numbers in a given range in 4 different ways. An even number is a number that is perfectly divisible by 2 or the remainder is 0 if you divide that number by 2. To find out all even numbers in a given range, we will take the start and the end values as inputs from the user.
We will ask the user to enter these values. The program will iterate over the numbers in the given range one by one. If an even number is found, it will print it to the user.
We can use the modulo operator, %
to check if a number is even or not. The modulo operation, a%b
returns the remainder of a/b
. So, for any even number n
, the value of n%2
will be always 0.
Steps to find even numbers in a range :
- Take the lower limit and the upper limit as inputs from the user. Store the values in two separate variables.
- Run one loop from the lower limit to the upper limit. For example, if the lower limit is 2 and the upper limit is 10, it will run from 2 to 10.
- Check if the number is divisible by 2 or not on each iteration of the loop.
- If it is divisible by 2, print out the number. Else, move to the next iteration.
- Exit when the loop is completed.
We will learn four different ways. The first two methods will use the above steps and the last two methods will use a slightly different optimized approach to find the even numbers.
Method 1: Python 3 Program to find the even numbers with a for loop:
The following program will find all the even numbers in the range of lower_limit
and upper_limit
.
lower_limit = int(input("Enter the lower limit : "))
upper_limit = int(input("Enter the upper limit : "))
for i in range(lower_limit,upper_limit+1):
if(i%2 == 0):
print(i)
You can also download the above program from Github.
Sample Output:
Explanation:
-
The
input()
function is used to read the user-input value. As this is a number, it is converted to an integer by using theint(input())
function. Theinput()
function returns the user-entered data as a string. We are using theint()
function to convert the string to an integer. Note that the user-entered value should be an integer, or else it will throw one exception. -
As explained above, we need to store the lower and the upper range or limit in two different variables to iterate between them. In this example program, the lower limit is assigned to the
lower_limit
variable and the upper limit is assigned to theupper_limit
variable. -
We are using a
for
loop to iterate between the numbers. You can also use onewhile
loop. -
The loop statement
for i in range(lower_limit,upper_limit+1)
indicates that the loop will run fromlower_limit
toupper_limit
. e.g. if thelower_limit
is 1 and theupper_limit
is 3, the loop will run fori=1, i=2
andi=3
. -
Inside the loop, we are checking if the number is divisible by 2 or not. We are using the modulo operator,
i%2
to check ifi
is an even number or odd. If the value ofi
is perfectly divisible by 2, it will return ‘0’. The modulo operator is the easiest way to test if a number is perfectly divisible by another number or not. -
If the current value of
i
is perfectly divisible by 2, print out its value. Else move to the next iteration.
Method 2: By using a while loop:
We can use a while
loop to find out all the even numbers in a given range. The syntax of the while loop
is:
while(condition):
# code block
It checks one condition
and executes its body until the condition is True
. We can use a while loop
to write the program as below:
lower = int(input("Enter the lower range: "))
upper = int(input("Enter the upper range: "))
while lower <= upper:
if lower % 2 == 0:
print(lower)
lower += 1
Download the code on Github
- The
while
loop will run until the value oflower
is smaller or equal toupper
. - On each step, it checks if the value of
lower
iseven
or not. If yes, it prints its value. - At the end of each iteration, it is incrementing the value of
lower
by1
.
If you run this program, it will print similar output.
Enter the lower range: 3
Enter the upper range: 10
4
6
8
10
Method 3: By using a while loop and jumping numbers:
Instead of iterating over all the numbers, we can jump between the even numbers. We know that if one number is even, the next number in the number series will be odd and so on. We can start the loop from the first even number and on each iteration, we can increment its value by 2 to point to the next even number.
The following program shows how to write it with a while
loop:
lower = int(input("Enter the lower range: "))
upper = int(input("Enter the upper range: "))
if lower % 2 != 0:
lower = lower + 1
while lower <= upper:
print(lower)
lower += 2
Download the code on Github
This is similar to the previous program, but we are changing the value of the lower
variable to the first even number before the loop starts. If the user-given lower range is an even value, it will not change it. Else, it increments the lower
variable by 1. On each iteration, the value of the lower
variable is incremented by 2.
It will print similar output:
Enter the lower range: 4
Enter the upper range: 12
4
6
8
10
12
Method 4: By using a for loop and jumping numbers:
The range()
function returns a list of numbers. We can use this function to print the even numbers in a given range. The syntax of this function is:
range(start, stop, step)
- The
start
parameter defines the starting position. It is an optional value and it is 0 by default. - The
stop
parameter defines the position to stop. - The
step
parameter defines the steps in between the numbers. It is 1 by default. It is an optional value.
If we provide 2 as the step
, it will skip one number on each step in the sequence.
To print out all the even numbers, we can point the value of lower
to the smallest even value and use it with the range()
function as below:
lower = int(input("Enter the lower range: "))
upper = int(input("Enter the upper range: "))
if lower % 2 != 0:
lower = lower + 1
for i in range(lower, upper + 1, 2):
print(i)
Sample output:
Enter the lower range: 1
Enter the upper range: 10
2
4
6
8
10
Get the code on Github
Conclusion :
In this example, we have learned how to print all the even numbers in a range in Python in different ways. We can use the same methods to find out all the odd numbers as well.
You might also like :
- Python add and remove elements from a list
- Python program to sort values of one list using the second list
- Python program to find the maximum and minimum element in a list
- Python program to find the multiplication of all elements in a list
- Python program to convert a list to string
- Python add and remove elements from a list