Python While Loop with Multiple Conditions

  • by
Python While Loop Multiple Conditions Cover Image

In this tutorial, you’ll learn how to write a Python while loop with multiple conditions, including and and or conditions. You’ll also learn how to use the NOT operator as well as how to group multiple conditions.

The Quick Answer: Embed Conditions with AND or OR Operators in Your While Loop

Quick Answer - Python While Loop Multiple Conditions

What is a Python While Loop

A Python while loop is an example of iteration, meaning that some Python statement is executed a certain number of times or while a condition is true. A while loop is similar to a Python for loop, but it is executed different. A Python while loop is both an example of definite iteration, meaning that it iterates a definite number of times, and an example of indefinite iteration, meaning that it iterates an indefinite number of times.

Let’s take a quick look at how a while loop is written in Python:

while [condition]:
     [do something]

In the example above, the while loop will complete the step do something indefinitely, until the condition is no longer met.

If, for example, we wrote:

while True:
    print('Welcome to datagy.io')

The program would run indefinitely, until the condition is not longer True. Because of this, we need to be careful about executing a while loop.

To see how we can stop a while loop in Python, let’s take a look at the example below:

a = 0

while a < 3:
    print('Hello')
    a += 1

# Returns
# Hello
# Hello
# Hello

In the sections below, you’ll learn more about how the Python while loop can be implemented with multiple conditions. Let’s get started!

Want to learn about Python for-loops? Check out my in-depth tutorial here, to learn all you need to know to get started!

Python While Loop with Multiple Conditions Using AND

Now that you’ve had a quick recap of how to write a Python while loop, let’s take a look at how we can write a while loop with multiple conditions using the AND keyword.

In this case, we want all of the conditions to be true, whether or not there are two, three, or more conditions to be met.

To accomplish meeting two conditions, we simply place the and keyword between each of the conditions. Let’s take a look at what this looks like:

a = 0
b = 10

while a < 4 and b > 3:
    print(f'Hello! The value of a is {a} and the value of b is {b}.')
    a += 1
    b -= 1

# Returns
# Hello! The value of a is 0 and the value of b is 10.
# Hello! The value of a is 1 and the value of b is 9.
# Hello! The value of a is 2 and the value of b is 8.
# Hello! The value of a is 3 and the value of b is 7.

We can see here that the code iterates only while both of the conditions are true. As soon as, in this case, a = 4, the condition of a < 4 is no longer true and the code stops execution.

Now let’s take a look at how we can implement an or condition in a Python while loop.

Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas!

Python While Loop with Multiple Conditions Using OR

Similar to using the and keyword in a Python while loop, we can also check if any of the conditions are true. For this, we use the or keyword, which checks whether either of our conditions are true.

In order to implement this, we simply place the or keyword in between the two conditions. We can also use more than two conditions and this would work in the same way.

For easier learning, let’s stick to two conditions:

a = 0
b = 10

while a < 4 or b > 3:
    print(f'Hello! The value of a is {a} and the value of b is {b}.')
    a += 1
    b -= 1

# Returns
# Hello! The value of a is 0 and the value of b is 10.
# Hello! The value of a is 1 and the value of b is 9.
# Hello! The value of a is 2 and the value of b is 8.
# Hello! The value of a is 3 and the value of b is 7.
# Hello! The value of a is 4 and the value of b is 6.
# Hello! The value of a is 5 and the value of b is 5.
# Hello! The value of a is 6 and the value of b is 4.

We can see that by simply switching from and to or, that or code execute many more times. In fact, the code runs until neither condition is not longer true.

Using a NOT Operator in a Python While Loop with Multiple Conditions

Another important and helpful operator to apply in Python while loops is the not operator. What this operator does is simply reverse the truth of a statement. For example, if we wrote not True, then it would evaluate to False. This can be immensely helpful when trying to write your code in a more plan language style.

Let’s see how we can apply this in one of our examples:

a = 0
b = 10

while a < 4 and not b < 3 :
    print(f'Hello! The value of a is {a} and the value of b is {b}.')
    a += 1
    b -= 1

# Returns
# Hello! The value of a is 0 and the value of b is 10.
# Hello! The value of a is 1 and the value of b is 9.
# Hello! The value of a is 2 and the value of b is 8.
# Hello! The value of a is 3 and the value of b is 7.

Here our code checks that a is less than 4 and that b is not less than 3. Because of this, our code only executes here until a is equal to 4.

Next, let’s take a look at how to group multiple conditions in a Python.

How to Group Multiple Conditions in a Python While Loop

There may be many times that you want to group multiple conditions, including mixing and and or statements. When you do this, it’s important to understand the order in which these conditions execute. Anything placed in parentheses will evaluated against one another.

To better understand this, let’s take a look at this example:

while (a or b) and c:
   print('Ok!')

In the code above, if either a or b evaluate to True and c is True then the code will run.

This is known as a Python truth table and it’s an important concept to understand.

In essence, the parentheses reduce the expression to a single truth that is checked against, simplifying the truth statement significantly.

Now, let’s take a look at a practical, hands-on example to better understand this:

a = 0
b = 10
c = 5

while (a < 4 or b > 3) and c < 9:
    print(f'Hello! The value of a is {a}, the value of b is {b}, and the value of c is {c}.')
    a += 3
    b -= 3
    c += 1

# Returns
# Hello! The value of a is 0, the value of b is 10, and the value of c is 5.
# Hello! The value of a is 3, the value of b is 7, and the value of c is 6.
# Hello! The value of a is 6, the value of b is 4, and the value of c is 7.

We can see here that the code stops after the third iteration. The reason for this a is less than 4 and b is greater than 3 after the third iteration. Because neither of the conditions in the parentheses are met, the code stops executing.

Conclusion

In this post, you learned how to use a Python while loop with multiple conditions. You learned how to use a Python while loop with both AND and OR conditions, as well as how to use the NOT operator. Finally, you learned how to group multiple conditions in a Python while loop.

To learn more about Python while loops, check out the official documentation here.

Cover of Introduction to Python for Data Science

Want to learn Python for Data Science? Check out my ebook for as little as $10!

Tags: