Compare multiple variables to the same value in Python

avatar

Borislav Hadzhiev

Last updated: Jun 18, 2022

banner

Photo from Unsplash

Compare multiple variables to the same value in Python #

Use the boolean or operator to compare multiple variables to the same value, e.g. if a == 'value' or b == 'value' or c == 'value':. The expression will evaluate to True if any of the variables is equal to the specified value.

main.py
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 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.

To compare multiple variables to the same value:

  1. Group the variables in a tuple.
  2. Use the in operator to check if the value is contained in the tuple.
  3. The in operator will return True if the value is equal to at least one of the variables.
main.py
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.

main.py
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.

main.py
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.

main.py
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.

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.