在Python中,还有一些用于数学运算的标准库方法,例如算术,逻辑,关系,按位等运算。这些方法可以在运算符模块下找到。
首先要使用它,我们需要将其导入运算符标准库模块。
import operator
在本节中,我们将看到一些用于按位操作和容器操作的运算符函数。
首先,我们将看到算术运算功能。这些如下。
| 序号 | 功能与说明 |
|---|---|
| 1 | 加(x,y) 该 |
| 2 | 子(x,y) 该 |
| 3 | mul(x,y) 该 |
| 4 | truediv(x,y) 该 |
| 5 | floordiv(x,y) 该 |
| 6 | mod(x,y) 该 |
| 7 | 战俘(x,y) 该 |
#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)))输出结果
Add: 101 Subtract: 11 Multiplication: 2520 True division: 1.2444444444444445 Floor division: 1 Mod: 11 pow: 125
该运算符模块还包含诸如<,<=,>,> =,==,!=之类的关系运算符。
运算符功能如下-
| 序号 | 功能与说明 |
|---|---|
| 1 | lt(x,y) 该 |
| 2 | le(x,y) 该 |
| 3 | eq(x,y) 该 |
| 4 | gt(x,y) 该 |
| 5 | ge(x,y) 该 |
| 6 | ne(x,y) 该 |
#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)))输出结果
Less Than: True Less Than Equal: True Greater Than: False Greater Than Equal: True Equal to: True Not Equal to: True