Table of Contents hide

While Loop Examples

Example : A Python program to print all the numbers from 1 to 10 using a While loop.
i = 1
while i <= 10:
  print(i)
  i = i + 1

Output:
1
2
3
4
5
6
7
8
9
10

-------------  OR -------------

i = 1
while i <= 10:
  print(i, end=" ")
  i = i + 1

Output:
1 2 3 4 5 6 7 8 9 10

-------------  OR -------------

x=int(input("Enter First Value = "))
y=int(input("Enter Second Value = "))

i = x
while i <= y:
  print(i, end=" ")
  i = i + 1

Output:
Enter First Value = 15
Enter Second Value = 35
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 
Example : A Python program to print all the numbers along with their total from 1 to 10 using a While loop.
i = 1
sum = 0
while i <= 10:
  print(i, end=" ")
  sum = sum + i
  i = i + 1
else:
  print("\n")
  print("The sum total is = ", sum)

Output:
1 2 3 4 5 6 7 8 9 10 

The sum total is =  55

----------------  OR  ----------------

i = 1
sum = 0
while i <= 10:
  print(i, end=" ")
  sum = sum + i
  i = i + 1

print("\n")
print("The sum total is = ", sum)

Output:
1 2 3 4 5 6 7 8 9 10 

The sum total is =  55
Example : A Python program to check whether the accepted value is Armstrong.
num = int(input("Enter a number: "))
sum = 0
temp = num

while temp > 0:
    digit = temp % 10
    sum = sum + (digit ** 3)
    # sum = sum + (digit * digit * digit)
    temp = temp//10
    
if num == sum:
    print(num, "is an Armstrong number")
else:
    print(num, "is not an Armstrong number")

Output:

Enter a number: 153
153 is an Armstrong number
Example : A Python program to print all the numbers from 1 to 10 except 5 using the Continue statement.
i = 0
while i < 10:
  i = i + 1
  if i == 5:
    continue
  print(i, end=" ")

Output:
1 2 3 4 6 7 8 9 10 
Example : A Python program to print all the numbers before 5 from 1 to 10 using a Break statement.
i = 1
while i <= 10:
  if i == 5:
    break
  print(i, end=" ")
  i = i + 1

Output:
1 2 3 4 
Example : A Python program to receive User Inputs after confirmation.
while True:
    x = int(input("Enter First no. = "))
    y = int(input("Enter Second no. = "))
    z = x + y
    print("The addition result is = ",z)

    option = input("To quit, Type N or n otherwise any character = ")
    if option == 'n' or option == 'N':
       break

Output:
Enter First no. = 40
Enter Second no. = 50
The addition result is =  90
To quit, Type N or n otherwise any character = n
Example : A Python program to find out the Factorial Result of accepted Single User Inputs after confirmation.
while True:
    x = int(input("Enter a no. for factorial = "))
    fact=1
    print("The factorial result is = ")
    while x>=1:
        fact=fact*x
        x=x-1
    print(fact)
    
    option = input("To quit, Type N or n otherwise any character and press Enter = ")
    if option == 'n' or option == 'N':
       break
Example : After confirmation, a Python program finds out the Even no. from an accepted User Inputs Range.
while True:
    low = int(input("Enter a smaller no. = "))
    high = int(input("Enter a larger no. = "))
    
    print("The result is = ",end=" ")
    while low<=high:
        if low%2==0:
            print(low,end=" ")
        low=low+1
    print()    # for newline
    option = input("To quit, Type N or n otherwise any character and press Enter = ")
    if option == 'n' or option == 'N':
       break

Output:
Enter a smaller no. = 10
Enter a larger no. = 20
The result is =  10 12 14 16 18 20 
To quit, Type N or n otherwise any character and press Enter =
   
Example : A Python program to display Each Digit of accepted Single User Inputs.
while True:
    number = int(input("Enter a number: "))
    
    print("The digits of the number are : ",end="")
    while number > 0:
        digit = number % 10  # Extract the last digit
        print(digit," ",end="")
        number = number // 10  # Remove the last digit
        
    print()    # for newline    
    option = input("To quit, Type N or n otherwise any character and press Enter = ")
    if option == 'n' or option == 'N':
       break
Output:
Enter a number: 354
The digits of the number are : 4  5  3  
To quit, Type N or n otherwise any character and press Enter = 
Example : A Python program to display Table Values of accepted Single User Inputs.
while True:
    number = int(input("Enter a Table Value : "))
    x=number
    print("The values of the table is :")
    while x<=number*10:
        print(x)
        x=x+number
        
    option = input("To quit, Type N or n otherwise any character and press Enter = ")
    if option == 'n' or option == 'N':
       break

