๐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.
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
Truewhen values matchโworks across numbers, strings, lists, and other data types - Type matters in some cases:
5 == 5.0isTrue(numeric equality), but"5" == 5isFalse(string vs. integer) - Foundation of conditional logicโyou'll use this in nearly every
ifstatement and data validation check
Not Equal To (!=)
- Returns
Truewhen 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 != yis more readable thannot (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
Truewhen 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"isTruebased on lexicographic order
Less Than (<)
- Returns
Truewhen the left value is smaller than the rightโstrictly less, not equal - Powers most
forandwhileloops:while i < 10is a classic pattern - String comparison follows Unicode order: uppercase letters come before lowercase
Greater Than or Equal To (>=)
- Combines
>and==into one checkโreturnsTrueif left is greater or equal - Use for inclusive upper bounds: validating that a score meets a minimum threshold
- Reduces nested conditions:
x >= 5replacesx > 5 or x == 5
Less Than or Equal To (<=)
- Combines
<and==into one checkโreturnsTrueif 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) - 1ensures 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
Trueonly when both variables reference the exact same objectโsame memory location - Critical for
Nonechecks: always usex is None, notx == None - Beware with mutable types: two lists with identical contents are
==but notis(they're separate objects)
Negated Identity (is not)
- Returns
Truewhen variables reference different objectsโopposite ofis - 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
| 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 | ==, !=, >= |
Self-Check Questions
-
What is the difference between
==andis? Give an example where two variables pass==but failis. -
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 Noneinstead ofx == Nonewhen checking for None values? -
If
a = [1, 2, 3]andb = a, what doa == banda is beach return? What ifb = [1, 2, 3]instead?