Operator Module in Python

Hello programmers, in this tutorial we will learn more about the operator module and its related functions in Python.

The Operator module in Python has many predefined functions which are used for mainly mathematical, comparison, logical, relational, etc. operations by the user. The main advantage of this module is that it reduced the user’s effort in building the same functionalities again and again.

We will see some of the basic functionalities used by the users on a frequent basis.

Arithmetic Operations in Python

add(x,y) – Using this function, we can add any two numbers and get the resultant output.

def addTwoNum(self):
    return operator.__add__(self.num1, self.num2)

Explanation
Here, we have to use the dunder(a magic method that is used for operator overloading) method for the addition of two numbers using the operator module. The following method performs ‘a + b’ where ‘a’ and ‘b’ numbers.

sub(x,y) – Using this function, we can subtract two numbers and get the resultant output.

def subTwoNum(self):
    return operator.__sub__(self.num1, self.num2)

Explanation
Here, we have used the dunder method for the subtraction of two numbers using the operator module. The following method performs ‘a – b’ where ‘a’ and ‘b’ numbers.

mul(x,y) – Using this function, we can multiply two numbers and get the resultant output.

def mulTwoNum(self):
    return operator.__mul__(self.num1, self.num2)

Explanation
Here, we have used the dunder method for the multiplication of two numbers using the operator module. The following method performs ‘a x b’ where ‘a’ and ‘b’ numbers.

mod(x,y) – Using this function, we can multiply two numbers and get the resultant output.

def modTwoNum(self):
    return operator.__mod__(self.num1, self.num2)

Explanation
Here, we have used the dunder method for the modulus of two numbers using the operator module. The following method performs ‘a % b’ where ‘a’ and ‘b’ numbers.

truediv(x,y) – Using this function, we can divide the two numbers and get the resultant output.

def truedivTwoNum(self):
    return operator.__truediv__(self.num1, self.num2)

Explanation
Here, we have used the dunder method for the true division of two numbers using the operator module. The following method performs ‘a / b’ where ‘a’ and ‘b’ numbers.

floordiv(x,y) – Using this function, we can divide two numbers and get the greatest small integer as the resultant output.

def floordivTwoNum(self):
    return operator.__floordiv__(self.num1, self.num2)

Explanation
Here, we have used the dunder method for floor division of two numbers using the operator module. The following method performs ‘a // b’ where ‘a’ and ‘b’ numbers.

pow(x,y) – Using this function, we can get the exponential raise of the number as the resultant output.

def powTwoNum(self):
    return operator.__pow__(self.num1, self.num2)

Explanation
Here, we have used the dunder method for exponents of two numbers using the operator module. The following method performs ‘a ** b’ where ‘a’ and ‘b’ numbers.

Given below is the functional code for the arithmetic operations discussed above.

#Import library
import operator

class useOperatorMod:
    def __init__(self, num1, num2):
        self.num1 = num1
        self.num2 = num2

    def addTwoNum(self):
        return operator.__add__(self.num1, self.num2)
    
    def subTwoNum(self):
        return operator.__sub__(self.num1, self.num2)

    def mulTwoNum(self):
        return operator.__mul__(self.num1, self.num2)
    
    def modTwoNum(self):
        return operator.__mod__(self.num1, self.num2)
    
    def truedivTwoNum(self):
        return operator.__truediv__(self.num1, self.num2)
    
    def floordivTwoNum(self):
        return operator.__floordiv__(self.num1, self.num2)
    
    def powTwoNum(self):
        return operator.__pow__(self.num1, self.num2)

#driver
if __name__ == "__main__":
    num1, num2 = 31, 8
    clsObj = useOperatorMod(num1, num2)
    print(f'The addition of {num1} and {num2} is: {clsObj.addTwoNum()}.')
    print(f'The subtraction of {num1} and {num2} is: {clsObj.subTwoNum()}.')
    print(f'The multiplication of {num1} and {num2} is: {clsObj.mulTwoNum()}.')
    print(f'The modulus of {num1} and {num2} is: {clsObj.modTwoNum()}.')
    print(f'The true division of {num1} and {num2} is: {clsObj.truedivTwoNum()}.')
    print(f'The floor division of {num1} and {num2} is: {clsObj.floordivTwoNum()}.')
    print(f'The power of {num1} and {num2} is: {clsObj.powTwoNum()}.')

Output

The addition of 31 and 8 is: 39.
The subtraction of 31 and 8 is: 23.
The multiplication of 31 and 8 is: 248.
The modulus of 31 and 8 is: 7.
The true division of 31 and 8 is: 3.875.
The multiplication of 31 and 8 is: 248.
The modulus of 31 and 8 is: 7.
The true division of 31 and 8 is: 3.875.
The floor division of 31 and 8 is: 3.
The power of 31 and 8 is: 852891037441.

Relational Operations in Python

lt(x,y) – Using this function, we can check if the number is less than the other number.

def ltTwoNum(self):
    return operator.__lt__(self.num1, self.num2)

Explanation
The ‘lt’ function returns True for the condition ‘a < b’ for any two given numbers ‘a’ and ‘b’.

le(x,y) – Using this function, we can check if the number is less than or equal to the other number.

def leTwoNum(self):
    return operator.__le__(self.num1, self.num2)

Explanation
The ‘le’ function returns True for the condition ‘a <= b’ for any two given numbers ‘a’ and ‘b’.

gt(x,y) – Using this function, we can check if the number is greater than the other number.

def gtTwoNum(self):
    return operator.__gt__(self.num1, self.num2)

Explanation
The ‘gt’ function returns True for the condition ‘a > b’ for any two given numbers ‘a’ and ‘b’.

ge(x,y) – Using this function, we can check if the number is greater than or equal to the other number.

def geTwoNum(self):
    return operator.__ge__(self.num1, self.num2)

Explanation
The ‘ge’ function returns True for the condition ‘a >= b’ for any two given numbers ‘a’ and ‘b’.

eq(x,y) – Using this function, we can check if the number is equal to the other number.

def eqTwoNum(self):
    return operator.__eq__(self.num1, self.num2)

Explanation
The ‘eq’ function returns True for the condition ‘a = b’ for any two given numbers ‘a’ and ‘b’.

Given below is the functional code for the relational operations discussed above.

#Import library
import operator

class useOperatorMod:
    def __init__(self, num1, num2):
        self.num1 = num1
        self.num2 = num2

    def ltTwoNum(self):
        return operator.__lt__(self.num1, self.num2)
    
    def leTwoNum(self):
        return operator.__le__(self.num1, self.num2)
    
    def gtTwoNum(self):
        return operator.__gt__(self.num1, self.num2)
    
    def geTwoNum(self):
        return operator.__ge__(self.num1, self.num2)
    
    def eqTwoNum(self):
        return operator.__eq__(self.num1, self.num2)
    

#driver
if __name__ == "__main__":
    num1, num2 = 31, 8
    clsObj = useOperatorMod(num1, num2)
    print(f'{num1} is less than {num2}: {clsObj.ltTwoNum()}.')
    print(f'{num1} is less than or equal to {num2}: {clsObj.leTwoNum()}.')
    print(f'{num1} is greater than {num2}: {clsObj.gtTwoNum()}.')
    print(f'{num1} is greater than or equal to {num2}: {clsObj.geTwoNum()}.')
    print(f'{num1} is equal to {num2}: {clsObj.eqTwoNum()}.')

Output

31 is less than 8: False.
31 is less than or equal to 8: False.
31 is greater than 8: True.
31 is greater than or equal to 8: True.
31 is equal to 8: False.

Leave a Reply

Your email address will not be published.