The Python greater than (left>right
) operator returns True
when its left
operand exceeds its right
operand. When the left
operand is smaller than or equal to the right
operand, the >
operator returns False
. For example, 3>2
evaluates to True
, but 2>3
and 3>3
both evaluate to False
.
Examples
Let’s explore a couple of examples regarding the greater than operator.
Is 3 greater than 2 and 2?
>>> 3 > 2 True
What about 2 greater than 3?
>>> 2 > 3 False
Can you compare collections such as lists?
>>> [1, 2] > [99] False >>> [1, 2] > [0] True >>> [1, 2] > [1, 2, 3] False >>> [1, 2] > [1, 1, 3] True
Yes!
The list “greater than” operator iterates over the lists and checks pairwise if the i-th element of the left operand is greater than the i-th element of the right operand. You can find a detailed discussion on the greater than operator with list operands below in this article.
Can you use the greater than operator on custom objects? Yes!
Python Greater Than on Custom Objects
To use the “greater than” operator on custom objects, you need to define the __gt__()
dunder method that takes two arguments: self
and other
. You can then use attributes of the custom objects to determine if one is greater than the other.
In the following code, you check if a Person is greater than the other Person by using the age
attribute as a decision criterion:
class Person: def __init__(self, age): self.age = age def __gt__(self, other): return self.age > other.age alice = Person(10) bob = Person(12) print(alice > bob) # False print(bob > alice) # True
Because Alice is 10 years old and Bob is 12 years old, the result of alice > bob
is False
and bob > alice
is True
.
Python Greater Than If Statement
The Python greater than >
operator can be used in an if statement as an expression to determine whether to execute the if branch or not. For example, the greater than if condition x>3
checks if the value of variable x
is greater than 3, and if it is, the if branch is entered.
The following code asks the user to input their age using the input()
function. It then checks if the user input, when converted to an integer using int()
, is larger than 18. If so, it enters the if branch. If not, it enters the else branch.
x = int(input('your age: ')) if x > 18: print('you can vote') else: print('you cannot vote - sorry')
Here’s an example execution of this code where the if branch is not entered:
your age: 18 you cannot vote - sorry
Here’s an example execution where the if branch is entered:
your age: 21 you can vote
Python Greater Than But Less Than
Python has a “greater than but less than” operator by chaining together two “greater than” operators. For example, the expression 5 < x < 18
would check whether variable x
is greater than 5 but less than 18. Formally, the expression x < y < z
is just a shorthand expression for (x < y) and (y < z)
.
Here’s a minimal example that checks if variable x is greater than 2 but less than 18:
x = 8 # Is x greater than 2 but less than 18? if 2 < x < 18: print('yes') # Output: yes
The code enters the if branch because the if condition is fulfilled.
Python Greater Than Lists
The list “greater than” operator iterates over the lists and checks pairwise if the i-th element of the left operand is greater than the i-th element of the right operand.
>>> [1, 2] > [99] False >>> [1, 2] > [0] True >>> [1, 2] > [1, 2, 3] False >>> [1, 2] > [1, 1, 3] True
[1, 2] > [99]
. Python first checks1 > 99
which isFalse
, so it immediately returnsFalse
.[1, 2] > [0]
. Python first checks1 > 0
which isTrue
.[1, 2] > [1, 2, 3]
. Python first compares 1 and 1—a tie! So, it moves on to the second elements 2 and 2—tie again! So, it moves to the third elements as a tie-breaker. But only the second list has a third element so it is considered greater than the first and the result of the operation isFalse
.[1, 2] > [1, 1, 3]
. Python compares elements 1 and 1—a tie! But then it compares the second elements 2 and 1 and determines that the first is greater than the second, so the result isTrue
.
The same method also applies to strings and other sequence types in Python such as tuples.
Is Everything Greater Than None?
You cannot use the greater than operator with None
as one of its operands. Python 3 expects that both operands implement the comparable interface, but the None type does not. That’s why Python raises a TypeError
if you try to compare variables with None
.
>>> 21 > None Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> 21 > None TypeError: '>' not supported between instances of 'int' and 'NoneType'
Comparison Operators
Comparison operators are applied to comparable objects and they return a Boolean value (True
or False
).
Operator | Name | Description | Example |
---|---|---|---|
> | Greater Than | Returns True if the left operand is greater than the right operand | 3 > 2 == True |
< | Less Than | Returns True if the left operand is smaller than the right operand | 3 < 2 == False |
== | Equal To | Returns True if the left operand is the same as the right operand | (3 == 2) == False |
!= | Not Equal To | Returns True if the left operand is not the same as the right operand | (3 != 2) == True |
>= | Greater Than or Equal To | Returns True if the left operand is greater than or equal to the right operand | (3 >= 3) == True |
<= | Less Than or Equal To | Returns True if the left operand is less than or equal to the right operand | (3 <= 2) == False |