English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
在Python中,还有一些用于数学运算的标准库方法,例如算术,逻辑,关系,按位等运算。这些方法可以在运算符模块下找到。
首先要使用它,我们需要将其导入运算符标准库模块。
import operator
在本节中,我们将看到一些用于按位操作和容器操作的运算符函数。
首先,我们将看到算术运算功能。这些如下。
Nummer | Funktion und Beschreibung |
---|---|
1 | 加(x,y) Das |
2 | 子(x,y) Das |
3 | mul(x,y) Das |
4 | truediv(x,y) Das |
5 | floordiv(x,y) Das |
6 | mod(x,y) Das |
7 | 战俘(x,y) Das |
#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)) # Gleicher als a % b print('pow: ' + str(operator.pow(5, 3)))
Ausgabeergebnis
Addition: 101 Verminderung: 11 Multiplikation: 2520 Wahre Division: 1.2444444444444445 Bodenteilung: 1 Mod: 11 pow: 125
Dieser Operator-Modul enthält auch Beziehungsoperatoren wie <, <=, >, >=, ==, !=.
Funktion des Operators-
Nummer | Funktion und Beschreibung |
---|---|
1 | lt(x,y) Das |
2 | le(x,y) Das |
3 | eq(x,y) Das |
4 | gt(x,y) Das |
5 | ge(x,y) Das |
6 | ne(x,y) Das |
#Beziehungs-Operatoren import operator print('Kleiner-als: ' + str(operator.lt(5, 10))) print('Kleiner-als-Gleich: ' + str(operator.le(10, 10))) print('Größer-als: ' + str(operator.gt(5, 5))) print('Größer-als-Gleich: ' + str(operator.ge(5, 5))) print('Gleich-zu: ' + str(operator.eq(12, 12))) print('Nicht-Gleich-zu: ' + str(operator.ne(15, 12)))
Ausgabeergebnis
Kleiner-als: True Kleiner-als-Gleich: True Größer-als: False Größer-als-Gleich: True Gleich-zu: True Nicht-Gleich-zu: True