Skip to content

Python Comparison Operators

How to use Python's comparison operators (>, <, ==, !=, >=, <=) to evaluate conditions and return boolean results (True/False) for value comparisons.

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) # 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
Navigation