《Python编程的术与道:Python语言入门》视频课程
《Python编程的术与道:Python语言入门》视频课程链接:https://edu.csdn.net/course/detail/27845
运算符 (Operators)
运算符用于对变量和值执行运算。
Python将运算符分为以下几类:
- 算术运算符 (Arithmetic operators)
- 赋值运算符 (Assignment operators)
- 比较运算符 (Comparison operators)
- 逻辑运算符 (Logical operators)
- 身份运算符 (Identity operators)
- 成员运算符 (Membership operators)
- 位运算符(Bitwise operators)
算术运算符
算术运算符与数值一起使用以执行常见的数学运算:
2**3
8
2**3.0
8.0
2.0**3 # 幂 - 返回x的y次幂
8.0
1/2
0.5
int(1/2)
0
1//2 # 取整除 - 返回商的整数部分(向下取整)
0
9//2
4
-9//2
-5
1%2 # 取模 - 返回除法的余数
1
3%2
1
4%2
0
Python赋值运算符
赋值运算符用于为变量赋值:
x=5
x+=3 # 加法赋值运算符 x=x+3
print(x)
8
print(x)
x-=3 # 减法赋值运算符
print(x)
8
5
print(x)
x*=3 # 乘法赋值运算符
print(x)
5
15
x/=3 # 除法赋值运算符
print(x)
5.0
x%=3 # 取模赋值运算符
print(x)
2.0
x**=3 # 幂赋值运算符
print(x)
8.0
x=5 # 0101
x&=3 # 0011
print(x) # 0001
1
x=5 # 0101
x|=3 # 0011
print(x) #0111
7
x=5 # 0101
x^=3 # 0011
print(x) # 0110
6
x=10 # 1010
x>>=3
print(x) # 0001
1
x=3 # 0011
x<<=3
print(x) # 11000
24
比较运算符
比较运算符用于比较两个值:
x = 6
y = 2
# equal to
print(x == y) # False
False
# not equal to
print(x != y) # True
True
# greater than
print(x > y) # True
True
# less than
print(x < y) # False
False
# greater than or equal to
print(x >= y) # True
True
逻辑运算符
x=7
print(x> 5 and x< 10)
True
print(x<5 or x < 4)
False
print(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) # False
False
# is not
print(x is not y) # True
True
a = [1, 2, 3]
b = a
b is a
print(id(a))
print(id(b))
2371673020616
2371673020616
b == a
True
b = a[:]
print(b)
b is a
[1, 2, 3]
False
b == a
True
成员运算符
L = ['red', 'green', 'blue']
# in
print('red' in L) # True
True
# not in
print('yellow' not in L) # True
True
位运算符
x = 0b1100
y = 0b1010
# and
print(bin(x & y)) # 0b1000
0b1000
# or
print(bin(x | y)) # 0b1110
0b1110
# xor
print(bin(x ^ y)) # 0b0110
0b110
# 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)) # 0b1100
0b110000
# shift 2 bits right
print(bin(x))
print(bin(x >> 2)) # 0b0011
0b1100
0b11
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = a & b; # 12 = 0000 1100 按位与运算符
print(c)
12
c = a | b; # 61 = 0011 1101 按位或运算符
print(c)
61
c = a ^ b; # 49 = 0011 0001 按位异或运算符
print(c)
49
c = ~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 = 0
e = (a + b) * c / d #( 30 * 15 ) / 5
print(e)
90.0
e = ((a + b) * c) / d # (30 * 15 ) / 5
print(e)
90.0
e = (a + b) * (c / d); # (30) * (15/5)
print(e)
90.0
e = a + (b * c) / d; # 20 + (150/5)
print(e)
50.0