Output:
Enter a Table Value : 4
The values of the table is :
4
8
12
16
20
24
28
32
36
40
To quit, Type N or n otherwise any character and press Enter =

-------------  OR  ---------------

while True:
    number = int(input("Enter a Table Value : "))
    x=1
    print("The values of the table is :")
    while x<=10:
        k=x*number
        print(number,"*",x," = ",k)
        x=x+1
        
    option = input("To quit, Type N or n otherwise any character and press Enter = ")
    if option == 'n' or option == 'N':
       break

Output:
Enter a Table Value : 5
The values of the table is :
5 * 1  =  5
5 * 2  =  10
5 * 3  =  15
5 * 4  =  20
5 * 5  =  25
5 * 6  =  30
5 * 7  =  35
5 * 8  =  40
5 * 9  =  45
5 * 10  =  50
To quit, Type N or n otherwise any character and press Enter = 
Example : A Python program to Check Prime No. of accepted Single User Inputs.
while True:
    number = int(input("Enter a number: "))
    x=1
    count=0
    while x <= number:
        if number%x==0:
            count=count+1
        x=x+1
    if count==2:
        print("No. is Prime")
    else:
        print("No. is Not Prime")
    print()    # for newline
    
    option = input("To quit, Type N or n otherwise any character and press Enter = ")
    if option == 'n' or option == 'N':
       break

Output:
Enter a number: 13
No. is Prime

To quit, Type N or n otherwise any character and press Enter = 

For Loop Examples

Example : A Python program to print all the values using For loop.
values = [5, 25, 30, 50, 65]
for x in values:
  print(x, end=" ")

Output:
5 25 30 50 65 

--------------  OR  ---------------

fruits = ["guava", "orange", "apple", "banana", "mango"]
for i in fruits:
  print(i, end=" ")

Output:
guava orange apple banana mango
Example : A Python program to find out the Factorial of user-accepted value using a For loop.
num = int(input("Enter a number for factorial : "))

fact = 1
for i in range(1, num + 1):
    fact = fact * i
print("Factorial of", num, "is:", fact)

Output:

Enter a number for factorial : 5
Factorial of 5 is: 120
Example : A Python program to find out the Sum Total of user-accepted values using a For loop.
num = int(input("Enter a number for sum : "))

sum = 0
for i in range(1, num + 1):
    sum = sum + i
print("The sum of all the values from 1 to", num, "are = ", sum)

Output:

Enter a number for sum : 10
The sum of all the values from 1 to 10 are =  55

-------------  OR  -------------

num1 = int(input("Enter first number for sum : "))
num2 = int(input("Enter second number for sum : "))

sum = 0
for i in range(num1, num2 + 1):
    sum = sum + i
print("The sum of all the values from", num1, "to", num2, "are = ", sum)

Output:
Enter first number for sum : 10
Enter second number for sum : 12
The sum of all the values from 10 to 12 are =  33
Example : A Python program to print all the Fibonacci Series values up to given terms using a For loop.
number = int(input("Enter the number of terms to display series : "))
a = 0
b= 1
#print (a, end=" ")
#print (b, end=" ")

print (a,b, end=" ")

for i in range(1, number-1):
    c=a+b
    print(c, end=" ")
    a=b
    b=c

Output:

Enter the number of terms to display series : 10
0 1 1 2 3 5 8 13 21 34

-----------  OR  -----------

number = int(input("Enter the number upto/below which you want to display series : "))
a = 0
b= 1
#print (a, end=" ")
#print (b, end=" ")

print (a,b, end=" ")

for i in range(1, number-1):
    c=a+b
    if c>number:
        break
    else:    
        print(c, end=" ")
        a=b
        b=c

Output:

Enter the number upto which you want to display series : 50
0 1 1 2 3 5 8 13 21 34
Example : A Python program prints all the values along with its total using a For loop.
sum = 0;
values = [5, 25, 30, 50, 65]
for x in values:
  sum = sum + x
  print(x, end=" ")
else:
  print("\n")
  print(sum, end=" ")

Output:
5 25 30 50 65 

175
 
-------------  OR -------------

sum = 0;
values = [5, 25, 30, 50, 65]
for x in values:
  sum = sum + x
  print(x, end=" ")

print("\n")
print(sum, end=" ")

