Python While Loop Example

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
  • Pass vs continue in python
  • Reverse using while loop python
  • Python while else

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

Pass vs continue in python

PassContinue
Pass statement simply does nothingWhere continue statement skip all the remaining statements in the loop and move controls back to the top of the loop.
Pass is used when you need some empty functions, classes, or loops for future implementationContinue is used when some condition has met within a loop and you need to skip the current iteration and move to next.
Example:
str = ” Guides”
for i in str:
if i == ‘d’
print(‘Pass executed’)
pass
print(i)
print()
Example:
str = ” Guides”
for i in str:
if i == ‘d’
print(‘Continue executed’)
continue
print(i)

Reverse using while loop python

Python while loop is used to repeatedly execute some statements until the condition is true. To get the element in the reverse order we will use while loop which will iterate till the first element and keep on decrementing “r”.

Example:

my_list = ['Welcome', 'to', 'Python', 'Guides']
r = len(my_list) - 1
while r >= 0 :
    print(my_list[r])
    r = r-1

After writing the above code (reverse using while loop python), Ones you will print ”my_list[r]” then the output will appear as “ Guides Python to Welcome ”. Here, while loop is used to iterate over the list and to access an element in reverse i.e. from (size-1) to 0.

You can refer to the below screenshots reverse using while loop python

Reverse while loop python
Reverse while loop python

Python while else

Python allows an optional else clause at the end of the while loop. The additional statement specified in the else clause will be executed when the while loop terminates.

Example:

num = 3
while num > 0:
    num = num - 1
    print(num)
else:
    print('Loop end')

After writing the above code (python while else), Ones you will print ” num ” then the output will appear as “ 2 1 0 Loop end ”. Here, while loop is used to iterate until the condition is exhausted and the else clause will be executed when while loop terminates.

You can refer to the below screenshots python while else

Python while else
Python while else

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
  • Pass vs continue in python
  • Reverse using while loop python
  • Python while else