Python - 运算符



Python 运算符( Python Operators )是用于对一个或多个操作数执行特定操作的特殊符号。变量、值或表达式可以用作操作数。例如,Python 的加法运算符 (+) 用于对两个变量、值或表达式执行加法运算。

以下是与 Python 运算符相关的一些术语:

  • 一元运算符:需要一个操作数来执行特定操作的 Python 运算符称为一元运算符。
  • 二元运算符:需要两个操作数来执行特定操作的 Python 运算符称为二元运算符。
  • 操作数:与运算符一起使用以执行特定操作的变量、值或表达式。

Python 运算符的类型

Python 运算符分为以下类别 -

让我们逐一看看所有的运算符。

Python 算术运算符

Python 算术运算符用于执行基本的数学运算,例如加法、减法、乘法等。

下表包含所有算术运算符及其符号、名称和示例(假设 a 和 b 的值分别为 10 和 20)-

运算符 名称 示例
+ 加法 a + b = 30
- 减法 a – b = -10
* 乘法 a * b = 200
/ 除法 b / a = 2
% 余数 b % a = 0
** 指数 a**b =10**20
// 地板除法 9//2 = 4

Python 算术运算符示例


a = 21
b = 10
c = 0

c = a + b
print ("a: {} b: {} a+b: {}".format(a,b,c))

c = a - b
print ("a: {} b: {} a-b: {}".format(a,b,c) )

c = a * b
print ("a: {} b: {} a*b: {}".format(a,b,c))

c = a / b
print ("a: {} b: {} a/b: {}".format(a,b,c))

c = a % b
print ("a: {} b: {} a%b: {}".format(a,b,c))

a = 2
b = 3
c = a**b	
print ("a: {} b: {} a**b: {}".format(a,b,c))

a = 10
b = 5
c = a//b	
print ("a: {} b: {} a//b: {}".format(a,b,c))

输出

a: 21 b: 10 a+b: 31
a: 21 b: 10 a-b: 11
a: 21 b: 10 a*b: 210
a: 21 b: 10 a/b: 2.1
a: 21 b: 10 a%b: 1
a: 2 b: 3 a**b: 8
a: 10 b: 5 a//b: 2

Python 比较运算符

Python 比较运算符比较它们两侧的值并确定它们之间的关系。它们也称为关系运算符。

下表包含所有比较运算符及其符号、名称和示例(假设 a 和 b 的值分别为 10 和 20)-

运算符 名字 例子
== Equal (a == b) is not true.
!= Not equal (a != b) is true.
> Greater than (a > b) is not true.
< Less than (a < b) is true.
>= Greater than or equal to (a >= b) is not true.
<= Less than or equal to (a <= b) is true.

Python 比较运算符示例


a = 21
b = 10
if ( a == b ):
	 	print ("Line 1 - a is equal to b")
else:
	 	print ("Line 1 - a is not equal to b")

if ( a != b ):
	 	print ("Line 2 - a is not equal to b")
else:
	 	print ("Line 2 - a is equal to b")

if ( a < b ):
	 	print ("Line 3 - a is less than b" )
else:
	 	print ("Line 3 - a is not less than b")

if ( a > b ):
	 	print ("Line 4 - a is greater than b")
else:
	 	print ("Line 4 - a is not greater than b")

a,b=b,a #values of a and b swapped. a becomes 10, b becomes 21

if ( a <= b ):
	 	print ("Line 5 - a is either less than or equal to 	b")
else:
	 	print ("Line 5 - a is neither less than nor equal to 	b")

if ( b >= a ):
	 	print ("Line 6 - b is either greater than 	or equal to b")
else:
	 	print ("Line 6 - b is neither greater than 	nor equal to b")

输出

Line 1 - a is not equal to b
Line 2 - a is not equal to b
Line 3 - a is not less than b
Line 4 - a is greater than b
Line 5 - a is either less than or equal to b
Line 6 - b is either greater than or equal to b

Python 赋值运算符

Python 赋值运算符用于为变量赋值。下表显示了所有 Python 赋值运算符。

下表包含所有赋值运算符及其符号、名称和示例 -

运算符 示例 等同于
= a = 10 a = 10
+= a += 30 a = a + 30
-= a -= 15 a = a - 15
*= a *= 10 a = a * 10
/= a /= 5 a = a / 5
%= a %= 5 a = a % 5
**= a **= 4 a = a ** 4
//= a //= 5 a = a // 5
&= a &= 5 a = a & 5
|= a |= 5 a = a | 5
^= a ^= 5 a = a ^ 5
>>= a >>= 5 a = a >> 5
<<= a <<= 5 a = a << 5

Python 赋值运算符示例


a = 21
b = 10
c = 0
print ("a: {} b: {} c : {}".format(a,b,c))
c = a + b
print ("a: {} 	c = a + b: {}".format(a,c))

c += a
print ("a: {} c += a: {}".format(a,c))

c *= a
print ("a: {} c *= a: {}".format(a,c))

c /= a	
print ("a: {} c /= a : {}".format(a,c))