Output:
5 25 30 50 65 

175
Example : A Python program to check whether the values are Prime or Not using a For loop.
num = int(input("Enter a number: "))

count=0

if num > 1:
    for i in range(2, num + 1):
        if num % i == 0:
            count=count+1
    
    if count == 1:
        print(num,"is a Prime Number")
    else:
        print(num, "is not a Prime Number")
else:
    print(num, "is not a prime number")

Output:

Enter a number: 20
20 is not a Prime Number
Example : A Python program to print some values with a Break statement using a For loop.
sum = 0;
values = [5, 25, 30, 50, 65]
for x in values:
  if x == 50:
    break
  print(x, end=" ")

Output:
5 25 30                                     
Example : A Python program to print some values with a Continue statement using a For loop.
sum = 0;
values = [5, 25, 30, 50, 65, 200]
for x in values:
  if x == 50:
    continue
  print(x, end=" ")

Output:
5 25 30 65 200 
Example : A Python program to display Each Digit of accepted Single User Inputs.
while True:
    number = input("Enter a number: ")
    
    print("The digits of the number are:")
    for digit in number:
        print(digit)
        
    option = input("To quit, Type N or n otherwise any character and press Enter = ")
    if option == 'n' or option == 'N':
       break

Output:
Enter a number: 854
The digits of the number are:
8
5
4
To quit, Type N or n otherwise any character and press Enter = 
Example : A Python program to print the number using range() in For loop.
for i in range(10):
    print(i, end = ' ')

Output:
0 1 2 3 4 5 6 7 8 9

-------------  OR -------------

for i in range(10):
    print(i)
Output:
0
1
2
3
4
5
6
7
8
9
-------------  OR -------------

for x in range(1,11):
    print(x, end=" ") 

Output:
1 2 3 4 5 6 7 8 9 10

-------------  OR -------------

for i in range(3, 10):
    print(i, end = ' ')

Output:
3 4 5 6 7 8 9

-------------  OR -------------

for x in range(5, 50, 4):
  print(x, end=" ") 

Output:
5 9 13 17 21 25 29 33 37 41 45 49
 
-------------  OR -------------

for i in range(15, 3, -1):
    print(i, end = ' ')

Output:
15 14 13 12 11 10 9 8 7 6 5 4 

-------------  OR -------------

for i in range(15, 3, -4):
    print(i, end = ' ')

Output:15 11 7
 
-------------  OR -------------

for str in 'Welcome':
    if str == 'l' or str == 'e' :
        continue
    print(str, end='')

Output:
  Wcom

-------------  OR -------------

for str in 'Hello India':
    pass
print(str, end='')

Output:
a

NB: 'pass' statements skip all letters except last letter.
Example : A Python program to print the specific pattern using range() in Nested For loop.
for i in range(1, 5):
    for j in range(1,5):
        print("*", end=' ')
    print()

Output:
* * * * 
* * * * 
* * * * 
* * * * 

-------------  OR -------------

x=1
for i in range(1, 5):
    for j in range(1,5):
        print(x, end=' ')
    x=x+1
    print()

Output:
1 1 1 1 
2 2 2 2 
3 3 3 3 
4 4 4 4

-------------  OR -------------

x=1
for i in range(1, 5):
    for j in range(1,5):
        print(x, end=' ')
        x=x+1
    print()

Output:
1 2 3 4 
5 6 7 8 
9 10 11 12 
13 14 15 16 

-------------  OR -------------

x=16
for i in range(1, 5):
    for j in range(1,5):
        print(x, end=' ')
        x=x-1
    print()

Output:
16 15 14 13 
12 11 10 9 
8 7 6 5 
4 3 2 1

-------------  OR -------------

for i in range(1, 5):
    for j in range(i):
        print(i, end=' ')
    print()

           OR

k=1
for i in range(1, 5):
    for j in range(i):
        print(k, end=' ')
    k=k+1
    print()

Output:
1 
2 2 
3 3 3 
4 4 4 4

-------------  OR -------------

k=1
for i in range(1, 5):
    for j in range(i):
        print(k, end=' ')
        k=k+1
    print()

Output:
1 
2 3 
4 5 6 
7 8 9 10

-------------  OR -------------

k=1;m=5
for i in range(1, 5):
    for j in range(1,m):
        print(k, end=' ')
        k=k+1
    m=m-1
    print()

Output:
1 2 3 4 
5 6 7 
8 9 
10 

Some Important links:

Loading

Categories: Python Theory

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.