Skip to content
Related Articles
Open in App
Not now

Related Articles

How to check multiple variables against a value in Python?

Improve Article
Save Article
Like Article
  • Last Updated : 19 May, 2021
Improve Article
Save Article
Like Article

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 variables
a = 100
b = 0
c = -1
 
# checking multiple variables against a value
if 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 variables
a = 100
b = 0
c = -1
 
# checking multiple variables against a value
if 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 variables
a = 90
 
# checking multiple variables against a value
if 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 variables
a = 9
b = 9
c = 9
 
# checking multiple variables against a value
if a == b == c == 9:
    print('Value Found!')
else:
    print('Not Found!')

Output:

Value Found!

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!