How to check multiple variables against a value in Python?
Last Updated :
19 May, 2021
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
a = 100
b = 0
c = - 1
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
a = 100
b = 0
c = - 1
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
a = 90
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
a = 9
b = 9
c = 9
if a = = b = = c = = 9 :
print ( 'Value Found!' )
else :
print ( 'Not Found!' )
|
Output:
Value Found!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...