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

Operatorfunktionen in Python

在Python中,还有一些用于数学运算的标准库方法,例如算术,逻辑,关系,按位等运算。这些方法可以在运算符模块下找到。

首先要使用它,我们需要将其导入运算符标准库模块。

import operator

在本节中,我们将看到一些用于按位操作和容器操作的运算符函数。

算术运算

首先,我们将看到算术运算功能。这些如下。

NummerFunktion und Beschreibung
1

加(x,y)

Dasadd()方法用于将两个数字x和y相加。它执行简单的加法。它类似于x + y操作。

2

子(x,y)

Dassub()方法用于从x中减去y。它类似于x-y操作。

3

mul(x,y)

Dasmul()方法用于将两个数字x和y相乘。它类似于x * y操作。

4

truediv(x,y)

Dastruediv()方法用于在将x除以y之后查找结果。此方法可能会返回分数值作为结果。它类似于x / y操作。

5

floordiv(x,y)

Dasfloordiv()方法用于找到x / y的商。它类似于x // y操作。

6

mod(x,y)

Dasmod()方法用于获取x / y的余数。它类似于x%y操作。

7

战俘(x,y)

Daspow()方法用于查找x ^ y。它类似于x ** y操作。

Beispielcode

#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

Beziehungsoperation

Dieser Operator-Modul enthält auch Beziehungsoperatoren wie <, <=, >, >=, ==, !=.

Funktion des Operators-

NummerFunktion und Beschreibung
1

lt(x,y)

Daslt()Die Methode wird verwendet, um zu überprüfen, ob die Zahl x kleiner als y ist. Wie die Operation x < y.

2

le(x,y)

Dasle()Die Methode wird verwendet, um zu überprüfen, ob die Zahl x kleiner oder gleich y ist. Wie die Operation x <= y.

3

eq(x,y)

Daseq()Die Methode wird verwendet, um zu überprüfen, ob die Zahlen x und y gleich sind. Wie die Operation x == y.

4

gt(x,y)

Dasgt()Die Methode wird verwendet, um zu überprüfen, ob die Zahl x größer als y ist. Wie die Operation x > y.

5

ge(x,y)

Dasge()Die Methode wird verwendet, um zu überprüfen, ob die Zahl x größer oder gleich y ist. Wie die Operation x >= y.

6

ne(x,y)

Dasne()Die Methode wird verwendet, um zu überprüfen, ob die Zahlen x und y ungleich sind. Wie die Operation x != y.

Beispielcode

#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