English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
在本文中,您将学习有关Python中不同类型的运算符,它们的语法以及如何在示例中使用它们的所有知识。
运算符是Python中执行算术或逻辑计算的特殊符号。运算符所操作的值称为操作数。
例如:
>>> 2+3 5
在这里,+是执行加法的运算符。2und3是操作数,5是操作的输出。
算术运算符用于执行数学运算,例如加法,减法,乘法等。
Operator | Bedeutung | Beispiel |
---|---|---|
+ | 加 - 两个操作数相加或一元加 | x + y + 2 |
- | 减 - 从左侧或一元减号,减去右侧操作数 | x-y- 2 |
* | 乘 -将两个操作数相乘 | x * y |
/ | 除 - 将左边操作数除以右边操作数(结果总是为float) | x / y |
% | 求模 -左操作数除以右操作数的余数 | x % y(x / y的余数) |
// | 取整除 - 返回商的整数部分(向下取整) | x // y |
** | 幂 - 返回x的y次幂 | x ** y(x的y次幂) |
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根据条件返回。
Operator | Bedeutung | Beispiel |
---|---|---|
> | 大于-如果左操作数大于右操作数,则为True | x > y |
< | 小于-如果左操作数小于右操作数,则为True | x < y |
== | 等于-如果两个操作数相等,则为True | x == y |
!= | 不等于-如果操作数不相等则为True | x != y |
>= | 大于或等于-如果左操作数大于或等于右,则为True | x >= y |
<= | 小于或等于-如果左操作数小于或等于右,则为True | x <= y |
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 are and, or, not operators.
Operator | Bedeutung | Beispiel |
---|---|---|
and | If both operands are true, it is true | x and y |
or | If any operand is true, it is true | x or y |
not | If the operand is false, it is True (complement the operand) | Not x |
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 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)
Operator | Bedeutung | Beispiel |
---|---|---|
& | Bitwise AND | x&y = 0(0000 0000) |
| | Bitwise OR | x | y = 14(0000 1110) |
~ | Bitwise NOT | 〜x = -11(1111 0101) |
^ | Bitwise XOR | x ^ y = 14(0000 1110) |
>> | Bitwise right shift | x >> 2 = 2(0000 0010) |
<< | Bitwise left shift | x << 2 = 40(0010 1000) |
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.
Operator | Beispiel | Identical |
---|---|---|
= | x = 5 | x = 5 |
+= | x + = 5 | x = x + 5 |
-= | x-= 5 | x = x-5 |
*= | x * = 5 | x = x * 5 |
/= | x / = 5 | x = x / 5 |
%= | x%= 5 | x = x%5 |
//= | x // = 5 | x = x // 5 |
**= | x ** = 5 | x = x ** 5 |
&= | x&= 5 | x = x&5 |
|= | x | = 5 | x = x | 5 |
^= | x ^ = 5 | x = x ^ 5 |
>>= | x >> = 5 | x = x >> 5 |
<<= | x << = 5 | x = x << 5 |
Python bietet einige spezielle Arten von Operatoren, wie den Identitätsoperator oder den Mitgliedschaftsoperator. Dies wird durch Beispiele beschrieben.
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.
Operator | Bedeutung | Beispiel |
---|---|---|
is | wahr, wenn die Operanden gleich sind (auf dasselbe Objekt verweisen) | x ist wahr |
is not | wahr, wenn die Operanden unterschiedlich sind (nicht auf dasselbe Objekt verweisen) | x ist nicht wahr |
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.
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.
Operator | Bedeutung | Beispiel |
---|---|---|
in | Wenn der Wert in der Sequenz gefunden wird/Variable, dann wahr | 5 in x |
not in | Wenn der Wert in der Sequenz nicht gefunden wird/Variable, dann wahr | 5 not in x |
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.