This guide explains Comparison Operators: ==, !=, <, >, <=, and >= through a focused example and practical reasoning. No prior knowledge of this specific operation is required.
Understanding the Concept
Comparison operators compare values and return booleans. Equality (==) and inequality (!=) compare values; ordering operators compare compatible types. Different non-orderable types can raise TypeError.
When You Would Use This
A booking program checks whether a reservation price is under a budget.
Step-by-Step Example
reservation_cost = 65
budget_limit = 80
print(reservation_cost < budget_limit)
print(reservation_cost == 65)
print(reservation_cost != 80)
print(4 <= 4)
How the Example Works
All four checks return True. A comparison is an expression, so it can be assigned to a variable or placed directly inside an if condition.
Example Output
True
True
True
True
Try It Yourself
Check whether a room temperature lies between 18 and 26 inclusive.
Common Beginner Mistakes
- Using
=to test equality. - Comparing a numeric string to a number as if their values were identical.
- Assuming
0 == Falseimplies they are the same type.
Key Takeaways
- Comparison operators compare values and return booleans.
- All four checks return
True. - Avoid this mistake: Using
=to test equality.