Last updated: Apr 9, 2024
Reading timeยท3 min
Use the is
operator to check for a boolean value in an if statement, e.g.
if variable is True:
.
The is
operator will return True
if the condition is met and False
otherwise.
variable = True # โ check if a variable has a boolean value of True if variable is True: # ๐๏ธ this runs print('The boolean is True') # ------------------------------------------ # โ check if a variable is truthy if variable: # ๐๏ธ this runs print('The variable stores a truthy value')
The first example checks if the variable stores a True
boolean value.
You should use the is
operator when you need to
check if a variable stores a boolean value or None
.
Use the equality operators
(equals ==
and not equals !=
) when you need to check if a value is equal to
another value, e.g. 'abc' == 'abc'
.
In short, use the is
operator with built-in constants like True
, False
and
None
.
You can use the same approach when checking for False
or a falsy value in an
if
statement.
variable = False if variable is False: # ๐๏ธ this runs print('The boolean is False') if not variable: # ๐๏ธ this runs print('The variable stores a falsy value')
True
and False
is very different than checking for a truthy or falsy value.Here is how we would check for a truthy value in an if
statement.
variable = True if variable: # ๐๏ธ this runs print('The variable stores a truthy value')
The falsy values in Python are:
None
and False
.0
(zero) of any numeric type""
(empty string), ()
(empty tuple), []
(empty list), {}
(empty dictionary), set()
(empty set), range(0)
(empty
range).if
block runs if any other value is stored in the variable.Here are some examples.
if 'hello': print('this runs โ ') if ['a', 'b']: print('This runs โ ') if '': print('this does NOT run โ๏ธ') if 0: print('this does NOT run โ๏ธ')
if not X
returns the negation of if X
. So with if not X
, we check if X
stores a falsy value.
You will often have to use multiple conditions in a single if
statement.
You can do that by using the boolean AND or boolean OR operators.
if True and True: print('This runs โ ') if True and False: print('This does NOT run โ๏ธ') # -------------------------------------- if True or False: print('This runs โ ') if False or False: print('This does NOT run โ๏ธ')
The first two examples use the
boolean AND operator and the
second two examples use the boolean or
operator.
and
operator, the expressions on the left and right-hand sides have to be truthy for the if
block to run.In other words, both conditions have to be met for the if
block to run.
if True and True: print('This runs โ ') if True and False: print('This does NOT run โ๏ธ')
When using the boolean or
operator, either of the conditions has to be met for
the if
block to run.
if True or False: print('This runs โ ') if False or False: print('This does NOT run โ๏ธ')
If the expression on the left or right-hand side evaluates to a truthy value,
the if
block runs.
I've also written an article on how to check for multiple conditions in an if statement.
You can learn more about the related topics by checking out the following tutorials: