# Common examples of while loop in Python

May 19, 2017 8115

This article is an extension of the ‘While’ chapter of Python. If you need to learn basics then visit the Python course first. You can also practice a good number of questions from practice section.

## Factorial of a number

You can always use Python’s ‘factorial’ function to calculate the factorial of a number. This example just demonstrates the calculation of the factorial using a while loop. You can visit this to see the same example using recursion.

print ("Enter a number to calculate the factorial of a number")
number = int(input())
fact = 1
while number>0:
fact = fact*number
number = number-1
print ("Factorial is",fact)


This example follows the following steps:

1. A variable ‘fact’ is assigned a value of 1.
2. The value of the variable ‘fact’ is changed to the value of “‘fact’ * number” (fact = fact*number)
3. The ‘number’ is changed to 1 less than its previous value.
4. Steps 2 and 3 are repeated until the value of the ‘number’ becomes positive.

Let’s calculate the factorial of 4 using this algorithm.

Firstly, the ‘fact’ is given a value of 1 and then in the first iteration of the loop, it is changed to fact*number (i.e., 1*4) and 4 is changed to 3.

In the second iteration, the value of ‘fact’ is again changed to faact*number (i.e., 4*3) and the ‘number’ is changed to 2.

In the third iteration, the value of ‘fact’ becomes 4*3*2 (initial value of fact was 4*3 and the value of ‘number’ is 2) and 2 is changed to 1.

In the fourth and last iteration, ‘ fact’ becomes 4*3*2*1 and the value of the ‘number’ is changed to 0 and so the loop stops.

## Prime numbers

This is a simple example of checking whether a number is prime or not. Prime number using Sieve algorithm is demonstrated in this article.

import math

print ("Enter the a number")
number = int(input())

i = 2
prime = True

#if the number is not divisible by any number less than the square root of the number
#then it is prime
while i <= int(math.sqrt(number)):
if number%i == 0:
prime = False
break
i = i+1

if number < 2:
prime = False

if prime:
print (number,"is a prime number")
else:
print (number,"is not a prime number")


The code is straight forward and very simple to understand.

Initially, we are making the value of the variable ‘prime’ ‘True’ and then we will check if the number is divisible by any number or not and if it is then we will make it ‘False’.

A number is a prime number if it hasn’t any factor between 2 and the square root of itself and this is exactly what we are checking. Our loop goes from 2 to square root of the number and if any factor is found then the value of ‘prime’ is changed to ‘False’ and the loop is stopped.

if number < 2 → prime = False – We may also get a number which is less than 2 and for this case, we are checking explicitly and making the value of ‘prime’ ‘False’.

## Palindrome

A Palindrome reads same from beginning and end. For e.g., 121, 1221, etc. You can also do this by first changing the number into a string and then reverse it and finally compare both the strings but this is the example using while loop.

print ("Enter the a number")
number = int(input())

temp = number
reverse_num = 0

while temp != 0:
reverse_num = (reverse_num*10)+(temp%10)
temp = temp//10

if reverse_num == number:
print ("Yes! a palindrome")
else:
print ("NO")


In this example, we are just making a new number which is in the reverse order of the previous one and finally comparing the both and to do so we are first multiplying the ‘reverse_num’ with 10 and adding it to the remainder of the division of ‘temp’ with 10. Suppose, the value of ‘temp’ is 45 then (reverse_num*10)+(temp%10) will give us 5 and then temp = temp//10 will make the value of ‘temp’ equal to 4. In the next iteration, the value of ‘reverse_num’ will become 54 ((5*10)+(4%10)) and the value of ‘temp’ will become 0.

## Armstrong Numbers

A number ‘ n’ is called Armstrong number if the sum of each of its digits raised to the power of the number of digits is equal to the number itself. For example, 153 is an Armstrong number because 153 = (1*1*1)+(5*5*5)+(3*3*3).

The example given below checks if a given number is an Armstrong number or not.

import math

print ("Enter the a number")
number = int(input())

#to calculate the number of digits in a number
number_of_digits = int(math.log10(number))+1

sum_arm = 0
temp = number
while temp != 0:
sum_arm = sum_arm + int(math.pow(temp%10,number_of_digits))
temp = temp//10
if sum_arm == number:
print ("Yes an Armstrong number")
else:
print ("No")


number_of_digits = int(math.log10(number))+1 – We are just calculating the number of digits here.

Let’s take a case of 153.

math.pow(temp%10,number_of_digits) will calculate (3*3*3) and temp = temp//10 will change 153 to 15. In the next iteration, the value of ‘sum_arm’ will become (3*3*3)+(5*5*5) and temp = temp//10 will change 15 to 1. And in the last iteration the value of ‘sum_arm’ will become (3*3*3) + (5*5*5) + (1*1*1).

Liked the post?
Developer and founder of CodesDope.
Editor's Picks
0 COMMENT