Python: while loop – Explained with examples
In this article, we will learn the syntax and usage of a while loop in python.
Need of a while loop?
In python, if we write some statements like this,
print('First line') print('Sample text') print('Last line')
The interpreter will execute these statements in sequential order, but only once from top to bottom. What if we want to run certain statements multiple times? We can do that using while loops.
A while loop in python executes a group of statements or a suite of statements multiple times, till a given condition is True.
Syntax of while loop
while condition: statement 1 statement 2 ....... ....... statement n
A while statement starts with a while keyword followed by a condition and a colon in the end. After the while statement, the block of the while loop starts. It includes a group of statements with one indent level. These statements in a block are also called a suite of statements in python.
How while loop works?
When the python interpreter encounters a while statement, then it evaluates the condition after the while keyword. If the condition in while statement evaluates to True, then it executes the suite of statements in the while block and then comes back to the while statement to recheck the condition. If the condition again evaluates to True, then it will also execute the suite of statements in the while block. This cyclic process will go on and on until the condition in the while statement evaluates to False.
So, using a while loop, we can control the flow of code and can execute certain statements multiple times until a condition evaluates to False.
Let’s check out some examples of while loop,
Python while loop examples
Use while loop to print numbers from 1 to 10
# Use while loop to print numbers from 1 to 10 x = 1 while x <= 10: print(x) x = x + 1
1 2 3 4 5 6 7 8 9 10
In this example, we define a variable x and initialize it to 1. Then we started a while loop with a condition x < 10. Now until this condition evaluates to False, the interpreter must execute the code in while loop block.
Inside the while loop block, we printed the value of x and then incremented it by 1. After executing the statements in the suite, control goes back to the starting of while loop to recheck the condition, and the whole process repeats till x becomes 11, and the condition evaluates to False.
Python while loop with multiple conditions
We can have various conditions in a while statement, and we can use ‘and’ & ‘or’ with these conditions. For example,
x = 5 count = 0 while x <= 100 and count < 10: if x % 2 == 0: count += 1 print(x) x += 1
6 8 10 12 14 16 18 20 22 24
Here we checked two conditions in a while statement. While loop will keep on executing the statements in-suite until x is less than 100, and the count is less than 10. It will print the only first five even numbers from 10 to 100. Variable x is to iterate from index 10 to 100, and the variable count is to keep the count of printed even numbers. As soon as x becomes greater than 100 or count becomes greater than 10, the loop ends.
While loop with else
Similar to if…else, we can have while…else in python, i.e., we can have an else block after a while block i.e.
while condition: statement 1 statement 2 statement 3 else: statement 4 statement 5
While loop will execute statements in the white suite multiple times till the condition evaluates to False. As soon as the condition in while statement evaluates to False, control jumps to the else block and executes all the statements in else suite.
x = 1 while x <= 10: print(x) x = x + 1 else: print('printed values from 1 to 10')
1 2 3 4 5 6 7 8 9 10 printed values from 1 to 10
Here we printed ten numbers from 1 to 10 using a while loop. As soon as the condition in while statement evaluates to False, it executes the code in the else block.
We can use while loop in python to iterate over specific statement multiple times.