upgrade
upgrade

🐍Intro to Python Programming

Python Comparison Operators

Study smarter with Fiveable

Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.

Get Started

Why This Matters

Comparison operators are the foundation of decision-making in Python. Every time your program needs to choose between paths—whether to execute a loop, filter data, or validate user input—it relies on comparisons that evaluate to True or False. These operators connect directly to conditionals, loops, data filtering, and algorithm design, making them essential for virtually every program you'll write.

You're being tested on more than just syntax here. Exam questions will ask you to predict output, identify bugs in conditional logic, and distinguish between value equality and object identity—a subtle but critical distinction that trips up many students. Don't just memorize the symbols; know when to use each operator and what kind of comparison it performs.


Equality Operators: Comparing Values

These operators check whether two values are the same or different. They compare the actual data, not where it's stored in memory.

Equal To (==)

  • Returns True when values match—works across numbers, strings, lists, and other data types
  • Type matters in some cases: 5 == 5.0 is True (numeric equality), but "5" == 5 is False (string vs. integer)
  • Foundation of conditional logic—you'll use this in nearly every if statement and data validation check

Not Equal To (!=)

  • Returns True when values differ—the logical opposite of ==
  • Essential for filtering and exclusion logic: checking if user input doesn't match expected values
  • Cleaner than negating equality: x != y is more readable than not (x == y)

Compare: == vs. !=—both compare values, but == confirms matches while != confirms differences. If an exam question asks you to write a condition that excludes a specific value, reach for !=.


Relational Operators: Ordering Values

These operators determine which value is larger or smaller. They're the workhorses of sorting, range checking, and loop control.

Greater Than (>)

  • Returns True when the left value exceeds the right—strictly greater, not equal
  • Core to sorting algorithms and determining maximum values in datasets
  • Works with strings alphabetically: "banana" > "apple" is True based on lexicographic order

Less Than (<)

  • Returns True when the left value is smaller than the right—strictly less, not equal
  • Powers most for and while loops: while i < 10 is a classic pattern
  • String comparison follows Unicode order: uppercase letters come before lowercase

Greater Than or Equal To (>=)

  • Combines > and == into one check—returns True if left is greater or equal
  • Use for inclusive upper bounds: validating that a score meets a minimum threshold
  • Reduces nested conditions: x >= 5 replaces x > 5 or x == 5

Less Than or Equal To (<=)

  • Combines < and == into one check—returns True if left is smaller or equal
  • Use for inclusive ranges: checking if a value falls within acceptable limits
  • Common in boundary conditions: while index <= len(list) - 1 ensures you don't exceed list bounds

Compare: > vs. >=—the difference is whether the boundary value itself passes the test. score > 90 excludes exactly 90; score >= 90 includes it. Exam questions love testing this distinction with off-by-one scenarios.


Identity Operators: Comparing Object References

These operators check whether two variables point to the same object in memory, not just whether they hold equal values. This is fundamentally different from equality.

Identity (is)

  • Returns True only when both variables reference the exact same object—same memory location
  • Critical for None checks: always use x is None, not x == None
  • Beware with mutable types: two lists with identical contents are == but not is (they're separate objects)

Negated Identity (is not)

  • Returns True when variables reference different objects—opposite of is
  • Preferred for checking non-None values: if result is not None: is Pythonic and clear
  • Useful for confirming independent copies: ensuring modifications won't affect the original

Compare: == vs. is== asks "do these have the same value?" while is asks "are these literally the same object?" Two identical lists pass == but fail is. This distinction appears frequently on exams: [1, 2] == [1, 2] is True, but [1, 2] is [1, 2] is False.


Quick Reference Table

ConceptBest Examples
Value equality==, !=
Strict ordering>, <
Inclusive ordering>=, <=
Object identityis, is not
None checkingis None, is not None
Loop conditions<, <=, !=
Data validation==, !=, >=

Self-Check Questions

  1. What is the difference between == and is? Give an example where two variables pass == but fail is.

  2. Which operator would you use to check if a user's age is at least 18? Why choose that operator over a strict comparison?

  3. Compare > and >=: write a condition where using the wrong one would cause an off-by-one error in a loop.

  4. Why should you use x is None instead of x == None when checking for None values?

  5. If a = [1, 2, 3] and b = a, what do a == b and a is b each return? What if b = [1, 2, 3] instead?