运算符 (Operators)
运算符用于对变量和值执行运算。
Python将运算符分为以下几类:
- 算术运算符 (Arithmetic operators)
- 赋值运算符 (Assignment operators)
- 比较运算符 (Comparison operators)
- 逻辑运算符 (Logical operators)
- 身份运算符 (Identity operators)
- 成员运算符 (Membership operators)
- 位运算符(Bitwise operators)
算术运算符
算术运算符与数值一起使用以执行常见的数学运算:

2**382**3.08.02.0**3 # 幂 - 返回x的y次幂8.01/20.5int(1/2)01//2 # 取整除 - 返回商的整数部分(向下取整)09//24-9//2-51%2 # 取模 - 返回除法的余数13%214%20Python赋值运算符
赋值运算符用于为变量赋值:

x=5x+=3 # 加法赋值运算符 x=x+3
print(x)8print(x)
x-=3 # 减法赋值运算符
print(x)8
5print(x)
x*=3 # 乘法赋值运算符
print(x)5
15x/=3 # 除法赋值运算符
print(x)5.0x%=3 # 取模赋值运算符
print(x)2.0x**=3 # 幂赋值运算符
print(x)8.0x=5 # 0101
x&=3 # 0011
print(x) # 00011x=5 # 0101
x|=3 # 0011
print(x) #01117x=5 # 0101
x^=3 # 0011
print(x) # 01106x=10 # 1010
x>>=3
print(x) # 00011x=3 # 0011
x<<=3
print(x) # 1100024比较运算符
比较运算符用于比较两个值:

x = 6
y = 2# equal to
print(x == y) # FalseFalse# not equal to
print(x != y) # TrueTrue# greater than
print(x > y) # TrueTrue# less than
print(x < y) # FalseFalse# greater than or equal to
print(x >= y) # TrueTrue逻辑运算符

x=7
print(x> 5 and x< 10)Trueprint(x<5 or x < 4)Falseprint(not(x<5 and x< 10))True身份运算符 (Identity Operators)

a = 10
b = 10
if ( a is b ):
print("Same identiy")
else:
print("Not same identity")
if ( a is not b ):
print("Not sameame identy")
else:
print("Same identity")
print(id(a))
print(id(b))Same identiy
Same identity
140716488237744
140716488237744# 修改变量 b 的值
b = 20
if ( a is b ):
print("same identiy")
else:
print("Not same identity")
if ( a is not b ):
print("Not same identy")
else:
print("Same identity")
print(id(a))
print(id(b))Not same identity
Not same identy
140716488237744
140716488238064==和is之间的选择
== 运算符比较两个对象的值(对象中保存的数据),而 is 比较对象的标识。is 用于判断两个变量引用对象是否为同一个(同一块内存空间),
通常,我们关注的是值,而不是标识,因此 Python 代码中 == 出现的频率比 is 高。
然而,在变量和单例值之间比较时,应该使用 is。比如: is 检查变量绑定的值是不是 None。可以写为:x is None ;否定的正确写法是:x is not None
x = [1, 2, 3]
y = [1, 2, 3]
print(id(x))
print(id(y))2371673022216
2371652384904# is
print(x is y) # FalseFalse# is not
print(x is not y) # TrueTruea = [1, 2, 3]
b = a
b is a
print(id(a))
print(id(b))2371673020616
2371673020616b == aTrueb = a[:]
print(b)
b is a[1, 2, 3]
Falseb == aTrue成员运算符

L = ['red', 'green', 'blue']# in
print('red' in L) # TrueTrue# not in
print('yellow' not in L) # TrueTrue位运算符


x = 0b1100
y = 0b1010# and
print(bin(x & y)) # 0b10000b1000# or
print(bin(x | y)) # 0b11100b1110# xor
print(bin(x ^ y)) # 0b01100b110# not
print(x) # 12; 0b1100
print(bin(x))
print(bin(~x)) # -0b1101
print(~x)12
0b1100
-0b1101
-13# shift 2 bits left
print(bin(x << 2)) # 0b11000b110000# shift 2 bits right
print(bin(x))
print(bin(x >> 2)) # 0b00110b1100
0b11a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101c = a & b; # 12 = 0000 1100 按位与运算符
print(c)12c = a | b; # 61 = 0011 1101 按位或运算符
print(c)61c = a ^ b; # 49 = 0011 0001 按位异或运算符
print(c)49c = ~a; # -61 = 1100 0011 按位取反运算符
print(c)-61# 左移动运算符:运算数的各二进位全部左移若干位,由 << 右边的数字指定了移动的位数,高位丢弃,低位补0。
c = a << 2; # 240 = 1111 0000
print(c)240# 右移动运算符:把">>"左边的运算数的各二进位全部右移若干位,>> 右边的数字指定了移动的位数
c = a >> 2; # 15 = 0000 1111
print(c)15运算符优先级

a = 20
b = 10
c = 15
d = 5
e = 0e = (a + b) * c / d #( 30 * 15 ) / 5
print(e)90.0e = ((a + b) * c) / d # (30 * 15 ) / 5
print(e)90.0e = (a + b) * (c / d); # (30) * (15/5)
print(e)90.0e = a + (b * c) / d; # 20 + (150/5)
print(e)50.0
















