Python While Loop Example

By: On:

In this Python tutorial, we will how to use while loop in python with a few examples. Also, we will discuss:

  • Python while loop
  • Infinite while loop in python
  • Break statement in python
  • Continue statement in python
  • Python while loop multiple conditions
  • Python while loop with else statement

While loop in python

In python, while loop is used iterate over the code until the given condition gets false.


Example:

value = 1
while value < 8:
print(value)
value = value + 1

After writing the above code (while loop in python), Ones you will print ” value ” then the output will appear as a “ 1 2 3 4 5 6 7 ”.

Here, the while loop is used to iterate over a block of code until it gets false. You can refer to the screenshot while loops in python.

While loop in python

Infinite while loop in python

In python, Infinite while loop is the loop where the while condition never gets false and the loop will never end.


Example:

while True:
print("Welcome to loops")

After writing the above code (infinite while loop in python), Ones you will print ” Welcome to loops ” then the output will appear as a “ Welcome to loops ” and it will continue till infinity.

Here, the while loop is used to print indefinitely because the condition will remain true. You can refer to the screenshot Infinite while loop in python.

Infinite while loop in python

Break statement in python

In python, break statement is used to stop the while loop even if the condition is true.

Example:

value = 2
while value < 8:
print(value)
if value == 5:
break
value = value + 1

After writing the above code (break statement in python), Ones you will print ” value ” then the output will appear as a “ 2 3 4 5 ”.

Here, even if my while condition is true we can stop the condition by using the break statement. You can refer to the screenshot break statement in python.

Break statement in python

Continue statement in python

In python, the continue statement is used to stop the current iteration, and then it will continue with the next till the last.

Example:

value = 1
while value < 7:
value = value + 1
if value == 4:
continue
print(value)

After writing the above code (continue statement in python), Ones you will print ” value ” then the output will appear as a “ 2 3 5 6 7 ”.

Here, we will use a continue statement to stop the current iteration and it will continue with the next. You can refer to the below screenshots continue statement in python.

Continue statement in python

Python while loop multiple conditions

In python, the while loop multiple conditions are used when two simple boolean conditions are joined by the logical operator ” and “.

Example:

value1 = 10
value2 = 20
while value1 > 0 and value2 > 0
print((value1, value2))
value1 = value1 - 3
value2 = value2 - 5

After writing the above code (python while loop multiple conditions), Ones you will print ” value 1, value2 ” then the output will appear as a “ (10, 20) (7, 15) (4, 10) (1, 5) ”.

Here, we will use the logical operator ” and “ to join the condition. You can refer to the below screenshots python while loop multiple conditions.

Python while loop multiple conditions

Python while loop with else statement

In python, the else part is associated with while loop statements. The else part is executed when the condition becomes false.

Example:

count = 0
while count < 4:
print("My count is less than 4")
count = count + 1
else:
print("My count is not less than 4")

After writing the above code (python while loop with else statement), Ones you will print then the output will appear as a “ My count is less than 4” which will be get printed 4 times till the condition is true and when the condition gets false then it goes to else part and prints ” My count is not less than 4“.

Here, when the while loop condition evaluates to false then else part is executed. You can refer to the below screenshots python while loop with else statement.

Python while loop with else statement

In this way, we can use while Loops in python.


You may like following Python tutorials:

In this tutorial, we learned how to use while Loops in python with examples:

  • While loop in python
  • Infinite while loop in python
  • Break statement in python
  • Continue statement in python
  • Python while loop multiple conditions
  • Python while loop with else statement

Leave a Comment