List of Comparison Operators
Operator | Symbol | Description | Example (a = 10, b = 20) |
---|---|---|---|
Equal to | == | Checks if two values are equal | a == b → False |
Not equal to | != | Checks if two values are not equal | a != b → True |
Greater than | > | Checks if the left value is greater | a > b → False |
Less than | < | Checks if the left value is smaller | a < b → True |
Greater than or equal to | >= | Checks if left ≥ right | a >= b → False |
Less than or equal to | <= | Checks if left ≤ right | a <= b → True |
Real-Life Analogy for comparison operators
Comparison operators can be understood using real-life scenarios. Imagine you are comparing scores in a game:
- Equal to (==): Did both players score the same points?
- Not equal to (!=): Did Player A and Player B score different points?
- Greater than (>): Did Player A score more than Player B?
- Less than (<): Did Player A score less than Player B?
- Greater than or equal to (>=): Did Player A score the same or more points than Player B?
- Less than or equal to (<=): Did Player A score the same or fewer points than Player B?
Basic Syntax
value1 operator value2
The expression evaluates to True or False based on the relationship between value1 and value2.
Examples for comparison operators in Python
1. Equal to (==) Checks if two values are equal. a = 10 b = 20 print(a == b) # Output: False
Example:1 Are two employees' salaries equal?
employee1_salary = 50000 employee2_salary = 50000 print(employee1_salary == employee2_salary) # Output: True
Example:2 Not Equal to (!=)
Checks if two values are not equal. a = 10 b = 20 print(a != b) # Output: True
Real-Life Example: Are the prices of two products different?
product1_price = 25 product2_price = 30 print(product1_price != product2_price) # Output: True
Example:3 Greater than (>)
Checks if the left value is greater than the right. a = 10 b = 20 print(a > b) # Output: False
Real-Life Example: Is the temperature today hotter than yesterday?
today_temp = 35 yesterday_temp = 30 print(today_temp > yesterday_temp) # Output: True
Example 4. Less than (<)
Checks if the left value is smaller than the right. a = 10 b = 20 print(a < b) # Output: True
Real-Life Example: Is the speed of a car slower than the speed limit?
car_speed = 80 speed_limit = 100 print(car_speed < speed_limit) # Output: True
Example 5. Greater than or Equal to (>=)
Checks if the left value is greater than or equal to the right.
a = 10 b = 20 print(a >= b) # Output: False
Real-Life Example: Has a student scored the minimum marks required to pass?
student_score = 40 passing_score = 40 print(student_score >= passing_score) # Output: True
Example 6. Less than or Equal to (<=)
Checks if the left value is less than or equal to the right.
a = 10 b = 20 print(a <= b) # Output: True
Real-Life Example: Is the current inventory level less than or equal to the reorder threshold?
inventory_level = 15 reorder_threshold = 20 print(inventory_level <= reorder_threshold) # Output: True
Using Comparison Operators with Conditional Statements
Comparison operators are commonly used in if statements to make decisions in your program.
Example: Granting access based on age
age = 18 if age >= 18: print("Access granted.") else: print("Access denied.")
Comparison Operators in Iterables
1. Comparing StringsStrings are compared lexicographically (dictionary order).
print("apple" < "banana") # Output: True2. Checking if a Value Exists
You can use the 'in' operator to check if a value exists in a list, tuple, or dictionary.
numbers = [1, 2, 3, 4, 5] print(3 in numbers) # Output: True
Chaining Comparison Operators
You can chain multiple comparison operators in a single statement.
Example 1:
x = 15 print(10 < x <= 20) # Output: True
This checks if x is greater than 10 and less than or equal to 20.
Real-World Application Example 2: Loan Eligibility
Compare multiple conditions to decide if a person qualifies for a loan.
age = 25 credit_score = 700 income = 50000 if age >= 18 and credit_score >= 650 and income > 30000: print("Loan Approved") else: print("Loan Denied")Example : Password Strength Check Ensure the password length meets a minimum requirement.
password = "mypassword123" if len(password) >= 8: print("Password is strong.") else: print("Password is weak.")
Key Takeaways while using Comparison operators
Comparison operators always return True or False.
They are essential for decision-making in if-else statements, loops, and more.
Use chained comparisons for clean and efficient code.
They work with numbers, strings, and other comparable types.
Let me know if you'd like more advanced examples or clarification!