• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Multiple Condition While Loops in Python

Multiple Condition While Loops in Python

February 18, 2022 Leave a Comment

In Python, while loops are very useful to loop over a collection of data. We can easily define a while loop with multiple conditions using logical operators.

count = 1

while count < 10 and count % 4 != 0:
    print(count)
    count = count + 1

#Output:
1
2
3

In Python, loops allow us to iterate over collections of data and perform various operations many times. Both while loops and for loops are useful, but in certain cases, while loops can be better.

When dealing with complex situations in our Python programs, we may need to create a while loop with multiple conditions.

Fortunately, we can use logical operators to create complex logical statements to handle loops with multiple conditions. We can use the logical operators and, or, and not to create while loops with multiple conditions easily.

Let’s say we want to create a while loop with two conditions. The first condition is that we want our counter variable to be less than 10. The second condition is we want the division of our counter by 4 to have a remainder not equal to 4.

Logically, those two conditions are as follows:

count < 10 and count % 4 != 0

We can use these conditions in a while loop easily.

Below is an example of a multiple condition while loop using the logical and operator in Python.

count = 1

while count < 10 and count % 4 != 0:
    print(count)
    count = count + 1

#Output:
1
2
3

Using the Logical Operator or with Multiple Conditions in a Python While Loop

We can also use the or operator to create a while loop with multiple conditions.

The or operator is true when at least one of the logical statements it joins are true, and is false if all of the statements are false.

Below is an example of a multiple condition while loop using the logical or operator in Python.

count = 1

while count < 10 or count % 4 != 0:
    print(count)
    count = count + 1

#Output:
1
2
3
4
5
6
7
8
9
10
11

Using the Logical Operator not with Multiple Conditions in a Python While Loop

We can also use the not operator to create a while loop with multiple conditions.

The not operator negates the boolean value returned by a logical statement.

Below is an example of a multiple condition while loop using the logical not operator in Python.

count = 15

while not(count < 10 and count % 4 != 0):
    print(count)
    count = count - 1

#Output:
15
14
13
12
11
10

This example is equivalent to the following while loop.

count = 15

while not(count < 10) or not(count % 4 != 0):
    print(count)
    count = count - 1

#Output:
15
14
13
12
11
10

Hopefully this article has been helpful for you to learn how to use while loops with multiple conditions in Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Read File Word by Word
  • 2.  How to Clear Turtle Screen in Python with clear() Function
  • 3.  Get Length of File Using Python
  • 4.  Add Minutes to Datetime Variable Using Python timedelta() Function
  • 5.  Check if File Exists in AWS S3 Bucket Using Python
  • 6.  How to Check if List is Empty in Python
  • 7.  Sort a List of Strings Using Python
  • 8.  What is the Correct File Extension for Python Files?
  • 9.  Read Pickle Files with pandas read_pickle Function
  • 10.  Using Python to Convert Integer to String with Leading Zeros

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy