Python Greater Than (>) Operator


Python Greater Than Operator

Python Greater Than operator is used to compare if an operand is greater than other operand.

Syntax

The syntax of greater than comparison operator is

operand_1 > operand_2

Greater than operator returns a boolean value. True if operand_1 is greate than operand_2 in value. Otherwise, it returns False. If the operands are sequences like strings, lists, tuple, etc., corresponding elements are compared to compute the result.

Example 1

In this example, we will compare two integers, x and y, and check if x is greater than y.

Python Program

x = 8
y = 7
result = x > y
print(result) #True

x = 5
y = 12
result = x > y
print(result) #False

Output

True
False

For x = 8, and y = 7, x > y computes if 8 is greater than 7, and returns True.

For x = 5, and y = 12, x > y computes if 5 is greater than 12, and returns False.

Example 2: Greater Than Operator with String Operands

You can compare strings in Python using greater than operator. You can compare if a Python String is greater than other string. Python considers lexicographic order of alphabets, or you could say the ASCII value. In that case, the alphabet 'b' is greater than alphabet 'a', and 'c' is greater than 'b', and so on. 'a' is greater than 'A'. The same explanation holds for other possible characters in a string.

While comparing, the first character of each string is compared. If they are equal, next characters are compared, else the result is returned.

In this example, we will compare two strings, x and y, and check if one string is greater than other.

Python Program

x = 'apple'
y = 'banana'
z = 'cherry'
k = 'Apple'
print(y > x) #True
print(y > z) #False
print(x > k) #True

Output

True
False
True

'b' is greater than 'a' and therefore 'banana' is greater than 'apple'. So, y>x returned True.

And when comparing 'banana' and 'cherry', if 'banana' greater than 'cherry', 'b' is not greater than 'c'. Therefore y > z results in False.

'a' is greater than 'A'. Therefore 'apple' > 'Apple' returned True.

Example 3: Greater Than Operator with Lists

As we have compared strings, we can compare Python Lists too. And the process of comparison for Lists happens in the same way as that of strings. Of course, it happens the same way for all the sequences.

In this example, we will compare if a list is greater than other list.

Python Program

x = [41, 54, 21]
y = [98, 8]
z = [41, 54, 4, 6]
print(x > y) #False
print(y > z) #True
print(x > z) #True

Output

False
True
True

[41, 54, 21] greater than [98, 8] first compares the elements 41 and 98. 41 is not greater than 98. Comparison ends here and the result is straight away False and the operator returns False.

[98, 8] greater than [41, 54, 4, 6] first compares the elements 98 and 41. The result is True and no further comparison is required. The operator returns True.

[41, 54, 21] greater than [41, 54, 4, 6] first compares the elements 41 and 41. No result. Then 54 and 54 are compared. Still no result. Then 21 and 4 are compared. This returns True for 21 > 4 .

Summary

In this tutorial of Python Examples, we learned how to compare two values or sequences like strings, lists, etc., using greater than comparison operator.