Skip to content

Comparison Operators

Comparison operators compare values and return True or False.

Operator Summary

Operator Description Example Result
== Equal to 3 == 3 True
!= Not equal to 3 != 2 True
< Less than 3 < 5 True
> Greater than 3 > 5 False
<= Less than or equal 3 <= 3 True
>= Greater than or equal 5 >= 3 True

Numeric Comparison

print(5 > 3)    # True
print(5 < 3)    # False
print(5 >= 5)   # True
print(5 <= 4)   # False
print(5 == 5)   # True
print(5 != 5)   # False

Integer and Float

Python handles mixed numeric types:

print(1 == 1.0)   # True
print(1 < 1.0)    # False
print(1 <= 1.0)   # True

Boolean Comparison

Booleans can be compared:

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

Chained Comparisons

Python allows chaining comparisons:

print(1 < 2 < 3)           # True
print(1 < 2 < 3 < 4 < 5)   # True
print(1 < 5 > 3)           # True (1 < 5 and 5 > 3)

This is equivalent to:

print(1 < 2 and 2 < 3)     # True

Practical Example

age = 25
if 18 <= age <= 65:
    print("Working age")

Comparison Gotchas

Floating-Point Precision

print(0.1 + 0.2 == 0.3)   # False!
print(0.1 + 0.2)          # 0.30000000000000004

Use approximate comparison:

import math
print(math.isclose(0.1 + 0.2, 0.3))  # True

Comparison Order

# These are equivalent
print(10 <= 20 == 30 > 40)
print((10 <= 20) and (20 == 30) and (30 > 40))  # False

Equality vs Identity

Operator Compares Example
== Values [1,2] == [1,2]True
is Identity [1,2] is [1,2]False

See Identity Operators for details.

Comparison with Different Types

Numeric Types Mix

print(1 == 1.0 == True)  # True (True is 1)
print(0 == False)         # True

String vs Number

# Python 3: Cannot compare
print("5" > 3)  # TypeError

None Comparison

print(None == None)  # True
print(None is None)  # True (preferred)

Use in Control Flow

if score >= 90:
    grade = 'A'
elif score >= 80:
    grade = 'B'
elif score >= 70:
    grade = 'C'
else:
    grade = 'F'

Summary

  • == compares values, is compares identity
  • Chained comparisons: a < b < c means a < b and b < c
  • Be careful with float comparison (use math.isclose)
  • Cannot compare incompatible types in Python 3