Python while loop multiple conditions

In this Python tutorial, we will discuss the Python while loop multiple conditions.

Python while loop multiple conditions

  • Let us see how to use multiple conditions using while loop in Python.
  • A while loop is used to iterate a sequence over a block. A user has no idea about the number of iteration that will take place during the execution it can be used in problems where a condition or an expression needs to be satisfied in order to execute multiple statements.
  • Use and operator to require multiple condition for a while loop to execute.

Syntax:

Here is the syntax of the while loop in Python.

while expression:
     statement(s)

Example:

Let’s take an example and check how to use multiple conditions using while loop

a = 17
b = 12
 
while a > 0 and b > 0 :
    a -= 3
    b -= 2
    print((a,b))

In this example, we can easily use a while loop with conditions. Now create two variables and assign them values.

Here is the screenshot of the following given code

Python while loop multiple conditions
Python while loop multiple conditions

Another example is to check how to use multiple conditions using while loop.

  • In this example, we will use an OR operator to combine with the while loop condition.
  • Here, we can easily use OR logical operator to combine multiple condition.
  • Logical and or operator to evaluate them separately and consider their result. The while loop body will execute and this condition expression is true.

Example:

Let’s take an example and check how to use OR operator in multiple conditions

m = 14
n = 10
 
while m > 0 or n > 0 :
    m -= 3
    n -= 2
    print(m,n)

Here is the Screenshot of the following given code

Python while loop multiple condition by or operator
Python while loop multiple conditions by or operator

Read Working with JSON data in Python

Not operator to use multiple conditions using while loop

  • In this example, we can easily use a logical not operator to execute Python while loops with multiple conditions.
  • The not operator reverse the value of a given expression. In simple words we can give the condition true if either of the operands is true.
  • Let’s take an example and check how to use not operator in while loop multiple condition.
import random
m = 4
n = 8
iter_items = random.randint(0,5)

coun_no = 0
while coun_no < m or coun_no < n and not coun_no >= iter_items:
    print(f"counted value: {coun_no}, z: {m}, y: {n}")
    coun_no += 1

Here is the Screenshot of the following given code

Python while loop multiple condition by not operator
Python while loop multiple conditions by not operator

Read Sorting algorithms in Python

Grouping multiple conditions in Python

  • Python multiple conditions in an if statement requires several true conditions at the same time. In this example, we can easily use the multiple logical operators in the while loop condition.
  • Here, we can easily use parenthesis to visualize conditional expression from left to right.
  • This can be done by using the ‘and’ ‘or’ operator in a single statement.
  • In this example set max_value function to a small value to see this in multiple conditions.

Example:

m=9
n=10
max_value = 3
count_no = 0
while (count_no < m or count_no < n) and not count_no >= max_value:
    print(f"count: {count_no}, m: {m}, n: {n}")
    count_no+=1

Here is the Screenshot of the following given code

Grouping multiple condition
Grouping multiple conditions

You may like the following Python tutorials:

In this tutorial, we learned while loop multiple conditions in Python.