Operator functions in Python &mathematical operations

Python Methods and Functions

Python has several additional standard library methods for mathematical operations such as arithmetic, logical, relational, bitwise, and so on. Operations. These methods can be found in the operator module.

To use it, we first need to import it into the operator's standard library module. 

 import operator 

In this section we will see some operator functions for bitwise and container operations.








Arithmetic Operations

First, we will see arithmetic control functions. It's like below.

  • add () is used to add two numbers x and y. It does simple addition. This is similar to the x + y operation.
  • sub () is used to subtract y from x. This is similar to the operation x &y.
  • mul () is used to multiply two numbers x and y. This is similar to the x * y operation.
  • truediv () is used to find the result after dividing x by y. This method can return fractional values ​​as a result. This is similar to the x / y operation.
  • floordiv () is used to find the x / y ratio. This is similar to the x // y operation.
  • mod () is used to get the remainder of x / y. This is similar to the x% y operation.
  • pow () is used to find x ^ y. This is similar to the x ** y operation.



Sample Code

 # Arithmetic Operators import operator print ( 'Add:' + str (operator.add (56, 45))) print ('Subtract:' + str (operator.sub (56, 45))) print ('Multiplication:' + str (operator.mul (56 , 45))) print ('True division:' + str (operator.truediv (56, 45))) # same as a / b print ('Floor division:' + str (operator.floordiv (56, 45)) ) #same as a // b print ('Mod:' + str (operator.mod (56, 45))) #same as a% b print ('pow:' + str (operator.pow (5, 3) )) 



Output

 Add: 101 Subtract: 11 Multiplication: 2520 True division: 1.2444444444444445 Floor division: 1 Mod: 11 pow: 125 



Relational Operations

The operator module also contains relational operators such as & lt ;, & lt; =, & gt;, & gt; =, == ,! =.

Operator functions as below &

  • lt () is used to check if x is less than y or not. It's like the operation x & lt; y.
  • le () is used to check if a number x is less than or equal to y or not. It's like the operation x & lt; = y.
  • eq () is used to check if x and y are equal or not. It's like the operation x == y.
  • gt () is used to check if x is greater than y or not. It's like the operation x & gt; y.
  • ge () is used to check if x is greater than or equal to y or not. It's like the operation x & gt; = y.
  • ne () is used to check if x and y are not equal. It's like operation x! = Y.



Sample Code

 # Relational Operators import operator print ('Less Than:' + str (operator.lt (5, 10))) print ('Less Than Equal:' + str (operator.le (10, 10))) print ('Greater Than:' + str (operator.gt (5, 5 ))) print ('Greater Than Equal:' + str (operator.ge (5, 5))) print ('Equal to:' + str (operator.eq (12, 12))) print ('Not Equal to : '+ str (operator.ne (15, 12))) 



Output

 Less Than: True Less Than Equal: Tr ue Greater Than: False Greater Than Equal: True Equal to: True Not Equal to: True 







Get Solution for free from DataCamp guru