Explore

Edit

Edit

# Standard Operator Functions in Python | Operators as Functions

Published on 09 Mar 2020

As you may know, Python has several inbuilt operators(arithmetic, bitwise, logical, assignment, relational etc) which are used to perform various mathematical operations like addition, subtraction, division etc. Similarly, Python has many predefined functions that can be used as a substitute for these in-built operators and these functions are called operators functions in Python. The operator module consists of these operator functions and hence you need to import this module before you use these functions in your code.For example,mul() function in the operator module corresponds to the * operator. This means both of these perform the same operation of multiplying two operands.Let us now discuss some of these operator functions in Python.

## List of Arithmetic Operator Functions in Python

Let us assume two operands a & b. Below is the list of operator functions in Python used to perform various arithmetic operations.

Operator Function
Operation
Description
Equivalent inbuilt Operator
This function returns the addition of the given arguments a & b
sub (a, b)
Subtraction
This function returns the difference of the given arguments a & b
– (Subtraction arithmetic operator)
mul (a, b)
Multiplication
This function returns the product of the given arguments a & b
* (Multiplication arithmetic Operator)
truediv (a, b)
Division
This function returns the quotient on dividing a by b
/ (Division arithmetic operator)
floordiv (a,b)
Floor Division
This function also returns the quotient on dividing a by b. But unlike truediv, floordiv returns only integer values.
// (Floor division arithmetic operator)
mod (a, b)
Modulus
This function returns the remainder obtained on dividing a by b
% (Modulo division operator)
pow (a, b)
Power
This function returns the value when the operand a is raised to the power of operand b.
** (Exponential arithmetic operator)

﻿

from operator import *
num1 = 40
num2 = 20

#subtraction
print(sub(num1, num2))

#multiplication
print(mul(num1, num2))

#division
print(truediv(num1, num2))

#floor division
print(floordiv(num1, num2))

#modulus
print(mod(num1, num2))

#power
print(pow(num1, num2))
Output:
60
20
800
2.0
2
0
109951162777600000000000000000000


## List of Relational Operator Functions in Python

Below is the list of operators functions in Python which are equivalent to inbuilt relational operators.

Operator Function
Operation
Description
Equivalent inbuilt Operator
It (a, b)
Less Than
This function checks if a is less than b or not. It returns True if a is less than b, else it returns False.
<
le (a, b)
Less Than or Equal To
This function checks if a is less than or equal to b or not. Returns True if a is less than or equal to b, else returns False.
<=
gt (a, b)
Greater Than
This function checks if a is greater than b or not. It returns True if a is greater than b, else it returns False.
>
ge (a, b)
Greater Than or Equal To
This function checks if a is greater than or equal to b or not. It returns True if a is greater than or equal to b, else it returns false.
>=
eq (a, b)
Equal To
This function checks if a is equal to b or not. It returns True if a is equal to b, else it returns False.
=
ne (a, b)
Not Equal To
This function checks if a is not equal to b or is equal. It returns True if a is not equal to b, else it returns False.
!=

Here is an example program showing the various relational operator functions.

from operator import *
num1 = 40
num2 = 20

#checking if a is less than b
print(lt(num1, num2))

#checking if a is less than or equal to b
print(le(num1, num2))

#checking if a is greater than b
print(gt(num1, num2))

#checking if a is greater than or equal to b
print(ge(num1, num2))

#checking if a is equal to b
print(eq(num1, num2))

#checking if a is not equal to b
print(ne(num1, num2))
Output:
False
False
True
True
False
True


## List of Bitwise Operator Functions in Python

In Python, we have inbuilt bitwise operators such as and, or, xor, not. Below are the standard operator functions for these operations.

Operator Function
Operation
Description
Equivalent inbuilt Operator
and_(a, b)
AND
This function computes bitwise and of the arguments a, b.
a & b
or_(a, b)
OR
This function computes the bitwise or of the arguments a, b.
a | b
xor(a, b)
XOR
This function computes the bitwise xor of the arguments a, b.
a ^ b
invert(a)
NOT
This function computes the bitwise inversion of a.
~ a

Below is a program to understand these operator functions better.

from operator import *
num1 = 3
num2 = 4

#performing AND operation
print(and_(num1, num2))

#performing OR operation
print(or_(num1, num2))

#perforning XOR operation
print(xor(num1, num2))

#performing NOT operation
print(invert(num1))

Output:
0

7

7

-4


## Object Related Operator Functions in Python

Below are a few operator functions used in objects such as lists, tuples, strings, etc to access the elements of the object.

### 1) setitem (object_name, position, value)

This function is used toupdatea particular value at a particular positionin alist. This functioncannot be applied to a tuple as tuples are immutable. For example,

from operator import *
list1 = [20, 30.67, "FACE Prep"]
setitem(list1, 1, 50)
print(list1)
Output:
[20, 50, 'FACE Prep']


Explanation:In the above-given code, using the setitem function, we assign the value 50 at index 1.

### 2) delitem (object_name, position)

This function is used to delete a particular item from an object. For instance,

from operator import *
list1 = [20, 30.67, "FACE Prep"]
delitem (list1, 1)
print(list1)
Output:
[20, 'FACE Prep']


### 3) getitem (object_name, position)

This function is used to retrieve a particular value from a particular position of an object, be it List, tuple or String. For example,

from operator import *
list1 = [20, 30.67, "FACE Prep"]
print(getitem(list1, 2))
Output:
FACE Prep


### 4) concat (object_name1, object_name2)

This function is used to concatenate or join elements of two objects. For example,

from operator import *
list1 = [20, 30.67, "FACE Prep"]
list2 = [40, 50.98, "Python"]
print(concat(list1, list2))
Output:
[20, 30.67, 'FACE Prep', 40, 50.98, 'Python']


### 5) contains(object_name, value)

This function is used to check if a particular element is present in a particular object or not. For instance,

from operator import *
list1 = [20, 30.67, "FACE Prep"]
print(contains(list1, "FACE Prep"))
Output:True