运算符优先级 #
运算符优先级决定了表达式中运算的顺序。
一、优先级表 #
从高到低排列:
| 优先级 | 运算符 | 描述 |
|---|---|---|
| 1 | () |
括号 |
| 2 | ** |
幂运算 |
| 3 | +x, -x, ~x |
正号、负号、按位取反 |
| 4 | *, /, //, % |
乘、除、整除、取余 |
| 5 | +, - |
加法、减法 |
| 6 | <<, >> |
左移、右移 |
| 7 | & |
按位与 |
| 8 | ^ |
按位异或 |
| 9 | | |
按位或 |
| 10 | in, not in, is, is not, <, <=, >, >=, !=, == |
比较运算符 |
| 11 | not |
逻辑非 |
| 12 | and |
逻辑与 |
| 13 | or |
逻辑或 |
| 14 | if-else |
条件表达式 |
| 15 | lambda |
lambda表达式 |
| 16 | := |
海象运算符 |
二、算术运算符优先级 #
2.1 基本优先级 #
python
# 幂运算优先级最高
result = 2 + 3 ** 2
# 等价于:2 + (3 ** 2) = 2 + 9 = 11
print(result) # 11
# 乘除优先于加减
result = 2 + 3 * 4
# 等价于:2 + (3 * 4) = 2 + 12 = 14
print(result) # 14
# 同优先级从左到右
result = 10 - 3 - 2
# 等价于:(10 - 3) - 2 = 7 - 2 = 5
print(result) # 5
2.2 幂运算的特殊性 #
python
# 幂运算是右结合的
result = 2 ** 3 ** 2
# 等价于:2 ** (3 ** 2) = 2 ** 9 = 512
print(result) # 512
# 负号与幂运算
result = -2 ** 2
# 等价于:-(2 ** 2) = -4
print(result) # -4
# 使用括号改变
result = (-2) ** 2
print(result) # 4
2.3 乘除运算 #
python
# 乘除同级,从左到右
result = 10 / 2 * 5
# 等价于:(10 / 2) * 5 = 5.0 * 5 = 25.0
print(result) # 25.0
result = 10 * 2 / 5
# 等价于:(10 * 2) / 5 = 20 / 5 = 4.0
print(result) # 4.0
三、比较运算符优先级 #
3.1 链式比较 #
python
# 比较运算符可以链式使用
x = 5
result = 1 < x < 10
# 等价于:1 < x and x < 10
print(result) # True
# 多个比较
result = 1 < 5 < 10 < 20
print(result) # True
3.2 比较与逻辑运算符 #
python
# 比较运算符优先级高于逻辑运算符
a, b = True, False
result = a and b == False
# 等价于:a and (b == False) = True and True = True
print(result) # True
# 另一个例子
x = 5
result = x > 3 and x < 10
# 等价于:(x > 3) and (x < 10) = True and True = True
print(result) # True
四、逻辑运算符优先级 #
4.1 not > and > or #
python
# not 优先级最高
result = not True and False
# 等价于:(not True) and False = False and False = False
print(result) # False
# and 优先级高于 or
result = True or False and False
# 等价于:True or (False and False) = True or False = True
print(result) # True
# 复杂表达式
result = not True or False and True
# 等价于:(not True) or (False and True) = False or False = False
print(result) # False
4.2 使用括号明确意图 #
python
# 不使用括号(遵循默认优先级)
result = True or False and False
print(result) # True
# 使用括号改变优先级
result = (True or False) and False
print(result) # False
# 复杂表达式建议使用括号
result = (a and b) or (c and d)
五、位运算符优先级 #
5.1 位运算优先级 #
python
# 位运算符优先级低于算术运算符
result = 2 + 3 & 1
# 等价于:(2 + 3) & 1 = 5 & 1 = 1
print(result) # 1
# 位移运算符优先级低于加减
result = 1 << 2 + 1
# 等价于:1 << (2 + 1) = 1 << 3 = 8
print(result) # 8
# 按位运算符优先级
result = 5 | 3 & 7
# 等价于:5 | (3 & 7) = 5 | 3 = 7
print(result) # 7
5.2 位运算与比较 #
python
# 比较运算符优先级高于位运算符
result = 5 & 3 == 1
# 等价于:5 & (3 == 1) = 5 & False
# TypeError: 不支持的操作
# 正确写法
result = (5 & 3) == 1
print(result) # True
六、结合性 #
6.1 左结合运算符 #
大多数运算符是左结合的(从左到右计算):
python
# 加减法(左结合)
result = 10 - 5 - 2
# 等价于:(10 - 5) - 2 = 3
print(result) # 3
# 乘除法(左结合)
result = 10 / 2 / 5
# 等价于:(10 / 2) / 5 = 1.0
print(result) # 1.0
# 比较运算符(左结合)
result = 1 < 2 < 3
# 但链式比较特殊,等价于:1 < 2 and 2 < 3
6.2 右结合运算符 #
少数运算符是右结合的:
python
# 幂运算(右结合)
result = 2 ** 3 ** 2
# 等价于:2 ** (3 ** 2) = 512
print(result) # 512
# 条件表达式(右结合)
x = 1
result = "a" if x == 1 else "b" if x == 2 else "c"
# 等价于:"a" if x == 1 else ("b" if x == 2 else "c")
print(result) # "a"
# 赋值运算符(右结合)
a = b = c = 5
# 等价于:a = (b = (c = 5))
七、使用括号 #
7.1 改变优先级 #
python
# 默认优先级
result = 2 + 3 * 4
print(result) # 14
# 使用括号改变
result = (2 + 3) * 4
print(result) # 20
# 嵌套括号
result = ((2 + 3) * 4 - 5) / 3
print(result) # 5.0
7.2 提高可读性 #
python
# 即使知道优先级,也建议使用括号提高可读性
# 不太清晰
result = a and b or c and d
# 更清晰
result = (a and b) or (c and d)
# 复杂条件
if (age >= 18 and has_license) or is_emergency:
print("可以开车")
# 位运算
result = (flags & mask) == expected
八、短路求值 #
8.1 and 短路 #
python
def expensive_func():
print("函数被调用")
return True
# 短路:第一个为假,不计算第二个
result = False and expensive_func()
# 函数不会被调用
print(result) # False
8.2 or 短路 #
python
# 短路:第一个为真,不计算第二个
result = True or expensive_func()
# 函数不会被调用
print(result) # True
8.3 利用短路 #
python
# 安全访问
user = None
name = user and user.name # 不会报错
# 默认值
value = None
result = value or "默认值"
# 条件执行
debug = True
debug and print("调试信息")
九、常见陷阱 #
9.1 is 与 == 混淆 #
python
# is 检查身份,== 检查值
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True(值相等)
print(a is b) # False(不同对象)
9.2 链式比较陷阱 #
python
# 这个表达式
x = 5
result = x > 0 < 10
# 等价于:x > 0 and 0 < 10
# 不是:(x > 0) < 10
# 另一个例子
result = False == False in [False]
# 等价于:False == False and False in [False]
# 即:True and True = True
print(result) # True
9.3 复合赋值与优先级 #
python
# 复合赋值先计算右边,再赋值
x = 5
x *= 2 + 3
# 等价于:x = x * (2 + 3) = 5 * 5 = 25
print(x) # 25
9.4 位运算与比较 #
python
# 错误示例
# result = 5 & 3 == 1 # TypeError
# 正确写法
result = (5 & 3) == 1
print(result) # True
十、最佳实践 #
10.1 使用括号明确意图 #
python
# 不推荐
if a and b or c and d:
pass
# 推荐
if (a and b) or (c and d):
pass
10.2 拆分复杂表达式 #
python
# 不推荐
if age >= 18 and age <= 65 and has_license and not is_drunk:
pass
# 推荐
is_adult = 18 <= age <= 65
can_drive = is_adult and has_license and not is_drunk
if can_drive:
pass
10.3 使用有意义的变量名 #
python
# 不推荐
if a and b or c:
pass
# 推荐
has_permission = user.is_admin or user.is_moderator
is_active = user.last_login < 30 * 24 * 3600
if has_permission and is_active:
pass
十一、总结 #
优先级口诀:
- 括号最优先
- 幂运算第二
- 正负号第三
- 乘除整余四
- 加减第五位
- 移位第六位
- 位与第七位
- 位异或第八
- 位或第九位
- 比较第十位
- not 第十一
- and 第十二
- or 第十三
记住要点:
- 不确定时,使用括号
- 幂运算是右结合
- 大多数运算符是左结合
- 比较运算符可以链式使用
- 利用短路求值提高效率
最后更新:2026-03-16