Comparison and logical operators
When coding, it’s often necessary to compare values or make decisions based on multiple conditions. For example, you might want to check if a number is greater than another, if two pieces of information are the same, or if multiple criteria are met simultaneously.
Python provides comparison operators for value comparison and logical operators for combining conditions to create complex logic.
Comparison operators
Comparison operators are used to compare two values and return a boolean result: True
False
Equal to ==
==
Checks if two values are the same. For example:
print(5 == 5) # Output: True
print(5 == 3) # Output: False
Usage:
Use ==
Not equal to !=
!=
Checks if two values are different. For example:
print(5 != 3) # Output: True
print(5 != 5) # Output: False
Usage:
Use !=
Greater than >
>
Checks if one value is larger than another. For example:
print(8 > 5) # Output: True
print(5 > 10) # Output: False
Usage:
Useful for comparing scores, ages, or sizes.
Less than <
<
Checks if one value is smaller than another. For example:
print(3 < 6) # Output: True
print(10 < 5) # Output: False
Usage:
Use <
Greater than or equal to >=
>=
Checks if a value is greater than or equal to another. For example:
print(7 >= 7) # Output: True
print(6 >= 8) # Output: False
Usage:
Commonly used to verify minimum requirements, such as eligibility criteria.
Less than or equal to <=
<=
Checks if a value is less than or equal to another. For example:
print(4 <= 4) # Output: True
print(9 <= 3) # Output: False
Usage:
Useful for scenarios like ensuring a value doesn’t exceed a maximum limit.
Logical operators
Logical operators let you combine multiple conditions into a single check. This is particularly useful when decision-making depends on more than one factor.
And and
and
Returns True
age = 25
is_adult = (age > 18) and (age < 65)
print(is_adult) # Output: True
Usage:
Use and
Or or
or
Returns True
day = "Saturday"
is_weekend = (day == "Saturday") or (day == "Sunday")
print(is_weekend) # Output: True
Usage:
Use or
Not not
not
Reverses the result of a condition. For example:
logged_in = False
print(not logged_in) # Output: True
Usage:
Use not
Combining comparison and logical operators
You can combine comparison and logical operators to create complex conditions. For example:
age = 20
has_ID = True
# Check if a person is allowed entry based on age and having an ID
can_enter = (age >= 18) and has_ID
print(can_enter) # Output: True
Real-World Application:
Checking if a user meets criteria for accessing restricted content or validating multi-factor conditions.
Order of operations (Operator precedence)
Python follows a specific order when evaluating combined conditions, similar to PEMDAS in arithmetic:
- Parentheses:
()
- Comparison Operators:
,==
,!=
,>
,<
,>=
<=
- Logical NOT:
not
- Logical AND:
and
- Logical OR:
or
Example:
x = 5
y = 10
z = 15
result = (x < y) and (y < z) or not (x == 5)
print(result) # Output: True
Practical examples
Validating input
password = "secure123"
user_input = "secure123"
if user_input == password:
print("Access granted.")
else:
print("Access denied.")
Checking age for eligibility
age = 17
parental_consent = True
if (age >= 18) or (age >= 16 and parental_consent):
print("Eligible to participate.")
else:
print("Not eligible.")
Finding ranges
number = 25
if number > 10 and number < 50:
print("Number is within range.")
else:
print("Number is out of range.")
Conclusion
Comparison and logical operators are essential for making decisions in Python. They empower your code to evaluate conditions, enforce rules, and create dynamic behaviors.
By mastering these tools, you’ll unlock the ability to write programs that can respond intelligently to a wide range of scenarios.
Start practicing these operators in your projects to build stronger logic and problem-solving skills!