📅  最后修改于: 2020-09-19 14:53:42             🧑  作者: Mango
运算符是Python中执行算术或逻辑计算的特殊符号。 运算符操作的值称为操作数。
例如:
>>> 2+3
5
在这里, +
是执行加法的运算符 。 2
和3
是操作数, 5
是操作的输出。
算术运算符用于执行数学运算,例如加法,减法,乘法等。
Operator | Meaning | Example |
---|---|---|
+ | Add two operands or unary plus | x + y+ 2 |
– | Subtract right operand from the left or unary minus | x – y- 2 |
* | Multiply two operands | x * y |
/ | Divide left operand by the right one (always results into float) | x / y |
% | Modulus – remainder of the division of left operand by the right | x % y (remainder of x/y) |
// | Floor division – division that results into whole number adjusted to the left in the number line | x // y |
** | Exponent – left operand raised to the power of right | x**y (x to the power y) |
x = 15
y = 4
# Output: x + y = 19
print('x + y =',x+y)
# Output: x - y = 11
print('x - y =',x-y)
# Output: x * y = 60
print('x * y =',x*y)
# Output: x / y = 3.75
print('x / y =',x/y)
# Output: x // y = 3
print('x // y =',x//y)
# Output: 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 | Meaning | Example |
---|---|---|
> | Greater than – True if left operand is greater than the right | x > y |
< | Less than – True if left operand is less than the right | x < y |
== | Equal to – True if both operands are equal | x == y |
!= | Not equal to – True if operands are not equal | x != y |
>= | Greater than or equal to – True if left operand is greater than or equal to the right | x >= y |
<= | Less than or equal to – True if left operand is less than or equal to the right | x <= y |
x = 10
y = 12
# Output: x > y is False
print('x > y is',x>y)
# Output: x < y is True
print('x < y is',x= y is False
print('x >= y is',x>=y)
# Output: x <= y is True
print('x <= y is',x<=y)
输出
x > y is False
x < y is True
x == y is False
x != y is True
x >= y is False
x <= y is True
逻辑运算符是and
or
, not
运算符。
Operator | Meaning | Example |
---|---|---|
and | True if both the operands are true | x and y |
or | True if either of the operands is true | x or y |
not | True if operand is false (complements 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)
输出
x and y is False
x or y is True
not x is False
这是这些运算符的真值表。
按位运算符作用于操作数,就好像它们是二进制数字的字符串一样。它们一点一点地运行,因此得名。
例如,二进制的2是10
,而7是111
。
在下表中:令x
= 10(二进制为0000 1010
)和y
= 4(二进制为0000 0100
)
Operator | Meaning | Example |
---|---|---|
& | 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 ) |
在Python中使用赋值运算符为变量赋值。
a = 5
是一个简单的赋值运算符 ,它将右边的值5分配给左边的变量a
。
Python有多种复合运算符,例如a += 5
,可以将其添加到变量中,然后再分配给它们。它等效于a = a + 5
。
Operator | Example | Equivalent to |
---|---|---|
= | 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语言提供了一些特殊类型的运算符,例如身份运算符或成员资格运算符。下面通过示例对其进行描述。
is
和is not
Python中的标识运算符。它们用于检查两个值(或变量)是否位于内存的同一部分。两个相等的变量并不意味着它们是相同的。
Operator | Meaning | Example |
---|---|---|
is | True if the operands are identical (refer to the same object) | x is True |
is not | True if the operands are not identical (do not refer to the same object) | x is not True |
x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]
# Output: False
print(x1 is not y1)
# Output: True
print(x2 is y2)
# Output: False
print(x3 is y3)
输出
False
True
False
在这里,我们看到x1
和y1
是相同值的整数,因此它们既相等又相同。 x2
和y2
(字符串)的情况相同。
但是x3
和y3
是列表。它们是相等的但不相同。这是因为尽管它们相等,但解释器将它们分别定位在内存中。
in
和not in
是Python中的成员运算符。它们用于测试是否在序列(字符串,列表,元组,集合和字典)中找到值或变量。
在字典中,我们只能测试键的存在,而不是值。
Operator | Meaning | Example |
---|---|---|
in | True if value/variable is found in the sequence | 5 in x |
not in | True if value/variable is not found in the sequence | 5 not in x |
x = 'Hello world'
y = {1:'a',2:'b'}
# Output: True
print('H' in x)
# Output: True
print('hello' not in x)
# Output: True
print(1 in y)
# Output: False
print('a' in y)
输出
True
True
True
False
这里, 'H'
是在x
,但'hello'
不存在于x
(记住,Python是区分大小写)。同样, 1
是键,而'a'
是字典y
的值。因此, 'a' in y
返回False
。