while Loop in Python Programming
Loops in Python
In this lesson, we will understand what is while loop in Python programming along with some examples.
What is while Loop
while loop is used in situations where we do not know the exact number of iterations of loop beforehand. while loops continue to loop as long as a test expression is true.
Syntax of while Loop
initialization_expression
while test_expression:
# body of the loop
# statements we want to execute
update_expression
Example
# initialization expression
i = 1
# test expression
while i<=5:
print('Hello World')
# update expression
i=i+1
Output
Hello World Hello World Hello World Hello World Hello World
We have to initialize the loop variable before starting the while loop. After initialization, we will write our test expression of while loop with a colon at the end of it. If the test expression evaluates to true, then the body of the while loop will execute and, the update expression will update the loop value. This process continues until the test expression evaluates to false. When the test expression evaluates to false the while loop terminates.
Example 1
Python program to print the numbers from 1 to 10 on the screen using while loop.
i=1
while i<=10:
print('%d' %(i))
i=i+1
Output
1 2 3 4 5 6 7 8 9 10
In the above example, we have run a while loop from 1 to 10. Each time when the loop runs, we print the value of the variable i on the screen on a separate line. The loop ends when the value of i is more than 10.
Example 2
Python program to print all the even numbers from 10 to 20 on the screen using while loop.
i=10
while i<=20:
print('%d' %(i))
i=i+2
Output
10 12 14 16 18 20
In the above example, we have run a while loop from 10 to 20. Each time when the loop runs, we print the value of the variable i on the screen on a separate line and then increment the value of i by 2. The loop ends when the value of i is more than 20.
Example 3
Python program to print all the numbers from 10 to 1 in reverse order on the screen using while loop.
i=10
while i>=1:
print('%d' %(i))
i=i-1
Output
10 9 8 7 6 5 4 3 2 1
In the above example, we have run a while loop from 10 to 1 in reverse order. Each time when the loop runs, we print the value of the variable i on the screen on a separate line and then decrement the value of i by 1. The loop ends when the value of i is less than 1.
Example 4
Python program to input a 4-digit number and find the sum of its digits using while loop.
sd=0
n=int(input('Enter a 4 digit number '))
if n>=1000 and n<=9999:
while n>0:
r=n%10 # return the last digit of the number as remainder
sd=sd+r
n=n//10 # removes the last digit of the number using floor division
print('Sum of digits = %d' %(sd))
else:
print('Not a 4 digit number')
Output
Enter a 4 digit number 2461 Sum of digits = 13
In the above example, we have input a 4-digit number. The loop starts with the 4-digit number and runs till the value of n is greater than 0. Each time when the loop runs, we find the last digit of the number using n%10 and add the digit in the variable sd and remove the last digit from the number using n=n//10. The loop ends when the value of n becomes 0 after removing all the digits from n.
Nested while Loop
Python programming allows using one while loop inside another while loop. This is known as nested while loop.
Syntax of Nested while Loop
initialization_expression
while test_expression:
# body of the outer loop
# statements we want to execute
initialization_expression
while test_expression:
# body of the inner loop
# statements we want to execute
update_expression;
update_expression
Example
i=1
while i<=5:
j=1
while j<=i:
print('%d' %(j),end='')
j=j+1
print()
i=i+1
Output
1 12 123 1234 12345
In the above example, we have run two loops, one outer loop and another inner loop. The outer loop runs from 1 to 5, and the inner loop runs from 1 to the current value of the outer loop (for example, when the value of i is 1, the inner loop runs from 1 to 1. When the value of i is 2, the inner loop runs from 1 to 2 and so on). The program ends when the outer loop test expression evaluates to false.
Infinite while Loop
An infinite while loop is a loop that never ends because its test condition never evaluates to false. It keeps on executing unless and until we terminate the loop using the break statement as shown in the example below.
Syntax of Infinite while Loop in Python
while 1:
{
# body of the infinite while loop
}
Example
i=0
while 1:
print('Hello World')
i=i+1
if i==5:
break # terminate the infinite loop when the value of i is 5
Output
Hello World Hello World Hello World Hello World Hello World
Test Your Knowledge
Attempt the practical questions to check if the lesson is adequately clear to you.