Check for multiple conditions in an if statement in Python

avatar
Borislav Hadzhiev

Last updated: Feb 18, 2023
4 min

banner

# Check for multiple conditions in an if statement in Python

Use the boolean and operator to check for multiple conditions in an if statement.

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')

check for multiple conditions in an if statement

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.

# Using the all() function to check for multiple conditions in if

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')

using all function to check for multiple conditions in if

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.

# Check for multiple conditions using boolean OR

If you need to check if at least one condition is met, use the boolean OR operator.

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.

# Using the any() function to check for multiple conditions

Alternatively, you can use the any() function to check if at least one of multiple conditions is met 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.

# Mixing the boolean AND and boolean OR operators

Here is an example of mixing the boolean AND and boolean OR operators.

main.py
num_1 = 50 num_2 = 75 if (num_1 > 100 and num_1 < 500) or num_2 > num_1: # ๐Ÿ‘‡๏ธ this runs print('if block runs') else: print('else block runs')

The code between the parentheses checks if the num_1 variable stores a number that is greater than 100 and the number is less than 500.

The first condition isn't met, so the expression evaluates to False.

When the first condition isn't met and the boolean AND operator is used, the second condition isn't evaluated at all.
main.py
# ๐Ÿ‘‡๏ธ this never runs (num_1 > 100 and num_1 < 500)

Then the boolean OR operator evaluates the condition to the right.

The condition checks if the num_2 variable is greater than num_1.

The condition is met, so the if block runs.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev