Skip to main content

Python Comparison Operators

Comparison Operators​

Comparison operators are used to compare values.

They return either True or False according to the condition.

note

Comparison operators are also known as Relational operators.

OperatorDescriptionExample
>Greater than: True if left operand is greater than the rightx > y
<Less than: True if left operand is less than the rightx < y
==Equal to: True if both operands are equalx == y
!=Not equal to: True if operands are not equalx != y
>=Greater than or equal to: True if left operand is greater than or equal to the rightx >= y
<=Less than or equal to: True if left operand is less than or equal to the rightx <= y

Examples​

Greater than (>)​

a = 5
b = 10

print(a > b) # False
print(b > a) # True
print(a > a) # False
print(b > b) # False

Less than (<)​

a = 5
b = 10

print(a < b) # True
print(b < a) # False
print(a < a) # False
print(b < b) # False

Equal to (==)​

a = 5
b = 10

print(a == b) # False
print(b == a) # False
print(a == a) # True
print(b == b) # True

Not equal to (!=)​

a = 5
b = 10

print(a != b) # True
print(b != a) # True
print(a != a) # False
print(b != b) # False

Greater than or equal to (>=)​

a = 5
b = 10

print(a >= b) # False
print(b >= a) # True
print(a >= a) # True
print(b >= b) # True

Less than or equal to (<=)​

a = 5
b = 10

print(a <= b) # True
print(b <= a) # False
print(a <= a) # True
print(b <= b) # True