This program to print first n even numbers can be written in many ways, but as part of this article you will be introduced to a new concept in python, especially with a “for” loop. Value of variable increments or decrements automatically by step which can be given as part of the range function. You will also go through other ways of printing even numbers in this article.

# Way 1 – Printing even numbers in Python using Step concept

#Initialise variable with number of even numbers you want n=20; #In the below example the syntax of for loop is as below #for i in range(start,stop,step): #start is the starting point for the range #stop is until which number the range should belong to #step is the number which you want to step by every time the statements gets looped for num in range(2,n*2+1,2): print num;

In the above example, we have given n*2 as stop, the reason being, if you are to display n even numbers, in the range of 1 to n you will only have n/2 even numbers, and for printing n even numbers you need to double the range, which results in getting the desired number of even numbers i.e. n even numbers.

# Way 2 – Printing even numbers in Python using simple for loop & if condition

#Initialise variable with number of even numbers you want n = 20; #run the numbers starting from 2 to n*2+1 for num in range(2,n*2+1): if num%2 ==0: print num;

# Way 3 – Printing even numbers in Python using simple while loop

#Initialise variable with number of even numbers you want n = 20; cnt = 1; #run the numbers starting from 1 to n while(cnt<=(n)): print (cnt*2); cnt = cnt + 1;

Like this there will be n number of solutions for the same problem, please feel free to share any of your queries in form of queries, we would be more than happy to respond to you.