Study smarter with Fiveable
Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.
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.
These operators check whether two values are the same or different. They compare the actual data, not where it's stored in memory.
==)True when values match—works across numbers, strings, lists, and other data types5 == 5.0 is True (numeric equality), but "5" == 5 is False (string vs. integer)if statement and data validation check!=)True when values differ—the logical opposite of ==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 !=.
These operators determine which value is larger or smaller. They're the workhorses of sorting, range checking, and loop control.
>)True when the left value exceeds the right—strictly greater, not equal"banana" > "apple" is True based on lexicographic order<)True when the left value is smaller than the right—strictly less, not equalfor and while loops: while i < 10 is a classic pattern>=)> and == into one check—returns True if left is greater or equalx >= 5 replaces x > 5 or x == 5<=)< and == into one check—returns True if left is smaller or equalwhile index <= len(list) - 1 ensures you don't exceed list boundsCompare: > 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.
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.
is)True only when both variables reference the exact same object—same memory locationNone checks: always use x is None, not x == None== but not is (they're separate objects)is not)True when variables reference different objects—opposite of isif result is not None: is Pythonic and clearCompare: == 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.
| Concept | Best Examples |
|---|---|
| Value equality | ==, != |
| Strict ordering | >, < |
| Inclusive ordering | >=, <= |
| Object identity | is, is not |
| None checking | is None, is not None |
| Loop conditions | <, <=, != |
| Data validation | ==, !=, >= |
What is the difference between == and is? Give an example where two variables pass == but fail is.
Which operator would you use to check if a user's age is at least 18? Why choose that operator over a strict comparison?
Compare > and >=: write a condition where using the wrong one would cause an off-by-one error in a loop.
Why should you use x is None instead of x == None when checking for None values?
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?