Latest Code Tutorials

Python While Loop: How to Use While Loop in Python

Loops are used in Python programming to repeat the specific block of code. In this post, you will learn to create the while loop in Python. 

Python While Loop

A while loop in Python is used to iterate over the block of code as long as the test condition) is true.

Python While Loop is used to execute the set of statements as long as the condition is true. The while loop tells a computer to do something as long as the condition is met or holds true. Its construct consists of the block of code and the condition. The condition is evaluated, and if the condition is true, the code within the block is executed. It repeats until the condition becomes false.

We generally use a while loop when we don’t know, the number of times to iterate.

See the following syntax of a while loop.


See the following syntax of a while loop.

while test_expression:
    { body of while }

In the while loop, the test expression is checked first. The body of a loop is entered only if a test_expression evaluates to True.

After one iteration, the test expression is rechecked. This process continues until the text_expression evaluates to False. Remember, we need to change the value somehow otherwise it will go to an infinite loop.

In Python, a body of a while loop is determined by indentation. The body starts with an indentation, and the first unindented line marks the end. Python interprets any non-zero value to TrueNone and 0 are interpreted as False.

See the following example of a while loop in Python.


i = 21
while i < 29:
  i += 1


Python While Loop Example

In the above program, the test expression will hold as long as our variable i is less than or equal to 29 in our program starting from 21.

We need to increase the value of i variable in the body of the loop. It is essential and mostly forgotten. If we failing to do so will result in an infinite loop or never-ending loop. Finally, the result is displayed.

Python While loop with else

Let’s take an example of while loop with else in Python. 

If you have used Python For Loop then in that we have else blocked the same here as we can have an optional else block with the while loop as well.

The else part is executed if the condition in a while loop evaluates to False.

A while loop can be stopped with the break statement. In such a case, the else part is ignored. Hence, the while loop’s else part runs if no break occurs and the condition is false.

Here is an example.


counter = 0

while counter < 3:
    counter = counter + 1
    print('Executed Else Statement')

In the above code, unless the condition is false, the counter is printed. After the condition is false, the else block is executed. See the output below.

while loop with else

The break Statement in Python

With the break statement, we can stop the loop even if the while condition is true.


i = 1
while i < 5:
  if i == 3:
  i += 1

See the output below.

The break Statement in Python

Finally, Python While Loop Example is over.

Leave A Reply

Your email address will not be published.

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