Check for multiple conditions in an if statement in Python

avatar

Borislav Hadzhiev

Last updated: May 1, 2022

banner

Photo from Unsplash

Check for multiple conditions in an if statement in Python #

Use the boolean and operator to check for multiple conditions in an if statement, e.g. if a == 1 and b == 3 and c == 7:. The if block will only run if all of the conditions are met.

main.py
a = 1 b = 3 c = 7 # ✅ check for multiple conditions using AND if a == 1 and b == 3 and c == 7: # 👇️ this runs print('All conditions in the if statement are met') else: print('Not all conditions in the if statement are met') # -------------------------------------------- # ✅ check for multiple conditions using OR if a == 10 or b == 30 or c == 7: # 👇️ this runs print('One or more conditions in the if statement are met') else: print('None of the conditions in the if statement is met')

The first example uses the boolean and operator to check for multiple conditions in an if statement.

The if block only runs if all of the conditions are met, otherwise the else block runs.

You can use any of the comparison operators in the conditions.

main.py
a = 1 b = 3 c = 7 if a > 0 and b > 0 and c > 0: # 👇️ this runs print('All conditions in the if statement are met') else: print('Not all conditions in the if statement are met')

The example checks if the variables a, b and c store values that are greater than 0.

If any of the conditions evaluates to False, the interpreter short-circuits and doesn't evaluate the next conditions.

Alternatively, you can use the all() function.

main.py
a = 1 b = 3 c = 7 if all([a > 0, b > 0, c > 0]): # 👇️ this runs print('All conditions in the if statement are met') else: print('Not all conditions in the if statement are met')

We passed a list containing multiple conditions to the all() function.

The all() built-in function takes an iterable as an argument and returns True if all elements in the iterable are truthy (or the iterable is empty).

If all of the conditions in the list evaluate to True, the if block runs, otherwise the else block runs.

If one of the conditions isn't met, the all() function will short-circuit and the else block will run.

Use the boolean or operator to check for multiple conditions in an if statement, e.g. if a == 10 or b == 30 or c == 7:. The if block will run if at least one of the conditions is met.

main.py
a = 1 b = 3 c = 7 # ✅ check for multiple conditions using OR if a == 10 or b == 30 or c == 7: # 👇️ this runs print('One or more conditions in the if statement are met') else: print('None of the conditions in the if statement is met')
When using the boolean or operator, the if block runs if at least one condition is met.

Alternatively, you can use the any() function to check if at least one of multiple conditions is satisfied in an if statement.

main.py
a = 1 b = 3 c = 7 if any([a > 100, b > 100, c > 6]): # 👇️ this runs print('All conditions in the if statement are met') else: print('Not all conditions in the if statement are met')

We passed a list containing multiple conditions to the any() function.

The any function takes an iterable as an argument and returns True if any element in the iterable is truthy.

If at least one of the conditions is met, the if block runs, otherwise the else block runs.

If one of the conditions is met, the any() function will short-circuit returning True and the if block will run.

If none of the conditions is met, the any() function will return False and the else block will run.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.