c 	= 2
print ("a: {} b: {} c : {}".format(a,b,c))
c %= a
print ("a: {} c %= a: {}".format(a,c))

c **= a
print ("a: {} c **= a: {}".format(a,c))

c //= a
print ("a: {} c //= a: {}".format(a,c))

输出

a: 21 b: 10 c : 0
a: 21 c = a + b: 31
a: 21 c += a: 52
a: 21 c *= a: 1092
a: 21 c /= a : 52.0
a: 21 b: 10 c : 2
a: 21 c %= a: 2
a: 21 c **= a: 2097152
a: 21 c //= a: 99864

Python 按位运算符

Python 按位运算符作用于位并执行逐位运算。这些运算符用于比较二进制数。

下表包含所有按位运算符及其符号、名称和示例 -

运算符 名称 示例
& AND a & b
| OR a | b
^ XOR a ^ b
~ NOT ~a
<< Zero fill left shift a << 3
>> Signed right shift a >> 3

Python 按位运算符示例


a = 20 	 	 	 	 	 	
b = 10 	 	 	 	 	 	

print ('a=',a,':',bin(a),'b=',b,':',bin(b))
c = 0

c = a & b; 	 	 	 	
print ("result of AND is ", c,':',bin(c))

c = a | b; 	 		
print ("result of OR is ", c,':',bin(c))

c = a ^ b; 	 	 	 	
print ("result of EXOR is ", c,':',bin(c))

c = ~a; 	 	 	 	 		
print ("result of COMPLEMENT is ", c,':',bin(c))

c = a << 2; 	 	 		
print ("result of LEFT SHIFT is ", c,':',bin(c))

c = a >> 2; 	 	 		
print ("result of RIGHT SHIFT is ", c,':',bin(c))

输出

a= 20 : 0b10100 b= 10 : 0b1010
result of AND is 0 : 0b0
result of OR is 30 : 0b11110
result of EXOR is 30 : 0b11110
result of COMPLEMENT is -21 : -0b10101
result of LEFT SHIFT is 80 : 0b1010000
result of RIGHT SHIFT is 5 : 0b101

Python 逻辑运算符

Python 逻辑运算符用于组合两个或多个条件并检查最终结果。Python 语言支持以下逻辑运算符。假设变量 a 持有 10,变量 b 持有 20,那么

下表包含所有逻辑运算符及其符号、名称和示例 -

运算符 名称 示例
and AND a and b
or OR a or b
not NOT not(a)

Python 逻辑运算符示例


var = 5

print(var > 3 and var < 10)
print(var > 3 or var < 4)
print(not (var > 3 and var < 10))

输出


	
True
True
False

Python 成员资格运算符

Python 成员资格运算符测试序列中的成员资格,例如字符串、列表或元组。

有两个成员资格运算符,如下所述 -

运算符 描述 示例
in 如果在指定序列中找到变量,则返回 True,否则返回 false。 a in b
not in 如果未在指定序列中找到变量,则返回 True,否则返回 false。 a not in b

Python 成员资格运算符示例


a = 10
b = 20
list = [1, 2, 3, 4, 5 ]

print ("a:", a, "b:", b, "list:", list)

if ( a in list ):
	 	print ("a is present in the given list")
else:
	 	print ("a is not present in the given list")

if ( b not in list ):
	 	print ("b is not present in the given list")
else:
	 	print ("b is present in the given list")

c=b/a
print ("c:", c, "list:", list)
if ( c in list ):
	 	print ("c is available in the given list")
else:
	 	 print ("c is not available in the given list")

输出

a: 10 b: 20 list: [1, 2, 3, 4, 5]
a is not present in the given list
b is not present in the given list
c: 2.0 list: [1, 2, 3, 4, 5]
c is available in the given list

Python 身份运算符

Python 身份运算符比较两个对象的内存位置。

下面解释了两个身份运算符 -

运算符 描述 示例
is 如果两个变量是同一对象,则返回 True,否则返回 false。 a is b
is not 如果两个变量不是同一个对象,则返回 True,否则返回 false。 a is not b

Python 身份运算符示例


a = [1, 2, 3, 4, 5]
b = [1, 2, 3, 4, 5]
c = a

print(a is c)
print(a is b)

print(a is not c)
print(a is not b)

输出

True
False
False
True

Python 运算符优先级

运算符优先级决定了运算符的计算顺序。Python 运算符具有不同的优先级。下表包含具有最高到最低优先级的运算符列表 -

下表列出了从最高优先级到最低优先级的所有运算符。

运算符 &描述
**

指数化(升至幂)

~ + -

补码、一元加号和减号(最后两个的方法名称为 +@ 和 -@)

* / % //

乘、除、模和下限除法

+ -

加法和减法

>> <<

右、左移位

&

按位 'AND'

^ |

按位排斥 'OR' 和常规 'OR'

<= < > >=

比较运算符

<> == !=

相等运算符

= %= /= //= -= += *= **=

赋值运算符

is is not

标识运算符

in not in

成员资格运算符

not or and

逻辑运算符

在此处阅读有关 Python 运算符优先级的更多信息:Python 运算符优先级