Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check multiple variables against a value in Python?
When working with multiple variables in Python, you often need to check if they match certain values or conditions. Python provides several approaches to efficiently compare multiple variables against values using logical operators, built-in functions, and data structures.
Using Logical 'and' and 'or' Operators
The most straightforward approach uses logical operators. The and operator requires all conditions to be true, while or requires at least one condition to be true.
Using 'and' Operator
Check if all variables equal specific values ?
x = 10
y = 20
z = 30
if x == 10 and y == 20 and z == 30:
print("All variables match their expected values")
else:
print("Not all variables match their expected values")
All variables match their expected values
Using 'or' Operator
Check if at least one variable equals a specific value ?
x = 5
y = 20
z = 35
if x == 10 or y == 20 or z == 30:
print("At least one variable matches its expected value")
else:
print("No variables match their expected values")
At least one variable matches its expected value
Using all() and any() Functions
Python's all() and any() functions provide a cleaner way to check multiple conditions by accepting an iterable of boolean expressions.
Using all() Function
The all() function returns True if all conditions in the list are true ?
price = 100
quantity = 5
discount = 10
if all([price == 100, quantity == 5, discount == 10]):
print("All variables have correct values")
else:
print("Some variables have incorrect values")
All variables have correct values
Using any() Function
The any() function returns True if at least one condition is true ?
temperature = 25
humidity = 60
pressure = 1000
if any([temperature > 30, humidity > 80, pressure < 950]):
print("At least one condition is met")
else:
print("No conditions are met")
No conditions are met
Checking Against Same Value
When checking if multiple variables equal the same value, you can use membership testing with tuples for cleaner code ?
status1 = "active"
status2 = "active"
status3 = "inactive"
# Method 1: Using 'in' operator with tuple
target_value = "active"
if all(status in [target_value] for status in [status1, status2, status3]):
print("All statuses are active")
else:
print("Not all statuses are active")
# Method 2: Direct comparison
if status1 == status2 == target_value:
print("status1 and status2 are both active")
Not all statuses are active status1 and status2 are both active
Using Sets for Efficient Comparison
For checking unique values or membership, sets provide an efficient approach ?
grades = [85, 92, 78, 96, 88]
passing_grades = {85, 92, 96, 88}
# Check if all grades are passing grades
if set(grades).issubset(passing_grades.union({78})):
print("All grades are accounted for")
# Check if any grade is above 90
if any(grade > 90 for grade in grades):
print("At least one grade is above 90")
All grades are accounted for At least one grade is above 90
Comparison of Methods
| Method | Best For | Readability | Performance |
|---|---|---|---|
| Logical operators | Simple conditions | High | Fast |
| all()/any() | Multiple conditions | Very High | Fast |
| Sets | Membership testing | Medium | Very Fast |
| Chained comparison | Same value checks | High | Fast |
Conclusion
Use all() and any() functions for clean, readable code when checking multiple conditions. For simple comparisons, logical operators work well, while sets excel at membership testing for better performance.
