Comparison operators
Comparison operators are used to compare the values. It returns either True or False. These operators are used to build condition. Here are the list of comparison operators
Operator |
Meaning |
Example |
> ( Greater than ) |
True if the left operand is greater than the right |
x > y |
< ( Less than ) |
True if the left operand is less than the right |
x < y |
== ( Equal to ) |
True if both the operands are equal |
x == y |
!= ( Not equal to ) |
True if operands are not equal |
x != y |
>= ( Greater than or equal to ) |
True if the left operand is greater than or equal to the right |
x >= y |
<= ( Less than or equal to ) |
True if the left operand is less than or equal to the right operand |
x <= y |
Example 1 : Comparison operators in Python
a = 1
b = 2
print("a > b is", a > b)
# Output: a > b is False
print("a < b is", a < b)
# Output: a < b is True
print('a == b is',a==b)
# Output: a == b is False
print('a != b is',a!=b)
# Output: a != b is True
print('a >= b is',a>=b)
# Output: a >= b is False
print('a <= b is',a<=b)
# Output: a <= b is True
a = 2
b = 2
print('a >= b is',a>=b)
# Output: a >= b is True
print('a <= b is',a<=b)
# Output: a <= b is True
Output
a > b is False
a < b is True
a == b is False
a != b is True
a >= b is False
a <= b is True
a >= b is True
a <= b is False
Example 2 : Comparison operators in Python
print("1 > 2 is", 1 > 2)
# Output: 1 > 2 is False
print("1.0 < 2.0 is", 1.0 < 2.0)
# Output: 1.0 < 2.0 is True
print('1 == 2 is', 1==2)
# Output: a == b is False
Output
1 > 2 is False
1.0 < 2.0 is True
1 == 2 is False