While Loop In Python (with Examples)

while loop In python

Hello Techies,

Today’s tutorial is based on while loop in Python. In this tutorial, we will learn How do you use a while loop in Python? With the help of examples.

What is a while loop in Python?

The while loop keeps repeating an action until an associated return false.

We usually use this loop when we don’t know how many times to iterate beforehand.

Syntax of while Loop in Python

while (condition):
    statements

In the while loop, the Python Interpreter checks condition, If the condition is True, then executes statements written after the colon(:). This process continues as long as the condition is true.

Flowchart of while Loop

This flowchart will give you an idea of how While Loop works.

flowchart of while loop

Example of Python while Loop

>>> x = 1
>>> while x<=10:
...     print(x)
...     x+=1
...
1
2
3
4
5
6
7
8
9
10

While loop with else

Same as for loop, while loop can also have an optional else block.

It tests the condition frequently and executes Statement 1 if it is True; If the condition is False the statement 2 of the else clause, will be executed and the loop will end. The else suite will always be run regardless of whether the statement in the loop has been implemented.

Syntax of While loop with else:

while (condition):
    statement1
else:
    statement2

Flowchart of While loop with else:

This flowchart will give you an idea of how While Loop with else works.

flowchart of while loop with else

Here is an example of While loop with else:

>>> x = 1
>>> while x<=5:
...     print(x)
...     x+=1
... else:
...     print("Else condition executed")
...
1
2
3
4
5
Else condition executed

Infinite while loop

If a condition is never FALSE, the loop becomes an infinite loop.

Syntax of Infinite While loop:

while (True):
    statements

Consider the below Example:

>>> while True:
...     print("Python")
...
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Traceback (most recent call last):
    File "<stdin>", line 2, in <module>
KeyboardInterrupt

This code is terminated by pressing Ctrl + C on the keyboard. Otherwise, this code would have run continuously and as a result, your system will crash.

So, True will never be False and this loop will theoretically execute forever. “forever” in this context means until you close it, but that doesn’t sound like something you’d want to do.

So let’s check how to use this loop properly:

Syntax of Infinite while loop:

while (True):
    statements
    if (condition):
        break

Example of Infinite while loop:

>>> x = 0
>>> while True:
...     x+=1
...     print(x)
...     if (x == 5):
...             break
...
1
2
3
4
5

Infinite loops can be very useful. Just remember that you have to make sure that the loop will be broken at some point so that it doesn’t become infinite.


Nested While Loop

A while loop can be contained within another while loop, is known as “Nested While Loop”.

Syntax of Nested while loop

while (True):
    statements
    while (True):
        statements
    statements

Example of Nested while loop

>>> x = 1
>>> while x<=5:
...     print("Outer Loop", x)
...     x+=1
...     y=1
...     while y<=7:
...             print("Inner Loop", y)
...             y+=1
...
Outer Loop 1
Inner Loop 1
Inner Loop 2
Inner Loop 3
Inner Loop 4
Inner Loop 5
Inner Loop 6
Inner Loop 7
Outer Loop 2
Inner Loop 1
Inner Loop 2
Inner Loop 3
Inner Loop 4
Inner Loop 5
Inner Loop 6
Inner Loop 7
Outer Loop 3
Inner Loop 1
Inner Loop 2
Inner Loop 3
Inner Loop 4
Inner Loop 5
Inner Loop 6
Inner Loop 7
Outer Loop 4
Inner Loop 1
Inner Loop 2
Inner Loop 3
Inner Loop 4
Inner Loop 5
Inner Loop 6
Inner Loop 7
Outer Loop 5
Inner Loop 1
Inner Loop 2
Inner Loop 3
Inner Loop 4
Inner Loop 5
Inner Loop 6
Inner Loop 7

One-line while loop in Python

The while loop can be specified on one line. If the block contains multiple statements forming a loop body, they can be separated by a semicolon (;).

Example of One-line while loop:

>>> x = 3
>>> while x > 0: x -= 1; print(x)
...
2
1
0

Interview Question

The interviewer will ask you to write a program based on a while loop 
or they will give you a program and ask you the output of that program.

Conclusion

In this tutorial, you learned about Python while loop, We have covered the following points:

  • Python while loop with syntax and example.
  • While loop with else
  • Infinite while loop in Python
  • Nested While Loop in Python
  • One-line while loop in Python
thank you

Leave a Comment