Comparison Operators
Comparison operators are used to compare values.
They return either True or False according to the condition.
| Operator | Description | Example |
|---|---|---|
> |
Greater than: True if left operand is greater than the right |
x > y |
< |
Less than: True if left operand is less than the right |
x < y |
== |
Equal to: True if both operands are equal |
x == y |
!= |
Not equal to: True if operands are not equal |
x != y |
>= |
Greater than or equal to: True if left operand is greater than or equal to the right |
x >= y |
<= |
Less than or equal to: True if left operand is less than or equal to the right |
x <= y |
Examples
Greater than (>)
a = 5
b = 10
print(a > b) # False
print(b > a) # True
print(a > a) # False
print(b > b) # FalseLess than (<)
a = 5
b = 10
print(a < b) # True
print(b < a) # False
print(a < a) # False
print(b < b) # FalseEqual to (==)
a = 5
b = 10
print(a == b) # False
print(b == a) # False
print(a == a) # True
print(b == b) # TrueNot equal to (!=)
a = 5
b = 10
print(a != b) # True
print(b != a) # True
print(a != a) # False
print(b != b) # FalseGreater than or equal to (>=)
a = 5
b = 10
print(a >= b) # False
print(b >= a) # True
print(a >= a) # True
print(b >= b) # TrueLess than or equal to (<=)
a = 5
b = 10
print(a <= b) # True
print(b <= a) # False
print(a <= a) # True
print(b <= b) # True