# Check for multiple conditions in an if statement in Python

Last updated: Apr 8, 2024
4 min

## #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
```Copied!```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
```Copied!```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
```Copied!```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.

## #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
```Copied!```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
```Copied!```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
```Copied!```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
```Copied!```#                 ๐๏ธ 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.