杰瑞科技汇

Python中not、and优先级谁更高?

在 Python 中,not 运算符的优先级非常高,仅次于所有比较运算符(如 >, <, , in, is 等)和算术运算符(如 , , , 等)。

Python中not、and优先级谁更高?-图1
(图片来源网络,侵删)

它的优先级高于布尔运算符 andor

优先级从高到低大致可以这样排序:

  1. 括号
  2. 算术运算符 (, , , , 等)
  3. 比较运算符 (>, <, , >=, <=, , in, is 等)
  4. not
  5. and
  6. or

优先级详解与示例

not vs. and

not 的优先级高于 and,这意味着在表达式中,not 会先被计算。

示例:

Python中not、and优先级谁更高?-图2
(图片来源网络,侵删)
# 表达式: not a and b
# Python 的计算顺序是: (not a) and b
a = True
b = True
# (not True) and True => False and True => False
result = not a and b
print(f"not True and True 的结果是: {result}") # 输出: False
a = False
b = True
# (not False) and True => True and True => True
result = not a and b
print(f"not False and True 的结果是: {result}") # 输出: True

如果你想让 and 先计算,你必须使用括号:

# 表达式: not (a and b)
# Python 的计算顺序是: not (a and b)
a = True
b = False
# not (True and False) => not (False) => True
result = not (a and b)
print(f"not (True and False) 的结果是: {result}") # 输出: True

not vs. or

not 的优先级也高于 or

示例:

# 表达式: not a or b
# Python 的计算顺序是: (not a) or b
a = True
b = False
# (not True) or False => False or False => False
result = not a or b
print(f"not True or False 的结果是: {result}") # 输出: False
a = False
b = False
# (not False) or False => True or False => True
result = not a or b
print(f"not False or False 的结果是: {result}") # 输出: True

同样,如果你想让 or 先计算,必须使用括号:

# 表达式: not (a or b)
# Python 的计算顺序是: not (a or b)
a = False
b = False
# not (False or False) => not (False) => True
result = not (a or b)
print(f"not (False or False) 的结果是: {result}") # 输出: True

not vs. 比较运算符

not 的优先级低于比较运算符,这是一个非常重要的点。

示例:

# 表达式: not x > 5
# Python 的计算顺序是: not (x > 5)
x = 10
# not (10 > 5) => not (True) => False
result = not x > 5
print(f"not 10 > 5 的结果是: {result}") # 输出: False
x = 3
# not (3 > 5) => not (False) => True
result = not x > 5
print(f"not 3 > 5 的结果是: {result}") # 输出: True

如果你想让 not 先作用于 x,你必须使用括号:

# 表达式: (not x) > 5
# Python 的计算顺序是: (not x) > 5
x = 0
# (not 0) > 5 => (True) > 5 => True > 5 => False
# 因为 bool 是 int 的子类, True == 1, 1 > 5 是 False
result = (not x) > 5
print(f"(not 0) > 5 的结果是: {result}") # 输出: False
x = -10
# (not -10) > 5 => (False) > 5 => 0 > 5 => False
# 因为 bool 是 int 的子类, False == 0, 0 > 5 是 False
result = (not x) > 5
print(f"(not -10) > 5 的结果是: {result}") # 输出: False

not vs. in

not 的优先级也低于 in 运算符。

示例:

# 表达式: not item in my_list
# Python 的计算顺序是: not (item in my_list)
my_list = [1, 2, 3]
item = 4
# not (4 in [1, 2, 3]) => not (False) => True
result = not item in my_list
print(f"not 4 in [1, 2, 3] 的结果是: {result}") # 输出: True

如果你想让 not 作用于整个 in 表达式,这是唯一且推荐的写法,你会发现 not (item in my_list)item not in my_list 的结果是相同的。

Python 提供了专门的关键字 not in 来增强可读性,它和 not (item in my_list) 完全等价,并且是更 Pythonic(更符合 Python 风格)的写法。

my_list = [1, 2, 3]
item = 4
# 推荐的写法
result = item not in my_list
print(f"4 not in [1, 2, 3] 的结果是: {result}") # 输出: True

总结表格

为了方便记忆,这里有一个简化的优先级表(部分常见运算符):

运算符 描述 优先级
括号 最高
幂运算
, , , 乘, 除, 整除, 取模
, 加, 减
, , >, <, >=, <=, in, is 比较, 成员身份, 身份
not 逻辑非
and 逻辑与
or 逻辑或

最佳实践

  1. 当你不确定时,使用括号:这是避免因优先级问题导致 bug 的最好方法,即使括号不是必需的,加上它也能让代码的意图更清晰。

    # 清晰
    if not (user.is_admin and user.is_active):
        print("访问被拒绝")
    # 可能引起混淆
    if not user.is_admin and user.is_active:
        print("访问被拒绝") # 这里的逻辑和上面完全不同
  2. 优先使用 not inis not:对于 not inis not,Python 提供了专门的语法,应该优先使用它们,而不是 not (item in list)not (obj is other_obj),这更符合 Python 的惯用风格,也更易读。

  3. 保持代码简单:如果逻辑变得非常复杂,not a and b or c and not d,考虑将其分解成多个有意义的变量或函数,而不是写在一行里,这能极大地提高代码的可读性和可维护性。

分享:
扫描分享到社交APP
上一篇
下一篇