Python Greater Than Operator: Usage and Pitfalls
Learn how Python's greater-than operator compares numbers, strings, sequences, sets, and custom objects, including chained comparisons and common pitfalls.

The Python greater than operator (>) returns True when the left operand is greater than the right operand, and False otherwise for built-in types. For custom classes, __gt__ may return any value, and Python uses that value's truthiness in boolean contexts. It is one of the six comparison operators in the language, and its behavior depends on the types of the operands. This article explains how > behaves across built-in types, how chained comparisons work, and how to define custom behavior with __gt__.
Basic Syntax and Return Value
The operator is binary and infix, placed between two expressions. With built-in types, the result is always a boolean (True or False). For numbers, the comparison is numeric:
print(5 > 3) # True
print(2 > 4) # False
print(3.5 > 3) # TrueIntegers and floats support ordering, but complex numbers do not; > on complex values raises a TypeError. The comparison is defined by the numeric value, not by type identity. For example, 1 > 1.0 is False because both represent the same value.
Comparing Strings and Bytes
Strings are compared lexicographically using the Unicode code point of each character. This means "apple" > "banana" is False because 'a' (U+0061) is less than 'b' (U+0062). The comparison proceeds character by character until a difference is found. If one string is a prefix of the other, the shorter string is considered smaller:
print("apple" > "app") # True
print("Zebra" > "apple") # False (uppercase letters have lower code points)Bytes objects follow the same lexicographic order but compare byte values directly. Mixing str and bytes with > raises a TypeError in Python 3, which is a deliberate design choice to avoid ambiguous implicit conversions.
Comparing Sequences and Collections
Lists and tuples are compared element-wise. The first differing element determines the result. If all elements are equal, the longer sequence is considered greater. This behavior is consistent with the natural ordering of sequences:
print([1, 2, 3] > [1, 2, 2]) # True
print((1, 2) > (1, 2, 0)) # FalseDictionaries do not support > because mappings have no natural ordering; comparing dictionaries raises a TypeError. Sets are different: the > operator is defined as a proper-superset test, not an ordering comparison. For example, {1, 2} > {1} is True, while {1, 2} > {1, 2} is False. Use >= or issuperset() for a non-strict superset check.
Chained Comparisons
Python allows chaining comparison operators, so a > b > c is equivalent to a > b and b > c. The middle operand is evaluated only once, and the comparison short-circuits if the first part is False. This is more readable and often more efficient than writing the and form explicitly:
x = 15
print(10 < x < 20) # True
print(10 > x > 20) # FalseChaining works with any combination of comparison operators, including >, <, >=, <=, ==, and !=. This is useful for range checks and boundary validation.
Overloading > with __gt__
For custom classes, you can define the behavior of > by implementing the __gt__ method. This method takes two arguments (self and other) and should return a boolean or any value that can be truth-tested. If __gt__ is not defined or returns NotImplemented, Python tries the reflected method __lt__ on the right operand. If both sides cannot handle the comparison, a TypeError is raised.
class Score:
def __init__(self, value):
self.value = value
def __gt__(self, other):
if isinstance(other, Score):
return self.value > other.value
return NotImplemented
s1 = Score(90)
s2 = Score(75)
print(s1 > s2) # TrueReturning NotImplemented tells Python to try the reflected operation on the other operand. This pattern is important for maintaining type safety and avoiding silent type coercion.
Common Pitfalls and Type Errors
Comparing incompatible types often raises TypeError. For example, 5 > "3" fails because there is no natural ordering between int and str. Similarly, comparing None with a number raises TypeError unless you explicitly handle None.
Another pitfall is comparing floating-point values that appear equal but differ due to precision. For example, 0.1 + 0.2 > 0.3 is True because of binary floating-point representation. Use math.isclose() for approximate equality, but for > the raw comparison is still valid for ordering, though not for equality checks.
When working with NumPy arrays, > returns an element-wise boolean array, not a single boolean. This can lead to unexpected behavior in if statements, which require a single boolean. Use .any() or .all() to reduce the array.
Performance and Maintainability Considerations
For large lists or tuples, > compares elements until a difference is found, so the worst-case cost is O(n). If comparisons are frequent and elements are expensive to compare, consider comparing a derived key or cached value instead of repeatedly scanning large collections.
For custom classes, avoid defining __gt__ with side effects or expensive work. Comparison operators should be pure and deterministic because they are often used in sorting and filtering. If your comparison logic is complex, implement a key function or a separate comparator method instead of relying on operator overloading.
Python's version compatibility is stable for comparison operators; the behavior described here applies to Python 3.x. In Python 2, comparing different types (e.g., int and str) used an arbitrary but consistent order, which was removed in Python 3 to prevent subtle bugs. Always test your comparisons across the Python versions you support, especially if you rely on type-specific ordering.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.