Python Basics — Comparison Operators

2 minute read

Comparison Operators

Comparison operators compare two values and return a boolean result (True or False). They’re essential for making decisions in your code.

Operator Reference

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

Basic Examples

x = 10
y = 20

print(x == y)   # False
print(x != y)   # True
print(x < y)    # True
print(x > y)    # False
print(x <= 10)  # True
print(x >= 15)  # False

== vs = (Common Confusion)

  • = is the assignment operator — it stores a value
  • == is the comparison operator — it checks equality
x = 5     # Assign 5 to x
x == 5    # Compare x with 5 → True

Comparing Different Types

# int vs float — Python compares the values
print(5 == 5.0)    # True
print(5 != 5.0)    # False

# int vs bool — True is 1, False is 0
print(1 == True)    # True
print(0 == False)   # True

# String vs number — always False
print("5" == 5)     # False (different types)

String Comparison

Strings are compared lexicographically (dictionary order) using Unicode values:

print("apple" < "banana")    # True ("a" comes before "b")
print("cat" > "car")         # True ("t" > "r")
print("Hello" == "hello")    # False (case-sensitive!)
print("abc" < "abd")         # True ("c" < "d")

Case-insensitive comparison:

name1 = "Alice"
name2 = "alice"

print(name1 == name2)                  # False
print(name1.lower() == name2.lower())  # True

Chained Comparisons

Python supports chained comparisons — a feature many languages don’t have:

x = 15

# Instead of: x > 10 and x < 20
print(10 < x < 20)     # True

# Multiple chains
print(1 < 5 < 10)      # True
print(1 < 5 > 3)       # True (1 < 5 AND 5 > 3)
print(1 < 5 < 3)       # False (5 < 3 is False)

# Equality chains
print(1 == 1 == 1)      # True
print(1 == 1 == 2)      # False

This is equivalent to and-ing the individual comparisons:

# These are the same:
print(10 < x < 20)
print(10 < x and x < 20)

Comparing Collections

# Lists — element by element
print([1, 2, 3] == [1, 2, 3])    # True
print([1, 2, 3] == [1, 2, 4])    # False
print([1, 2] < [1, 3])           # True (compares at index 1: 2 < 3)

# Tuples — same behavior
print((1, 2) == (1, 2))          # True
print((1, 2) < (1, 3))           # True

Common Mistakes

Mistake Example Fix
Using = instead of == if x = 5: → SyntaxError Use if x == 5:
Case-sensitive comparison "Hello" == "hello" → False Use .lower() on both
Comparing types directly "5" == 5 → False Convert first: int("5") == 5
Float precision 0.1 + 0.2 == 0.3 → False Use math.isclose()

Float Comparison Best Practice

import math

# ❌ Direct comparison may fail
print(0.1 + 0.2 == 0.3)           # False

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

Summary

  • Comparison operators return True or False
  • Use == for equality, = for assignment — don’t confuse them
  • Python supports chained comparisons: 1 < x < 10
  • Strings are compared lexicographically (case-sensitive)
  • Use math.isclose() for float comparisons
  • Compare different types carefully — "5" == 5 is False

Next up: Logical Operators →

Updated: