English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Operator functions in Python

In Python, there are also some standard library methods used for mathematical operations, such as arithmetic, logical, relational, bitwise, etc. These methods can be found inOperatorsFind it under the module.

Firstly, to use it, we need to import the operator standard library module.

import operator

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

Arithmetic operations

Firstly, we will see the arithmetic operation functions. These are as follows.

Serial NumberFunction and Description
1

add(x, y)

Theadd()The method is used to add two numbers x and y. It performs simple addition. It is similar to x + y operation.

2

sub(x, y)

Thesub()The method is used to subtract y from x. It is similar to x-y operation.

3

mul(x, y)

Themul()The method is used to multiply two numbers x and y. It is similar to x * y operation.

4

truediv(x, y)

Thetruediv()The method is used to find the result after dividing x by y. This method may return a fractional value as the result. It is similar to x / y operation.

5

floordiv(x, y)

Thefloordiv()The method is used to find x / The quotient of y. It is similar to x // y operation.

6

mod(x, y)

Themod()The method is used to get x / The remainder of y. It is similar to x%y operation.

7

War prisoner(x, y)

Thepow()The method is used to find x ^ y. It is similar to x ** y operation.

Example 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 Result

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

Relational Operation

This operator module also includes relational operators such as <, <=, >, >=, ==, !=, etc.

The functions of the operators are as follows-

Serial NumberFunction and Description
1

lt(x, y)

Thelt()method is used to check if the number x is less than y. Just like the x < y operation.

2

le(x, y)

Thele()method is used to check if the number x is less than or equal to y. Just like the x <= y operation.

3

eq(x, y)

Theeq()method is used to check if the numbers x and y are equal. Just like the x == y operation.

4

gt(x, y)

Thegt()method is used to check if the number x is greater than y. Just like the x > y operation.

5

ge(x, y)

Thege()method is used to check if the number x is greater than or equal to y. Just like the x >= y operation.

6

ne(x, y)

Thene()The method is used to check if the numbers x and y are not equal. Just like the x != y operation.

Example 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 Result

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