Skip to content

while condition Python

  • by

Python while the condition is a loop and it is used to iterate over a block of code as long as the test expression (condition) is true.

  #Loop body goes here

The condition_statement is a single condition or a set of multiple conditions that return a boolean value of true or false. The code inside the loop body is not executed, If condition statement is false from the start and the program moves on to the code outside the while block.

while condition Python

Simple example code.

num = 1

while num <= 5:
    num = num + 1



Can you have 2 conditions in a while loop?

Answer: You can use multiple conditions in while loop. See below example where the while loop has two conditions, one using the AND operator and the other using the OR operator.

iterator = 1
second_iterator = 10
third_iterator = 1

while third_iterator <= 4 and iterator <= 10 or second_iterator < 5:
    print(iterator, second_iterator, third_iterator)
    iterator = iterator + 1
    second_iterator = second_iterator + 1
    third_iterator = third_iterator + 1

print("Outside while loop")


while condition Python

Do comment if you have any doubts or suggestions on this Python while loop topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *