Relational operators are symbols used to compare values and determine their relationship to one another. They are essential in programming for making decisions and controlling the flow of code. Here's a breakdown of the most common relational operators:
1. Equal to (==)
5 == 5
evaluates to True
.2. Not equal to (!=)
5 != 6
evaluates to True
.3. Greater than (>)
10 > 5
evaluates to True
.4. Less than (<)
5 < 10
evaluates to True
.5. Greater than or equal to (>=)
10 >= 10
evaluates to True
.6. Less than or equal to (<=)
5 <= 10
evaluates to True
.Examples:
x = 10
y = 5
# Check if x is equal to y
if x == y:
print("x is equal to y")
# Check if x is greater than y
if x > y:
print("x is greater than y")
# Check if x is not equal to y
if x != y:
print("x is not equal to y")
Common Uses:
if
, else if
, else
) to control the flow of execution based on the result of a comparison.while
, for
) to determine when a loop should continue or terminate.Important Notes:
True
or False
).5 > 10
is different from 10 > 5
.