Comparison Operators in Python: Complete Guide
Python comparison operators evaluate two values and return a boolean result—either True or False. These six operators form the foundation of conditional logic and are essential for controlling program flow. Understanding when to use each operator, especially the distinction between == and is, is crucial for writing correct Python code.
Key Takeaways
- Python has six comparison operators:
==,!=,<,>,<=,>= - All comparison operators return boolean values:
TrueorFalse ==checks value equality;ischecks object identity in memory- String comparisons use lexicographical (alphabetical) order
- Always use
iswhen comparing withNone, not==
The Six Comparison Operators
Python provides exactly six comparison operators that work on numbers, strings, and any comparable objects.
| Operator | Name | Example | Result |
|---|---|---|---|
== | Equal to | 10 == 10 | True |
!= | Not equal to | 10 != 5 | True |
< | Less than | 5 < 10 | True |
> | Greater than | 10 > 5 | True |
<= | Less than or equal to | 10 <= 10 | True |
>= | Greater than or equal to | 10 >= 5 | True |
Every comparison operator always returns a boolean value—never anything else. This predictability makes them reliable building blocks for if statements, while loops, and other control flow structures.
Comparing Numbers and Strings
Comparison operators work seamlessly with both numeric and string data types, but the semantics differ slightly between them.
Comparing Numeric Values
When comparing numbers, the operators behave exactly as you'd expect from mathematics:
print(10 > 5) # Output: True
print(10 == 10.0) # Output: True (Python treats int and float equally)
print(3.14 <= 3) # Output: False
print(-5 != 0) # Output: True
Python automatically handles comparisons between integers and floats. The value 10 and 10.0 compare as equal because they represent the same quantity.
Comparing Strings
When comparing strings, Python uses lexicographical order—essentially alphabetical ordering based on character codes:
print("apple" < "banana") # Output: True
print("hello" == "world") # Output: False
print("Python" > "python") # Output: False (uppercase < lowercase in ASCII)
print("cat" != "dog") # Output: True
Uppercase letters come before lowercase letters in ASCII ordering, so "Python" is actually less than "python". Always be mindful of case when comparing strings.
Understanding == vs. is: Equality vs. Identity
This is the most critical distinction in Python comparison. Many bugs arise from confusing these two operators.
The Core Difference
== (Equality Operator): Checks if the values of two operands are equal.
is (Identity Operator): Checks if two variables reference the exact same object in memory.
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = list1
print(list1 == list2) # Output: True (same values)
print(list1 is list2) # Output: False (different objects in memory)
print(list1 is list3) # Output: True (same object reference)
In this example, list1 and list2 contain identical elements, so == returns True. However, they are two separate list objects stored at different memory addresses, so is returns False. By contrast, list3 is assigned the reference to list1, so both variables point to the same object in memory, making is return True.
When to Use is: The None Case
The most important real-world use of is is when checking for None:
result = None
if result is None:
print("Result is None")
# WRONG - don't do this:
if result == None:
print("This also works, but is not Pythonic")
The Python style guide (PEP 8) explicitly recommends using is for None comparisons. Since there is only one None object in the entire Python runtime, is and == will always produce the same result, but is is faster and conveys intent more clearly.
Comparing Complex Objects
Comparison behavior varies depending on the data type:
# Tuples compare element-by-element
print((1, 2) == (1, 2)) # Output: True
# Sets compare membership, not order
print({1, 2} == {2, 1}) # Output: True
# Dictionaries compare keys and values
print({"a": 1} == {"a": 1}) # Output: True
# Custom objects need special methods (covered in OOP chapters)
Frequently Asked Questions
What's the difference between == and is in practical code?
== compares values; is compares object identity. Use == for almost all comparisons. Use is only when you specifically need to check if two variables reference the exact same object—particularly when comparing with None. In 95% of code, you'll use ==.
Can I chain comparison operators in Python?
Yes, Python supports chaining: 0 < x < 10 is equivalent to (0 < x) and (x < 10). This is more readable and evaluates x only once. Example: if 18 <= age <= 65: print("Working age").
Why do uppercase and lowercase letters compare differently?
Python uses ASCII/Unicode values for string comparison. Uppercase letters (65-90) come before lowercase letters (97-122). To do case-insensitive comparison, convert to the same case first: "Python".lower() == "python".lower().
What happens if I compare incompatible types like a number and a string?
Python 3 raises a TypeError if you try to compare incompatible types: print(5 > "hello") throws an error. This is intentional—it prevents accidental logical errors.
Which operator should I use: == or is?
Default to ==. Use is only for None, True, False, or when you specifically need identity checks. For normal value comparisons, == is always the right choice.