Comparison operators (also called relational operators) are used to compare two values or expressions. The result of a comparison is always a Boolean value:
TrueFalseThese operators are widely used in decision-making and conditions.
| Operator | Name | Description | Example |
|---|---|---|---|
== |
Equal to | Checks if values are equal | 5 == 5 → True |
!= |
Not equal to | Checks if values are different | 5 != 3 → True |
> |
Greater than | Left value is greater | 5 > 3 → True |
< |
Less than | Left value is smaller | 3 < 5 → True |
>= |
Greater or equal | Greater or equal | 5 >= 5 → True |
<= |
Less or equal | Smaller or equal | 3 <= 5 → True |
==)Checks whether two values are equal.
print(5 == 5)
print(5 == 3)
Output
True
False
!=)Checks whether two values are not equal.
print(5 != 3)
print(5 != 5)
Output
True
False
>)Checks if the left value is greater than the right value.
print(10 > 5)
Output
True
<)Checks if the left value is less than the right value.
print(3 < 7)
Output
True
>=)print(5 >= 5)
print(6 >= 5)
Output
True
True
<=)print(4 <= 5)
print(5 <= 5)
Output
True
True
a = 10
b = 20
print(a < b)
print(a == b)
print("apple" == "apple")
print("apple" > "banana")
Explanation
Python allows chaining of comparison operators.
x = 10
print(5 < x < 20)
Output
True
print(5 == 5.0)
Output
True
Explanation
age = 18
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible")
Comparison operators are used to compare values.
They return Boolean results.
Types include:
==, !=, >, <, >=, <=Useful for decision-making and control flow.
Supports chained comparisons.