📜  Python运算子

📅  最后修改于: 2020-09-19 14:08:19             🧑  作者: Mango

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

Python中的运算符是什么?

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

例如:

>>> 2+3
5

在这里, +是执行加法的运算符 。 23是操作数, 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)

示例1: Python的算术运算符

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

比较运算符

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

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

示例2: Python的比较运算符

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 ornot运算符。

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

示例3: 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)

输出

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语言提供了一些特殊类型的运算符,例如身份运算符或成员资格运算符。下面通过示例对其进行描述。

身份运算符

isis 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

示例4: Python的身份运算符

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

在这里,我们看到x1y1是相同值的整数,因此它们既相等又相同。 x2y2 (字符串)的情况相同。

但是x3y3是列表。它们是相等的但不相同。这是因为尽管它们相等,但解释器将它们分别定位在内存中。

会员经营者

innot 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

示例#5: Python的成员资格运算符

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