Operator Functions in Python | Set 1

1. add (a, b) : — This function returns the addition of the given arguments. 
Operation —  a + b.

2. sub (a, b) : — This function returns the difference of the given arguments. 
Operation —  a — b.

3. mul (a, b) : — This function returns the product of the given arguments. 
Operation —  a * b.

# Python code to demonstrate how it works
# add (), sub (), mul ()

  
# import operator module

import operator

 
# Initializing variables

a = 4

 

b = 3

 
# using add () to add two numbers

print ( “The addition of numbers is:” , end = " "); 

print (operator.add (a, b))

 
# using sub () to subtract two numbers

print ( "The difference of numbers is:" , end = " "); 

print (operator.sub (a, b))

 
# using mul () to multiply two numbers

print ( "The product of numbers is:" , end = " "); 

print (operator.mul (a, b))

Output:

 The addition of numbers is: 7 The difference of numbers is: 1 The product of numbers is: 12 

4. truediv (a, b) : — This function returns the separation of the given arguments. 
Operation —  a / b.

5. floordiv (a, b) : — This function also returns the division of the given arguments. But the value is an intermediate value, that is, returns the largest small integer
Operation —  a // b.

6. pow (a, b) : — This function returns raising to the power of the given arguments. 
Operation —  a ** b.

7. mod (a, b) : — this function returns the module of the given arguments. 
Operation — % b.

# Python code to demonstrate how it works
# truediv (), floordiv (), pow (), mod ()

 
# import operator module

import operator

 
# Initializing variables

a = 5

 

b = 2

 
# using truediv () to separate two numbers

print ( " The true division of numbers is: " , end = ""); 

print (operator.truediv (a, b))

 
# using floordiv () to divide two numbers

print ( "The floor division of numbers is:" , end = " "); 

print (operator.floordiv (a, b))

 
# using pow () to power two numbers

print ( "The exponentiation of numbers is:" , end = ""); 

print (operator. pow (a, b))

 
# using mod () to get a two-number module

print ( "The modulus of numbers is:" , end = ""); 

print (operator.mod (a, b))

Output:

 The true division of numbers is: 2.5 The floor division of numbers is: 2 The exponentiation of numbers is: 25 The modulus of numbers is: 1 

8. lt (a, b) : — This function is used to check if a is less than b or not . Returns true if a is less than b, otherwise returns false. 
Operation —  a & lt; b .

9. le (a, b) : — This function is used to check if ab is less than or equal to . Returns true if a is less than or equal to b, otherwise returns false. 
Operation —  a & lt; = b .

10. eq (a, b) : — This function is used to check if ab is equal to or not . Returns true if a is equal to b, otherwise returns false. 
Operation —  a == b .

# Python code to demonstrate how it works
# lt (), le () and eq ()

 
# import statement module

import operator

 
# Initializing variables

a = 3

  

b = 3

 
# using lt () to check if a is less than b

if (opera tor.lt (a, b)):

print ( "3 is less than 3" )

else : print ( "3 is not less than 3" )

 
# using le () to check if a is less than or equal to b

if (operator.le (a, b)):

print ( "3 is less than or equal to 3" )

else : print ( "3 is not less than or equal to 3 " )

  
# using eq () to check if ab is equal

if (operator.eq (a, b)):

  print ( "3 is equal to 3" )

else : print ( "3 is not equal to 3" )

Output:

 3 is not less than 3 3 is less than or equal to 3 3 is equal to 3  

11. gt (a, b) : — This function is used to check if a is greater than b or not . Returns true if a is greater than b, otherwise returns false. 
Operation —  a & gt; b .

12. ge (a, b) : — This function is used to check whether a is greater than or equal to a or less than b . Returns true if a is greater than or equal to b, otherwise returns false. 
Operation —  a & gt; = b .

13. ne (a, b) : — This function is used to check if a is not equal to b or . Returns true if a is not equal to b, otherwise returns false. 
Operation —  huh! = B.

# Python code to demonstrate how it works
# gt (), ge () and ne ()

 
# import statement module

import operator

 
# Initializing variables

a = 4

 

b = 3

  
# using gt () to check if a is greater than b

if (operator.gt (a, b)): < / code>

print ( "4 is greater than 3" )

else : print ( "4 is not greater than 3 " )

  
# using ge () to check if ab is greater than or equal to ab

if (operator.ge (a, b)):

print ( "4 is greater than or equal to 3" )

else : print ( "4 is not greater than or equal to 3 " )

  
# using ne () to check that a is not equal to b

if (operator.ne (a, b)):

  print ( "4 is not equal to 3" )

else : print ( "4 is equal to 3" )

Output:

 4 is greater than 3 4 is greater than or equal to 3 4 is not equal to 3 

This article is courtesy of Manjeet Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.