Python while Loop

In this tutorial, we will learn about the while loop in Python programming with the help of examples.

Video: Python while Loop

In programming, loops are used to repeat a block of code. For example, if we want to show a message 100 times, then we can use a loop. It's just a simple example, we can achieve much more with loops.

In the previous tutorial, we learned about Python for loop. Here, we are going to learn about while loops.

Python while Loop

Python while loop is used to run a specific code until a certain condition is met.

The syntax of while loop is:

while condition:
    # body of while loop


  1. A while loop evaluates the condition
  2. If the condition evaluates to True, the code inside the while loop is executed.
  3. condition is evaluated again.
  4. This process continues until the condition is False.
  5. When condition evaluates to False, the loop stops.

Flowchart for Python While Loop

while Loop in Python programming
Flowchart for while Loop in Python

Example: Python while Loop

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    i = i + 1



Here's how the program works:

Variable Condition: i <= n Action
i = 1
n = 5
True 1 is printed. i is increased to 2.
i = 2
n = 5
True 2 is printed. i is increased to 3.
i = 3
n = 5
True 3 is printed. i is increased to 4.
i = 4
n = 5
True 4 is printed. i is increased to 5.
i = 5
n = 5
True 5 is printed. i is increased to 6.
i = 6
n = 5
False The loop is terminated.

Example 2: Python while Loop to Display Game Level

current_level = 0
final_level = 5

game_completed = True

while current_level <= final_level:
    if game_completed: 
        print('You have passed level', current_level)
        current_level += 1

print('Level Ends')


You have passed level 0
You have passed level 1
You have passed level 2
You have passed level 3
You have passed level 4
You have passed level 5
Level Ends

In the above example, we have used the while loop to check the current level and display it on the console.

Infinite while Loop in Python

If the condition of a loop is always True, the loop runs for infinite times (until the memory is full). For example,

# infinite while loop
while True:
    # body of the loop

In the above example, the condition is always True. Hence, the loop body will run for infinite times.

Python While loop with else

A while loop can have an optional else block as well.

The else part is executed after the condition in the while loop evaluates to False. For example,

counter = 0

while counter < 3:
    print('Inside loop')
    counter = counter + 1
    print('Inside else')


Inside loop
Inside loop
Inside loop
Inside else

Here, we have used the counter variable to print the 'Inside Loop' string three times.

On the fourth iteration, the condition in while becomes False. Hence, the else part is executed.

Note: The else block will not execute if the while loop is stopped by a break statement.

Python for vs while loops

The for loop is usually used when the number of iterations is known. For example,

# this loop is iterated 4 times (0 to 3)
for i in range(4):

And while loop is usually used when the number of iterations are unknown. For example,

while condition:
    # body of loop
Did you find this article helpful?