- What relational operators are ā tools to compare values
- Equal to (==) ā check if two values are equal
- Not Equal (!=) ā check if two values are different
- Greater Than (>) ā check if one value is larger
- Less Than (<) ā check if one value is smaller
- Greater Than or Equal To (>=) ā check if one value is larger or equal
- Less Than or Equal To (<=) ā check if one value is smaller or equal
- Real-world examples ā age verification, grading, eligibility checks
- Common mistakes ā and how to avoid them
What are Relational Operators?
Relational operators (also called comparison operators) are used to compare two values and determine the relationship between them. They always return a Boolean value: True or False.
š” Key insight: Relational operators are the foundation of decision-making in Python. They power if statements, while loops, and all conditional logic.
Think of them as the "question words" of programming. They ask questions like: "Is this equal to that?" or "Is this greater than that?" and Python answers with True or False.
Quick Reference Table
Here's a quick reference of all Python relational operators:
| Operator | Example | Result | Description |
|---|---|---|---|
== |
5 == 5 |
True |
Equal to |
!= |
5 != 3 |
True |
Not equal |
> |
5 > 3 |
True |
Greater than |
< |
5 < 3 |
False |
Less than |
>= |
5 >= 5 |
True |
Greater than or equal to |
<= |
5 <= 10 |
True |
Less than or equal to |
1. Equal To (==)
The equal to operator (==) checks if two values are equal. It returns True if they are equal, and False otherwise.
# Equal To (==) Examples
print(5 == 5) # Output: True
print(5 == 3) # Output: False
# With variables
x = 10
y = 10
print(x == y) # Output: True
# With strings
print("hello" == "hello") # Output: True
print("hello" == "world") # Output: False
2. Not Equal (!=)
The not equal operator (!=) checks if two values are different. It returns True if they are not equal, and False otherwise.
# Not Equal (!=) Examples
print(5 != 3) # Output: True
print(5 != 5) # Output: False
# With variables
a = 10
b = 20
print(a != b) # Output: True
# With strings
print("python" != "java") # Output: True
print("python" != "python") # Output: False
3. Greater Than (>)
The greater than operator (>) checks if the left value is greater than the right value.
# Greater Than (>) Examples print(5 > 3) # Output: True print(3 > 5) # Output: False print(5 > 5) # Output: False # With variables age = 18 print(age > 17) # Output: True
4. Less Than (<)
The less than operator (<) checks if the left value is less than the right value.
# Less Than (<) Examples print(3 < 5) # Output: True print(5 < 3) # Output: False print(5 < 5) # Output: False # With variables score = 75 print(score < 80) # Output: True
5. Greater Than or Equal To (>=)
The greater than or equal to operator (>=) checks if the left value is greater than or equal to the right value.
# Greater Than or Equal To (>=) Examples print(5 >= 5) # Output: True print(5 >= 3) # Output: True print(3 >= 5) # Output: False # With variables temperature = 30 print(temperature >= 25) # Output: True
6. Less Than or Equal To (<=)
The less than or equal to operator (<=) checks if the left value is less than or equal to the right value.
# Less Than or Equal To (<=) Examples print(5 <= 5) # Output: True print(3 <= 5) # Output: True print(5 <= 3) # Output: False # With variables discount = 10 print(discount <= 15) # Output: True
7. Real-World Examples
š Age Verification for Voting
# Age Verification Example
age = 18
voting_age = 18
if age >= voting_age:
print("You are eligible to vote!")
else:
print("You are not eligible to vote yet.")
# Output: You are eligible to vote!
š Grade Calculator
# Grade Calculator Example
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
print(f"Score: {score}, Grade: {grade}")
# Output: Score: 85, Grade: B
š·ļø Price Comparison for Shopping
# Price Comparison Example
price1 = 25.99
price2 = 30.50
if price1 < price2:
print("Item 1 is cheaper!")
elif price1 > price2:
print("Item 2 is cheaper!")
else:
print("Both items have the same price!")
# Output: Item 1 is cheaper!
8. Common Mistakes to Avoid
ā Mistake 1: Using = instead of ==
This is the most common mistake! = is for assignment, == is for comparison.
# WRONG (assignment instead of comparison)
if x = 5: # This will cause a SyntaxError!
print("x is 5")
# CORRECT (comparison)
if x == 5:
print("x is 5")
ā Mistake 2: Confusing > and <
Remember: > means "greater than" and < means "less than."
# WRONG
if 5 < 3: # This is False, not True!
print("This won't print")
# CORRECT
if 5 > 3: # This is True
print("5 is greater than 3")
ā Mistake 3: Comparing different data types
Python can compare numbers, but comparing strings with numbers may cause errors.
# WRONG (comparing string with number)
if "10" > 5: # TypeError in some cases
print("This may cause an error")
# CORRECT (convert to same type)
if int("10") > 5: # Convert string to int
print("10 is greater than 5")
Try It Yourself!
Experiment with relational operators directly in your browser. Modify the code and see the results in real time.
RELATIONAL OPERATORS
========================================
1. EQUAL TO (==)
5 == 5: True
5 == 3: False
2. NOT EQUAL (!=)
5 != 3: True
5 != 5: False
3. GREATER THAN (>)
5 > 3: True
3 > 5: False
4. LESS THAN (<)
3 < 5: True
5 < 3: False
5. GREATER THAN OR EQUAL TO (>=)
5 >= 5: True
5 >= 3: True
6. LESS THAN OR EQUAL TO (<=)
5 <= 5: True
3 <= 5: True
7. REAL-WORLD EXAMPLE (Age Verification)
Age: 18
Can vote? True
ā Relational operators help you make decisions!
š You've Mastered Python Relational Operators!
You understand all relational operators (==, !=, >, <, >=, <=). These are essential for decision-making in Python!
Quick Quiz ā Test Your Knowledge
Let's see what you've learned about Python relational operators:
5 == 5 in Python?10 > 15?Frequently Asked Questions
š¤ What's the difference between = and ==?
= is the assignment operator used to assign values to variables. == is the equality operator used to compare two values. Never confuse them!
š§ Can relational operators work with strings?
"apple" < "banana" returns True.
š What do relational operators return?
True or False. They are used in conditional statements and loops.
š Can I chain relational operators in Python?
5 < x < 10. This checks if x is between 5 and 10.
š Where to Go From Here
Now that you understand Python relational operators, here are some related topics to explore:
š§ Logical Operators
Combine conditions with AND, OR, NOT
šÆ Decision Making
Use conditions to control program flow
š If Statements
Make decisions with if, elif, else