This blog contain 10 examples of while loop
Example 1 : Printing numbers from 1 to 10
num = 1
while num <= 10:
print(num)
num+=1
Output
1 2 3 4 5 6 7 8 9 10
Example 2 : Printing even numbers from 1 to 50
num = 1 while num <= 50: if num%2 == 0: print(num) num+=1
Output
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
Example 3 : Printing odd numbers from 1 to 50
num = 1 while num <= 50: if num%2 != 0: print(num) num+=1
Output
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
Example 4 : Printing table of 2
num = 1 table2 = 2 while num <= 10: res = table2 * num print('%d * %d = %d'%(table2,num,res)) num+=1
Output
2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 2 * 10 = 20
Example 5 : Printing square of number from 1 to 10
num = 1 while num <= 10: res = num*num print('Square of %d : %d'%(num,res)) num+=1
Output
Square of 1 : 1 Square of 2 : 4 Square of 3 : 9 Square of 4 : 16 Square of 5 : 25 Square of 6 : 36 Square of 7 : 49 Square of 8 : 64 Square of 9 : 81 Square of 10 : 100
Example 6 : Finding area of a circle for radius 1 to 10
num = 1 pi = 3.14 while num <= 10: radius = num * num area_of_circle = pi * radius print('Area of circle with radius %d = %.2f'%(num,area_of_circle)) num+=1
Output
Area of circle with radius 1 = 3.14
Area of circle with radius 2 = 12.56
Area of circle with radius 3 = 28.26
Area of circle with radius 4 = 50.24
Area of circle with radius 5 = 78.50
Area of circle with radius 6 = 113.04
Area of circle with radius 7 = 153.86
Area of circle with radius 8 = 200.96
Area of circle with radius 9 = 254.34
Area of circle with radius 10 = 314.00
Example 7 : Asking user to input name and printing it for 5 times
name = input('Enter your name : ') count = 1 while count <= 5: print('[%d]Your Name:%s'%(count,name)) count+=1
Output
Enter your name : Kobe Bryant [1]Your Name:Kobe Bryant [2]Your Name:Kobe Bryant [3]Your Name:Kobe Bryant [4]Your Name:Kobe Bryant [5]Your Name:Kobe Bryant
Example 8 : Asking user to input any 5 numbers and calculating square of each number and displaying the result
count = 0 num = [0,0,0,0,0] print('Please enter any 5 numbers') while count <= 4: num[count] = int(input('Enter number : ')) count += 1 count = 0 print('Calculating Square of each number') while count <= 4: val = num[count] sqr = val*val print('Square of %d is %d'%(val,sqr)) count += 1
Output
Please enter any 5 numbers Enter number : 2 Enter number : 3 Enter number : 4 Enter number : 5 Enter number : 6 Calculating Square of each number Square of 2 is 4 Square of 3 is 9 Square of 4 is 16 Square of 5 is 25 Square of 6 is 36
Example 9 : Asking user to input any string and then displaying each character of a string.
str = input("Enter any string : ")
l = len(str)
count = 0
while count <= l-1:
print(str[count])
count += 1
Output
Enter any string : New York N e w Y o r k
Example 10 : Asking user to input string and displaying characters in reverse order
str = input("Enter any string : ") l = len(str) count = 1 while count <= l: print(str[-count]) count += 1
Output
Enter any string : New York k r o Y w e N