Home » Python

Python operator.gt() Function with Examples

Python operator.gt() Function: Here, we are going to learn about the operator.gt() function with examples in Python programming language.
Submitted by IncludeHelp, on April 14, 2020

operator.gt() Function

operator.gt() function is a library function of operator module, it is used to perform "greater than operation" on two values and returns True if the first value is greater than the second value, False, otherwise.

Module:

    import operator

Syntax:

    operator.gt(x,y)

Parameter(s):

  • x,y – values to be compared.

Return value:

The return type of this method is bool, it returns True if x is greater than y, False, otherwise.

Example 1:

# Python operator.gt() Function Example

import operator

# integers
x = 10
y = 20

print("x:",x, ", y:",y)
print("operator.gt(x,y): ", operator.gt(x,y))
print("operator.gt(y,x): ", operator.gt(y,x))
print("operator.gt(x,x): ", operator.gt(x,x))
print("operator.gt(y,y): ", operator.gt(y,y))
print()

# strings
x = "Apple"
y = "Banana"

print("x:",x, ", y:",y)
print("operator.gt(x,y): ", operator.gt(x,y))
print("operator.gt(y,x): ", operator.gt(y,x))
print("operator.gt(x,x): ", operator.gt(x,x))
print("operator.gt(y,y): ", operator.gt(y,y))
print()

# printing the return type of the function
print("type((operator.gt(x,y)): ", type(operator.gt(x,y)))

Output:

x: 10 , y: 20
operator.gt(x,y):  False
operator.gt(y,x):  True
operator.gt(x,x):  False
operator.gt(y,y):  False

x: Apple , y: Banana
operator.gt(x,y):  False
operator.gt(y,x):  True
operator.gt(x,x):  False
operator.gt(y,y):  False

type((operator.gt(x,y)):  <class 'bool'>

Example 2:

# Python operator.gt() Function Example
import operator

# input two numbers
x = int(input("Enter first number : "))
y = int(input("Enter second number: "))

# printing the values
print("x:",x, ", y:",y)

# comparing
if operator.gt(x,y):
  print(x, "is greater than", y)
else:
  print(x, "is not greater than", y)  

Output:

RUN 1:
Enter first number : 20
Enter second number: 10
x: 20 , y: 10
20 is greater than 10

RUN 2:
Enter first number : 10
Enter second number: 20
x: 10 , y: 20
10 is not greater than 20


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.