Last updated: Apr 8, 2024
Reading timeยท4 min
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.
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.
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.
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
.
False
, the interpreter short-circuits and doesn't evaluate the next conditions.all()
function to check for multiple conditions in if
Alternatively, you can use the all()
function.
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.
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.
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')
or
operator, the if
block runs if at least one condition is met.any()
function to check for multiple conditionsAlternatively, you can use the any()
function to check if at least one of
multiple conditions is met in an if
statement.
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
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.
Here is an example of mixing the boolean AND and boolean OR operators.
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
.
# ๐๏ธ 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.
You can learn more about the related topics by checking out the following tutorials: