Comparison operators in Python test the relationship between two values and return a boolean result, either True or False. They are the decision-making foundation of every program. Without comparison operators, you could not write an if-statement that checks whether a score passes a threshold, a while-loop that runs until a counter reaches zero, or a filter that selects only the items that match a condition. Python provides six comparison operators: one for equality, one for inequality, and four for ordering comparisons. All six work across numbers, strings, and most built-in collection types, and they share the same precedence level.
This article follows the introduction to operators and the deep dives into arithmetic and assignment operators. Comparison operators are the natural next step because they produce the boolean values that logical operators combine into complex conditions. The concepts here also connect back to Python data types explained, since comparison behavior varies across integers, floats, strings, and collections. Once you have both comparison and logical operators under your belt, you will be ready for the control flow section where if-statements and while-loops put these operators to work. The boolean results from comparison operators feed directly into the patterns shown in Python boolean data type.
Equality and inequality
The equality operator, written as a double equals sign, compares two values and returns True when they are the same and False when they differ. For numbers, equality means the same numeric value regardless of type: the integer 5 and the float 5.0 compare as equal. For strings, equality means the same sequence of characters in the same case. For collections like lists and tuples, equality means the same elements in the same order. Python checks equality by value, not by identity, which means two separate list objects with the same contents are considered equal even though they occupy different locations in memory.
print(5 == 5) # True
print(5 == 5.0) # True (int and float)
print("hello" == "hello") # True
print([1, 2] == [1, 2]) # True (separate objects, same contents)
print(5 == 3) # FalseThe inequality operator, written as an exclamation mark followed by an equals sign, is the logical opposite of equality. It returns True when the two values are different and False when they are the same. Every equality comparison has an equivalent inequality comparison with the opposite result.
A critical beginner distinction is the difference between the double equals and the single equals. The double equals compares values. The single equals assigns values. Python raises a SyntaxError for bare assignment inside an if-condition, which prevents the bug from silently producing wrong results.
Ordering operators
The four ordering operators test less-than, greater-than, less-than-or-equal, and greater-than-or-equal relationships. For numbers, the order is the standard mathematical number line. For strings, the order is lexicographic, meaning Python compares the Unicode code point of each character one position at a time until it finds a difference. The string "apple" is less than "banana" because the code point of lowercase a is 97 and the code point of lowercase b is 98. Uppercase letters all have lower code points than lowercase letters, so "Zebra" is less than "apple" in a Python comparison even though it would appear later in a case-insensitive dictionary.
print(5 < 10) # True
print(3.5 > 2.1) # True
print(7 <= 7) # True (equal, so less-than-or-equal passes)
print("dog" < "doghouse") # True (shorter string is less)Lists and tuples are compared element by element, left to right, using the same rules. The first pair of elements that differ determines the result. If all corresponding elements are equal, the shorter collection is considered less than the longer one. This means a list with fewer elements is less than a longer list that starts with the same values. Dictionaries do not support ordering comparisons, and comparing a list to a tuple with equality always returns False because they are different types, even when they contain the same values.
How chained comparisons work
Python supports chained comparisons, a feature that not every programming language offers. Instead of writing two separate comparisons joined by the and keyword, you can write them together and Python evaluates the chain as a single expression. The interpreter breaks the chain into pairwise comparisons joined by and, but it evaluates the middle expression only once. This is both cleaner to read and slightly more efficient when the middle operand is a function call or a complex expression that you do not want to compute twice.
Comparison chaining works with any combination of comparison operators, not just less-than. You can write a chain using equality to check that three variables hold the same value, or mix operators to express relationships between multiple values. The operators in a chain do not need to point in the same direction, and Python does not enforce any logical relationship between them. It simply evaluates each adjacent pair and combines the results with and.
Comparing values across different types
Python lets you compare values of different numeric types. Comparing an integer to a float works as expected because Python converts the integer to a float internally before comparing. Comparing numbers to strings, however, always returns False for equality and raises a TypeError for ordering operators. This was a deliberate change from Python 2, where cross-type ordering comparisons were allowed but produced arbitrary and confusing results. In Python 3, the language forces you to be explicit about conversions, which catches bugs where you accidentally compare incompatible types.
The None value is a singleton, and the recommended way to check for None is with the identity operator is rather than the equality operator. Writing an identity check for None is both more precise and slightly faster, and it is the convention used throughout the Python standard library and most third-party packages. Identity operators are covered in detail in a later article on identity and membership operators.
Rune AI
Key Insights
- Python has six comparison operators: equal, not equal, less than, greater than, less than or equal, and greater than or equal.
- Comparison operators always return a boolean True or False.
- Python supports chained comparisons that evaluate multiple conditions together.
- Strings compare lexicographically by Unicode code point; lists and tuples compare element by element.
- Never confuse the equality operator with the assignment operator; using assignment where comparison is intended causes bugs.
Frequently Asked Questions
What does `==` do in Python?
How does Python compare strings?
Can I chain multiple comparisons together?
Conclusion
Comparison operators let your program make decisions by testing relationships between values. Equality, inequality, and ordering operators return True or False, and chained comparisons let you write range checks that read like natural mathematical notation.
More in this topic
Nested Lists in Python
Learn how to create, access, and modify nested lists in Python. Build grids and matrices, and understand how shallow copies affect multi-dimensional data.
List Comprehensions in Python
Master Python list comprehensions to create, filter, and transform lists in a single line. Learn the syntax, common patterns, and when to use a regular loop instead.
Copy Python Lists
Learn how to copy Python lists using slicing, the copy method, the list constructor, and the copy module. Understand shallow vs deep copies.