Python greater than or equal comparison is done with >=
, the greater than or equal operator. The result of the operation is a Boolean.
>>> 8 >= 3
True
>>> 8 >= 8
True
>>> 3 >= 8
False
The most common use of the greater than or equal operator is to decide the flow of the application:
a, b = 3, 5
if a >= b:
print('a is greater than or equal to b')
else:
print('a is not greater than or equal to b')
Comparing Strings in Python
You can use the greater than or equal operator to compare strings. It will use lexicographical order to do the comparison, meaning that it compares each item in order. It first compares the first two chars in each string, if they are equal it goes to the next char, and so on.
>>> 'bb' >= 'aa'
True
>>> 'ab' >= 'aa'
True
>>> 'ab' >= 'ab'
True
>>> 'ab' >= 'ac'
False
>>> 'B' >= 'a'
False
>>> 'a' >= 'B'
True
Comparing Lists in Python
It’s the same lexicographical comparison as with Strings, each item is compared in the order that it is in the list.
>>> [2, 3] >= [1, 2]
True
>>> [2, 3] >= [2, 1]
True
>>> [2, 3] >= [2, 3]
True
>>> [2, 3] >= [2, 4]
False
Comparing Tuples in Python
It’s the same lexicographical comparison as with Strings, each item is compared in the order that it is in the tuple.
>>> (2, 3) >= (1, 2)
True
>>> (2, 3) >= (2, 1)
True
>>> (2, 3) >= (2, 3)
True
>>> (2, 3) >= (2, 4)
False
Comparing Set and Dictionaries in Python
You can not use the greater than or equal operator to compare sets and dictionaries in Python.
>>> set([4]) >= set([3])
False # Doesn't work correctly
>>> {5: 'b', 2: 3} >= {2: 3, 5: 'b'}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>=' not supported between instances of 'dict' and 'dict'