How to check multiple variables against a value in Python?
Given some variables, the task is to write a Python program to check multiple variables against a value. There are three possible known ways to achieve this in Python:
Method #1: Using or operator
This is pretty simple and straightforward. The following code snippets illustrate this method.
Example 1:
Python3
# assigning variablesa = 100b = 0c = -1# checking multiple variables against a valueif a == -1 or b == 100 or c == -1: print('Value Found!')else: print('Not Found!') |
Output:
Value Found!
Method #2: Using in keyword
It is usually used to search through a sequence but can very well replace the code above.
Python3
# assigning variablesa = 100b = 0c = -1# checking multiple variables against a valueif a in [100, 0, -1]: print('Value Found!')else: print('Not Found!') |
Output:
Value Found!
You can use it for inverse statements also:
Example 2:
Python3
# assigning variablesa = 90# checking multiple variables against a valueif a not in [0, -1, 100]: print('Value Found!')else: print('Not Found!') |
Output:
Value Found!
Method #2: Using == operator
This approach is only applicable to multiple variables when checked with a single value.
Example 1:
Python3
# assigning variablesa = 9b = 9c = 9# checking multiple variables against a valueif a == b == c == 9: print('Value Found!')else: print('Not Found!') |
Output:
Value Found!




