How to check multiple variables against a value in Python?


The variable is an entity that stores on different values. When we assign a new value, every time, the previous value will be replaced with the new one. In python, a variable can be alphabetic, underscore, numeric value starting with the alphabet etc. The multiple variables can be against a value; it can be checked by using the following ways in python.

Using logical ‘or’ and ‘and’

One of the way to check multiple variables against a value in python is by logical or and and. The and operator will check if all the variables are equal to their respective values and the or operator will check if any one variable is equal to its respective value.

Example

In this example we will use the and operator to check if all the defined variables are equal to their respective values. The following is the code which can be considered as the reference.

a = 300
b = 400
c = 10
if a == b and b == c and c == a:
    print("All the variables are equal to their respective values")
else:
    print("All the variables are not equal to their respective values")

Output

The below is the output of the and operator used to check if all the multiple variables are equal to their respective values.

All the variables are not equal to their respective values

Example

In this example we will use the or operator to check at least one variable is equal to its respective value.

a = 300
b = 10
c = 10
if a == b or b == c or c == a:
    print("All the variables are equal to their respective values")
else:
    print("All the variables are not equal to their respective values")

Output

All the variables are equal to their respective values

Using list of variables

The other way to check if the given variables are equal to their respective values by using the all() function and any() function of the python.

The all() function will check if all the values of the given variables is equal or not and the any() function will check if at least one variable is equal to its respective value.

Example

Here, we will pass the list of variables with the values to the all() function then it will return the text whether all the variables equal to their respective values or not.

a = 300
b = 10
c = 10
if all([a == 300,b == 10,c == 10]):
    print("All the variables are equal to their respective values")
else:
    print("All the variables are not equal to their respective values")

Output

All the variables are equal to their respective values

Example

Let’s see another example of using the any() function to check at least one variable is equal to its respective value.

a = 300
b = 10
c = 10
if any([a == 0,b == 0,c == 0]):
    print("At least one variable is equal to their respective values")
else:
    print("All the variables are not equal to their respective values") 

Output

All the variables are not equal to their respective values

Using a Dictionary

In the way we will use the dictionary to save the variables as keys and their respective value as values. Here to check if all or at least one variable in dictionary is equal to the respective value we use the list comprehension along with the locals() function. The locals() function is used to filter the variables in the dictionary.

Example

In this example we will check if all the variables in the dictionary are equal to their respective values using the list comprehension and locals() function.

a = 300
b = 20
c = 30
d = 340
dic = {"a" : 10,"b" : 20,"c" : 40,"d" : 230}
if all([dic[key] == value for key, value in locals().items() if key in dic]):
    print("All the variables are equal to their respective values")
else:
     print("All the variables are not equal to their respective values")

Output

All the variables are not equal to their respective values

Updated on: 09-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements