The Python greater than or equal to (left>=right
) operator returns True
when its left
operand is not exceeded by its right
operand. When the left
operand is smaller than the right
operand, the >=
operator returns False
. For example, 3>=2
and 3>=3
evaluate to True
, but 2>=3
evaluates to False
.
Examples
Let’s explore a couple of examples regarding the greater than or equal to operator.
Is 3 greater than or equal to 2?
>>> 3 >= 2 True
What about 2 greater than or equal to 3?
>>> 2 >= 3 False
What about 2 greater than or equal to 2?
>>> 2 >= 2 True
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 >>> [1, 2] >= [1, 2] True
Yes!
The list “greater than or equal to” operator iterates over the lists and checks pairwise if the i-th element of the left operand is at least as big as the i-th element of the right operand. You can find a detailed discussion on the greater than or equal to operator with list operands below.
Can you use the greater than or equal to operator on custom objects? Yes!
Python Greater Than or Equal to Overload
To use the “greater than or equal to” operator on custom objects, you can use overloading of the __ge__()
dunder method (short for: greater than or equal to). The method takes two arguments: self
and other
and it returns a Boolean value. You can use the arguments’ attributes to determine if one is greater than or equal to the other.
In the following code, you check if a Person is greater than or equal to the other Person by using the age
attribute as a decision criterion:
class Person: def __init__(self, age): self.age = age def __ge__(self, other): return self.age >= other.age alice = Person(10) bob = Person(12) print(alice >= bob) # False print(bob >= alice) # True print(bob >= bob) # 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
. If you compare a Person
object with itself, the age
attribute is equal, so the result will be True
.
Python Not Greater Than or Equal
To invert the greater than or equal operator >=
, you can use the expression not x >= y
with the meaning “smaller than”. So, you can use the expression x < y
in most cases. Only if you overload the __ge__
dunder method to define your own “greater than or equal” operator could the semantics between not x>= y
and x<y
differ.
>>> x = 10 >>> y = 20 >>> not x >= y True >>> x < y True
Python If Statement Greater Than or Equal to
The Python greater than or equal to >=
operator can be used in an if statement as an expression to determine whether to execute the if branch or not. For example, the if condition x>=3
checks if the value of variable x
is greater than or equal to 3, and if so, enters the if branch.
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 at least 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 entered:
your age: 18 you can vote
Here’s an example execution where the if branch is not entered:
your age: 17 you cannot vote - sorry
Python Chaining Greater Than or Equal
Python allows you to chain the greater than or equal operator. For example, the expression 18 >= x >= 5
would check whether variable x
is between 5 and 18, both included. 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 between 5 and 18 (included).
x = 8 # Is x between 5 and 18? if 18 >= x >= 5: print('yes') # Output: yes
The code enters the if branch because the if condition is fulfilled.
Python Greater Than or Equal to on Lists
The list “greater than or equal to” operator iterates over the lists and checks pairwise if the i-th element of the left operand is greater than or equal to 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] >= [1, 2] 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
.[1, 2] >= [1, 2]
. Python first checks1 >= 1
and then checks2>=2
. Still a tie, so the result isTrue
as they’re equal.
The same method also applies to strings and other sequence types in Python such as tuples.
Is Everything Greater Than or Equal to None?
You cannot use the greater than or equal to 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 |