Tutorials Tonight
Follow Us

Python While Loop Multiple Conditions


In this article, you will learn how to use multiple conditions in a while loop in Python.

    Table Of Contents

  1. While Loop In Python
  2. Multiple Conditions In While Loop Using And
  3. Multiple Conditions In While Loop Using Or
  4. Multiple Conditions In While Loop Using Not
  5. Grouping Multiple Conditions In While Loop
  6. Conclusion

While Loop In Python

Python while loop is used to execute a block of code repeatedly until a condition is satisfied. It is used when we don't know the number of times to iterate beforehand.

While loop executes the code block until the condition is true. If the condition is false, the code block will not be executed.

Let's see an example of a while loop in Python.

count = 0
while count < 5:
    print(count, end=" ")
    count += 1

Output:

0 1 2 3 4

Here, the condition is count < 5. The condition expression is evaluated first. If the condition is true, the code block is executed. After executing the code block, the condition is evaluated again. If the condition is true, the code block is executed again. This process continues until the condition is false.

Here in the above example, we have used a single condition. The whole execution process of the loop depends on this. But we can also use multiple conditions in the while loop.


Multiple Conditions In Python While Loop

While writing while loop we want our loop to depend on more than one condition. For example, printing the sum of 2 variables between their given range.

Let's see how we can use multiple conditions in the while loop in Python.

Multiple Conditions In While Loop Using And

Above we discussed printing the sum of 2 variables between their given range. Let's see how we can do that using a while loop.

2 or more than 2 conditions are separated by a logical operator. Like and, or, not. Here we will use and operator.

Let's see an example of multiple conditions in a while loop using and operator.

num1 = 1
num2 = 10

# num1 is less than 6 and num2 is greater than 5
while num1 < 6 and num2 > 5:
    print(str(num1) + " + " + str(num2) + " = " + str(num1 + num2))
    num1 += 1
    num2 -= 1

Output:

1 + 10 = 11
2 + 9 = 11
3 + 8 = 11
4 + 7 = 11
5 + 6 = 11

Here, we have 2 conditions. The loop will execute until both the conditions are true. If any one of the conditions is false, the loop will not execute.


Multiple Conditions In While Loop Using Or

Let's see another example of multiple conditions in while loop using or operator.

num1 = 1
num2 = 10

while num1 < 7 or num2 > 4:
    print("num1 is " + str(num1) + " and num2 is " + str(num2))
    num1 += 1
    num2 -= 1

Output:

num1 is 1 and num2 is 10
num1 is 2 and num2 is 9
num1 is 3 and num2 is 8
num1 is 4 and num2 is 7
num1 is 5 and num2 is 6
num1 is 6 and num2 is 5

Multiple Conditions In While Loop Using Not

Let's see another example of multiple conditions in a while loop using not operator.

num1 = 1
num2 = 10

while not num1 > 6 and not num2 < 5:
    print("num1 is " + str(num1) + " and num2 is " + str(num2))
    num1 += 1
    num2 -= 1

Output:

num1 is 1 and num2 is 10
num1 is 2 and num2 is 9
num1 is 3 and num2 is 8
num1 is 4 and num2 is 7
num1 is 5 and num2 is 6
num1 is 6 and num2 is 5

Grouping Multiple Conditions In While Loop

We can also group multiple conditions in a while loop using parenthesis.

Let's see an example of grouping multiple conditions in a while loop.

num1 = 1
num2 = 10
num3 = 5

while (num1 < 6 and num2 > 5) and num3 < 7:
    print("num1 is " + str(num1) + " and num2 is " + str(num2) + " and num3 is " + str(num3))
    num1 += 1
    num2 -= 1
    num3 += 1

Output:

num1 is 1 and num2 is 10 and num3 is 5
num1 is 2 and num2 is 9 and num3 is 6

Conclusion

So, this is all about multiple conditions in the while loop in Python. We have discussed multiple conditions in while loop using and, or and not operator. We have also discussed grouping multiple conditions in a while loop using parenthesis.