What are Relational Operators ?

Relational operators (also known as Comparison Operators) are symbols used in Python to compare two values or expressions.

When you use a relational operator, Python examines the relationship between the two operands and always returns a Boolean value: either True or False. These operators form the absolute backbone of decision-making in programming. They are heavily used inside conditional statements (like if-else blocks) and loops to control the execution path of a script.

Python Relational Operators Table

Operator Name Syntax
== Equal To a == b
!= Not Equal To a != b
> Greater Than a > b
<< /th> Less Than a < b
>= Greater Than or Equal To a >= b
<=< /th> Less Than or Equal To a <= b

1. Equal To Operator (==)

The equal to operator (==) checks if the values of two operands are completely identical. If they are equal, the condition evaluates to True; otherwise, it returns False.
Note: Do not confuse == with the single =, which is used to assign values to variables

# Storing two passwords to check for a match
saved_password = "SecurePass123"
entered_password = "WrongPass123"

# Checking if they are identical
is_match = saved_password == entered_password

print("Access granted:", is_match)
# Output: Access granted: False

2. Not Equal To Operator (!=)

The not equal to operator (!=) checks if two values are different from each other. If the values are not equal, it returns True. If they are completely identical, it returns False.

# Storing the original price and a discounted coupon price
original_price = 500
coupon_price = 350

# Checking if a discount has been successfully applied
has_discount = original_price != coupon_price

print("Is a discount active?", has_discount)
# Output: Is a discount active? True

3. Greater Than Operator (>)

The greater than operator (>) checks if the left-hand value is strictly larger than the right-hand value. If it is larger, it returns True; otherwise, it returns False.

# Storing a user's age and the legal driving age limit
user_age = 21
driving_limit = 18

# Checking if the user is old enough to drive
can_drive = user_age > driving_limit

print("Is the user allowed to drive?", can_drive)
# Output: Is the user allowed to drive? True

4. Less Than Operator (<)< /h3>

The less than operator (<) checks if the left-hand value is strictly smaller than the right-hand value. If it is smaller, it returns True; otherwise, it returns False.

# Tracking current temperature against a water freezing point
current_temp = -4
freezing_point = 0

# Checking if water will turn into ice
is_freezing = current_temp < freezing_point

print("Is the water freezing?", is_freezing)
# Output: Is the water freezing? True

5. Greater Than or Equal To Operator (>=)

The greater than or equal to operator (>=) checks if the left-hand value is either larger than or exactly equal to the right-hand value. If either condition is met, it returns True.

# Checking exam pass boundaries
student_marks = 40
passing_marks = 40

# A student passes if they score higher than or exactly the passing mark
has_passed = student_marks >= passing_marks

print("Did the student pass?", has_passed)
# Output: Did the student pass? True

6. Less Than or Equal To Operator (<=)< /h3>

The less than or equal to operator (<=) checks if the left-hand value is either smaller than or exactly equal to the right-hand value. If either condition is met, it returns True.

# Checking flight luggage weight limits
baggage_weight = 15.5
allowed_limit = 15.5

# Luggage is safe if it weighs less than or exactly the allowed limit
is_allowed = baggage_weight <= allowed_limit

print("Is the baggage within limits?", is_allowed)
# Output: Is the baggage within limits? True

Chaining Relational Operators

A unique feature of Python is the ability to chain multiple relational operators together in a single line, mimicking standard mathematical inequalities. This makes code incredibly readable.

age = 25

# Checking if age is between 18 and 30 inclusive
is_eligible = 18 <= age <= 30

print("Is person eligible?", is_eligible)
# Output: Is person eligible? True

Python evaluates this from left to right. It checks if 18 <= 25 (which is True) and if 25 <=30 (which is also True). Since both parts pass, the final outcome is True.