Last updated: Apr 9, 2024
Reading timeยท8 min
Use the equality operator to check if multiple variables are equal, e.g.
if a == b == c:
.
The expression will compare the variables on the left-hand and right-hand
sides of the equal signs and will return True
if they are equal.
a = 123 b = 123 c = 123 if a == b == c: # ๐๏ธ this runs print('Multiple variables are equal')
We used the equality ==
operator multiple times to check if multiple variables
store the same value.
If the variables store the same value, the expression returns True
, otherwise,
False
is returned.
a = 123 b = 123 c = 123 are_equal = a == b == c print(are_equal) # ๐๏ธ True
If you need to check if multiple variables are equal to a specific value, add another equal sign to the expression and compare to the value.
a = 123 b = 123 c = 123 are_equal_to_value = a == b == c == 123 print(are_equal_to_value) # ๐๏ธ True if a == b == c == 123: # ๐๏ธ this runs print('Variables are equal to value')
To check if multiple variables in a sequence (e.g. list, tuple) are equal:
count()
method to count the occurrences of the first item in the
sequence.a = 123 b = 123 c = 123 my_list = [a, b, c] if my_list.count(my_list[0]) == len(my_list): # ๐๏ธ this runs print('Multiple variables are equal')
The list.count()
method takes a value as an argument and returns the number of
times the provided value appears in the list.
Alternatively, you can use a generator expression to check if all values in a sequence are equal.
a = 123 b = 123 c = 123 my_list = [a, b, c] if all(item == my_list[0] for item in my_list): # ๐๏ธ this runs print('Multiple variables are equal.')
We used a generator expression to iterate over the list.
On each iteration, we check if the current value is equal to the first list element and return the result.
The all() built-in function
takes an iterable as an argument and returns True
if all elements of the
iterable are truthy (or the iterable is empty).
This is a three-step process:
all()
function.a = 123 b = 123 c = 123 my_list = [a, b, c] if all(item == 123 for item in my_list): # ๐๏ธ This runs print('Variables are equal to value')
On each iteration, we compare the current list item to a specific value and return the result.
If all of the comparisons evaluate to True
, the all()
function will also
return True
.
You can use the not in
operator to check if a variable is not equal to
multiple values.
The not in
operator will return True
if the variable is not equal to any of
the specified values and False
otherwise.
my_str = 'another' multiple_values = ['bobby', 'hadz', 'com'] if my_str not in multiple_values: # ๐๏ธ this runs print('The variable is NOT equal to any of the specified values') else: print('The variable is equal to one or more of the specified values') print(my_str not in multiple_values) # ๐๏ธ True
We used the not in
operator to check if a variable is not equal to multiple
values.
The in operator tests
for membership. For example, x in l
evaluates to True
if x
is a member of
l
, otherwise it evaluates to False
.
x not in l
returns the negation of x in l
.The if
block will run only if the variable is not equal to any of the
specified values, otherwise the else
block runs.
Alternatively, you can use the all()
function.
The all()
function will return True
if the variable is not equal to any of
the values and False
otherwise.
my_str = 'another' multiple_values = ['bobby', 'hadz', 'com'] if all(my_str != item for item in multiple_values): # ๐๏ธ this runs print('The variable is NOT equal to any of the specified values') else: print('The variable is equal to one or more of the specified values') # ๐๏ธ True print(all(my_str != item for item in multiple_values))
We used a generator expression to iterate over the list.
On each iteration, we check if the variable is not equal to the current value and return the result.
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).
my_str = 'another' multiple_values = ['bobby', 'hadz', 'com'] # ๐๏ธ True print(all(my_str != item for item in multiple_values))
If the all()
function finds a value that is equal to the variable, it will
short-circuit returning False
.
You can use the in
operator to check if a variable equals one of two values.
The in
operator will evaluate to True
if the variable is equal to one of the
two values and False
otherwise.
my_str = 'bobby' two_values = ('bobby', 'hadz') if my_str in two_values: # ๐๏ธ this runs print('The variable is equal to one of the two values') else: print('The variable is NOT equal to any of the specified values') print(my_str in two_values) # ๐๏ธ True
We used the in
operator to check if a variable is equal to one of two values.
We grouped the values in a tuple
, so we can test for membership.
The in operator tests
for membership. For example, x in t
evaluates to True
if x
is a member of
t
, otherwise it evaluates to False
.
If the variable is equal to at least one of the values, the if
block runs,
otherwise, the else
block runs.
my_str = 'bobby' two_values = ('bobby', 'hadz') print(my_str in two_values) # ๐๏ธ True print('another' in two_values) # ๐๏ธ False
Alternatively, you can use the any()
function.
my_str = 'bobby' two_values = ('bobby', 'hadz') if any(my_str == item for item in two_values): # ๐๏ธ this runs print('The variable is equal to one of the two values') else: print('The variable is NOT equal to any of the specified values')
We used a generator expression to iterate over the list.
On each iteration, we check if the variable is equal to the current value and return the result.
The any function
takes an iterable as an argument and returns True
if any element in the
iterable is truthy.
my_str = 'bobby' two_values = ('bobby', 'hadz') # ๐๏ธ True print(any(my_str == item for item in two_values))
If the condition is met, the any()
function short-circuits and returns True
.
If the iterable is empty or none of the elements in the iterable are truthy, the
any
function returns False
.
my_str = 'another' two_values = ('bobby', 'hadz') if any(my_str == item for item in two_values): print('The variable is equal to one of the two values') else: # ๐๏ธ this runs print('The variable is NOT equal to any of the specified values') # ๐๏ธ False print(any(my_str == item for item in two_values))
The variable is not equal to any of the specified values, so the condition is
never met and the else
block runs.
Use the boolean OR operator to compare multiple variables to the same value.
a = 'dev' b = 'test' c = 'ship' # โ Check if one of multiple variables is equal to a value if a == 'dev' or b == 'dev' or c == 'dev': # ๐๏ธ this runs print('One or more of the variables is equal to dev') else: print('None of the variables is equal to dev') # ------------------------------------------------------- # โ Check if one of multiple variables is equal to a value using `in` if 'dev' in (a, b, c): print('One or more of the variables is equal to dev') # ------------------------------------------------------- # โ Check if multiple variables are equal to a value if a == b == c == 'dev': print('The variables are equal to dev') # ------------------------------------------------------- # โ Check if multiple variables are equal to value using `and` if a == 'dev' and b == 'dev' and c == 'dev': print('The variables are equal to dev')
We used the boolean or
operator to check if one of the multiple variables is
equal to a value.
The if
block in the example runs if either of the 3 conditions is met.
You don't have to use the equality (==) operator, you can use any of the other comparison operators.
Alternatively, you can use the in
operator.
in
operatorThis is a three-step process:
in
operator to check if the value is contained in the tuple.in
operator will return True
if the value is equal to at least one of
the variables.a = 'dev' b = 'test' c = 'ship' # โ Check if one of multiple variables is equal to a value using `in` if 'dev' in (a, b, c): print('One or more of the variables is equal to dev')
The in operator tests
for membership. For example, x in t
evaluates to True
if x
is a member of
t
, otherwise it evaluates to False
.
If you need to compare multiple variables to the same value where all conditions
have to be met, use the and
operator.
a = 'dev' b = 'test' c = 'ship' if a == 'dev' and b == 'dev' and c == 'dev': print('The variables are equal to dev')
The if
block runs only if all of the variables are equal to the specified
value.
You can use the same approach with different comparison operators.
Here is an example that checks if multiple variables are all greater than 0
.
a = 1 b = 2 c = 3 if a > 0 and b > 0 and c > 0: # ๐๏ธ this runs print('The variables are all greater than 0')
If you need to check if multiple variables are equal to a value, use the equality (==) operator multiple times.
a = 'dev' b = 'dev' c = 'dev' if a == b == c == 'dev': # ๐๏ธ this runs print('The variables are equal to dev')
The if
block runs only if all of the variables store the specified value.
You can learn more about the related topics by checking out the following tutorials: