English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
Firstly, we will see the arithmetic operation functions. These are as follows.
Serial Number | Function and Description |
---|---|
1 | add(x, y) The |
2 | sub(x, y) The |
3 | mul(x, y) The |
4 | truediv(x, y) The |
5 | floordiv(x, y) The |
6 | mod(x, y) The |
7 | War prisoner(x, y) The |
#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
This operator module also includes relational operators such as <, <=, >, >=, ==, !=, etc.
The functions of the operators are as follows-
Serial Number | Function and Description |
---|---|
1 | lt(x, y) The |
2 | le(x, y) The |
3 | eq(x, y) The |
4 | gt(x, y) The |
5 | ge(x, y) The |
6 | ne(x, y) The |
#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