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

Python 基础教程

Python 流程控制

Python-Funktion

Python-Datentypen

Python 文件操作

Python 对象和类

Python 日期和时间

Python 高级知识

Python 参考手册

Python-Operator

在本文中,您将学习有关Python中不同类型的运算符,它们的语法以及如何在示例中使用它们的所有知识。

python中的运算符是什么?

运算符是Python中执行算术或逻辑计算的特殊符号。运算符所操作的值称为操作数。

例如:

>>> 2+3
5

在这里,+是执行加法的运算符。2und3是操作数,5是操作的输出。

算术运算符

算术运算符用于执行数学运算,例如加法,减法,乘法等。

OperatorBedeutungBeispiel
+加 - 两个操作数相加或一元加x + y + 2
-减 - 从左侧或一元减号,减去右侧操作数x-y- 2
*乘 -将两个操作数相乘x * y
/除 - 将左边操作数除以右边操作数(结果总是为float)x / y
%求模 -左操作数除以右操作数的余数x % y(x / y的余数)
//取整除 - 返回商的整数部分(向下取整)x // y
**幂 - 返回x的y次幂x ** y(x的y次幂)

Beispiel1:Python中的算术运算符

x = 15
y = 4
# 输出: x + y = 19
print('x + y =', x+y)
# 输出: x - y = 11
print('x - y =', x-y)
# 输出: x * y = 60
print('x * y =', x*y)
# 输出: x / y = 3.75
print('x / y =', x/y)
# 输出: x // y = 3
print('x // y =', x//y)
# 输出: x ** y = 50625
print('x ** y =', x**y)

运行该程序时,输出为:

x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625

比较运算符

比较运算符用于比较值。它返回True或False根据条件返回。

OperatorBedeutungBeispiel
>大于-如果左操作数大于右操作数,则为Truex > y
<小于-如果左操作数小于右操作数,则为Truex < y
==等于-如果两个操作数相等,则为Truex == y
!=不等于-如果操作数不相等则为Truex != y
>=大于或等于-如果左操作数大于或等于右,则为Truex >= y
<=小于或等于-如果左操作数小于或等于右,则为Truex <= y

Beispiel2:Python中的比较运算符

x = 10
y = 12
# Ausgabe: x > y ist False
print('x > y ist ', x > y)
# Ausgabe: x < y ist True
print('x < y ist ', x < y)
# Ausgabe: x == y ist False
print('x == y ist ', x == y)
# Ausgabe: x != y ist True
print('x != y ist ', x != y)
# Output: x >= y is False
print('x >= y is', x>=y)
# Output: x <= y is True
print('x <= y is', x<=y)

Ausgaberesultat

x > y is False
x < y is True
x == y is False
x != y is True
x >= y is False
x <= y is True

Logical Operators

Logical operators are and, or, not operators.

OperatorBedeutungBeispiel
andIf both operands are true, it is truex and y
orIf any operand is true, it is truex or y
notIf the operand is false, it is True (complement the operand)Not x

Beispiel3: Logical operators in Python

x = True
y = False
print('x and y is', x and y)
print('x or y is', x or y)
print('not x is', not x)

Ausgaberesultat

x and y is False
x or y is True
not x is False

This is the truth table for these operators:Truth Table.

Bitwise Operators

Bitwise operators act on operands as if they were strings of binary digits. They run one by one, hence the name.

For example,2is10binary,7is111.

be listed in the table below:Letx= 10(0000 1010binary) andy= 4(0000 0100 binary)

OperatorBedeutungBeispiel
&Bitwise ANDx&y = 0(0000 0000)
|Bitwise ORx | y = 14(0000 1110)
~Bitwise NOT〜x = -11(1111 0101)
^Bitwise XORx ^ y = 14(0000 1110)
>>Bitwise right shiftx >> 2 = 2(0000 0010)
<<Bitwise left shiftx << 2 = 40(0010 1000)

Assignment Operator

In Python, the assignment operator is used to assign values to variables.

a = 5is a simple assignment operator that assigns the value on the right5assigned to the left variablea.

There are many similar compound operators in Python, a += 5They are added to the variable and then assigned to them. Equivalent to a = a + 5.

OperatorBeispielIdentical
=x = 5x = 5
+=x + = 5x = x + 5
-=x-= 5x = x-5
*=x * = 5x = x * 5
/=x / = 5x = x / 5
%=x%= 5x = x%5
//=x // = 5x = x // 5
**=x ** = 5x = x ** 5
&=x&= 5x = x&5
|=x | = 5x = x | 5
^=x ^ = 5x = x ^ 5
>>=x >> = 5x = x >> 5
<<=x << = 5x = x << 5

Special Operator

Python bietet einige spezielle Arten von Operatoren, wie den Identitätsoperator oder den Mitgliedschaftsoperator. Dies wird durch Beispiele beschrieben.

Identitätsoperator

is und is not sind Identitätsoperator in Python. Sie werden verwendet, um zu überprüfen, ob zwei Werte (oder Variablen) im selben Speicherbereich liegen. Zwei gleiche Variablen bedeuten nicht, dass sie dasselbe sind.

OperatorBedeutungBeispiel
iswahr, wenn die Operanden gleich sind (auf dasselbe Objekt verweisen)x ist wahr
is notwahr, wenn die Operanden unterschiedlich sind (nicht auf dasselbe Objekt verweisen)x ist nicht wahr

Beispiel4: Identitätsoperator in Python

x1 = 5
y1 = 5
x2 =
y2 =
x3 [1=2=3,
y3 [1=2=3,
# Ausgabe: False
is not y1 ]1is y
# Ausgabe: True
is not y2 print(x2is y
# Ausgabe: False
is not y3 print(x3is y

Ausgaberesultat

False
True
False

)x1undy1hier sehen wirx2undy2der Fall ist derselbe. Dies liegt daran, dass sie gleiche Werte sind, daher sind sie sowohl gleich als auch gleich.

aberx3undy3sind Listen. Sie sind gleich, aber unterschiedlich. Dies liegt daran, dass sie obwohl gleich, vom Interpreter unterschiedlich in der Speicherposition zugeordnet werden.

Mitgliedschaftsoperator

in und not in sind Mitgliedschaftsoperator in Python. Sie werden verwendet, um zu testen, ob ein Wert in einer Sequenz (Zeichenkette,Liste,Tupel,MengeundDictionaryob ein Wert oder eine Variable im

In einem Dictionary können wir nur die Existenz von Schlüsseln testen, nicht von Werten.

OperatorBedeutungBeispiel
inWenn der Wert in der Sequenz gefunden wird/Variable, dann wahr5 in x
not inWenn der Wert in der Sequenz nicht gefunden wird/Variable, dann wahr5 not in x

Beispiel #5: Mitgliedschaftsoperator in Python

x = 'Hello world'
y = {1: 'a',2: 'b'}
# Ausgabe: True
print('H' in x)
# Ausgabe: True
print('hello' not in x)
# Ausgabe: True
print(1 in y)
# Ausgabe: False
print('a' in y)

Ausgaberesultat

True
True
True
False

Hier ist 'H' in x enthalten, aber 'hello' ist in x nicht enthalten (denken Sie daran, dass Python zwischen Gross- und Kleinbuchstaben unterscheidet). Ähnlich wie1a ist ein Schlüssel, während y der Wert im Dictionary y ist, daher gibt y für a False zurück.