python中not,and,or的优先级问题及用法

  • 优先级问题

– 在学习python中,发现其中的Boolean Operations — and, or, not的用法与常见的布尔代数用法有很大不同,其中确定优先级是判断一个表达式结果的关键,下面给出官方标准库的解释:

这里写图片描述

These are the Boolean operations ,ordered by ascending priority.官方标准库里的解释。这些布尔操作,按升序提升优先级。

即得到优先级关系:or<and<not,同一优先级默认从左往右计算。
  • python指令参考
    由于本机使用的是python2.7.13的版本,故查询了对应版本的指令介绍,其中也定义了Boolean operations,如下图:

这里写图片描述

这里,我们将对应具体的表达式进行分析讨论其中的优先级问题。例如对于 “a or b and c or d”而言,根据 or_test ::= and_test | or_test “or” and_test,在语法树上,自顶向下看,解析为 (a or b and c) or d ,这个形式为一个or_test, 符合 or_test “or” and_test 这个形式。这个应该没异议~//或者可尝试解析为a or (b and c or d),看一下能否后续分解。

第二层分别看 (a or b and c) 和 d ,d是一个comparison, 同时也是一个and_test, 同时也符合 or_test形式中or_test “or” and_test 的右部 and_test形式。d已经是原子了就不向下看了。

而(a or b and c) 是第一层的or_test的左部,只能按一个or_test形式来解析,因为,假如串”a or b and c” 按and_test形式来解析,那只能匹配形式 and_test ::= and_test “and” not_test ,也就是(a or b) and c, “and”左边必须为另一个and_test形式,但串 “a or b” 无法匹配进and_test形式。,按or_test形式来解析,则可以匹配 a or (b and c), 这是一个or_test,”or”的左边是一个or_test形式(单独and_test同时也可以匹配or_test形式),右边是一个and_test形式。因此 语法树被解析为 ( (a) or ( (b) and (c) ) ) or (d) 。

那求值的过程,就比较容易理解了,要求or表达式的值,先要求其中的左右不分and表达式的值,求and表达式的值,先要求and左右部的comparison原子或另一个and表达式的值。

最后,可尝试来分析a or (b and c or d)这种分解情况,满足or_test形式,即两边都为and_test形式。然后分析(b and c or d),因为(b and c or d)为and_test形式,即内部可分为b 和 (c or d),然而(c or d)并不能满足not_test 形式,即该表达式只能按照第一种情况分配。

  • False的定义及范例如下:

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:

None

False

zero of any numeric type, for example, 0, 0L, 0.0, 0j.

any empty sequence, for example, ”, (), [].

any empty mapping, for example, {}.

instances of user-defined classes, if the class defines a nonzero() or len() method, when that method returns the integer zero or bool value False. [1]
All other values are considered true — so objects of many types are always true.

Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.)

  • 求值技巧

关于求值的顺序,先左部,再右部,还要加入“短路”机制,说明这点的文档原文:
The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.
The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

对应同一优先级,我个人的理解为:or的目的是找到作用范围内第一个True或最后一个False,and的目的是找到作用范围第一个False或最后一个True。(其中,作用范围内的概念必须明确)。

这里写图片描述

#对于and而言,第一行返回(作用范围内)第一个假值,第二行返回最后一个正值
#对于or而言,第一行返回(作用范围内)第一个真值,第二行返回最后一个假值

对于包含and,not,or的表达式,通过优先级关系,处理起来也是较为简单的。利用短路逻辑规则:表达式从左至右运算,若 or 的左侧逻辑值为 True ,则短路 or 后所有的表达式(不管是 and 还是 or),直接输出 or 左侧表达式 。表达式从左至右运算,若 and 的左侧逻辑值为 False ,则短路其后所有 and 表达式,直到有 or 出现,输出 and 左侧表达式False到 or 的左侧,参与接下来的逻辑运算。若 or 的左侧为 False ,或者 and 的左侧为 True 则不能使用短路逻辑。

这里写图片描述

最后,对于not的定义比较简单,如果x为False则not x 为True,反之亦然。

这里写图片描述

注:以下链接分别为 The Python Language Reference / The Python Standard Library/参考知乎问题中江欢dalao的优质回答。

https://docs.python.org/2.7/reference/expressions.html#boolean-operations
https://docs.python.org/2/library/stdtypes.html#boolean-operations-and-or-not
https://www.zhihu.com/question/20152384

  • 14
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值