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
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
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
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
You may like the following Python tutorials:
- For loop vs while loop in Python
- Python For Loop
- Python while loop continue
- How to Sum Elements in List in Python using For Loop
In this tutorial, we learned while loop multiple conditions in Python.
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.