【转载】python中not,and,or的优先级问题及用法
作业:
>>> print(5<4 or 3)
3
>>> print(2>1 or 6)
True
>>> print(5>1 and 0)
0
>>> print(5>1 and 0 or 1 and 0 <1>9 or 8)
8
>>> print(5>1 and 0 or 1 and 0 <1>9 )
False
###
and 返回第一个假值 或最后一个真值
or返回第一个真值或最后一个假值
###
我个人的理解为:or的目的是找到作用范围内第一个True或最后一个False,and的目的是找到作用范围第一个False或最后一个True。(其中,作用范围内的概念必须明确)
###
python中not,and,or的优先级问题及用法
- 优先级问题
– 在学习python中,发现其中的Boolean Operations — and, or, not的用法与常见的布尔代数用法有很大不同,其中确定优先级是判断一个表达式结果的关键,下面给出官方标准库的解释:
These are the Boolean operations ,ordered by ascending priority.官方标准库里的解释。这些布尔操作,按升序提升优先级。
即得到优先级关系:or<and<not,同一优先级默认从左往右计算。
- 1
- 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
【转载】python中not,and,or的优先级问题及用法的更多相关文章
- Python中的map()函数和reduce()函数的用法
Python中的map()函数和reduce()函数的用法 这篇文章主要介绍了Python中的map()函数和reduce()函数的用法,代码基于Python2.x版本,需要的朋友可以参考下 Py ...
- [转载]python中的sys模块(二)
#!/usr/bin/python # Filename: using_sys.py import sys print 'The command line arguments are:' for i ...
- [转载]python中multiprocessing.pool函数介绍
原文地址:http://blog.sina.com.cn/s/blog_5fa432b40101kwpi.html 作者:龙峰 摘自:http://hi.baidu.com/xjtukanif/blo ...
- 转载:Python中的new style class机制实现
1.Python中的对象模型python中所有东西都是对象 class对象:表示Python内置的类型和定义的类型instance对象(实例对象):表示由class对象创建的实例 1.1 对象间的关系 ...
- 转载--------Python中:self和__init__的含义 + 为何要有self和__init__
背景 回复:我写的一些Python教程,需要的可以看看,中SongShouJiong的提问: Python中的self,__init__的含义是啥?为何要有self,__init这些东西? 解释之前, ...
- (转载)Python中模块的发布与安装
模块(Module) Python中有一个概念叫做模块(module),这个和C语言中的头文件以及Java中的包很类似,比如在Python中要调用sqrt函数,必须用import关键字引入math这个 ...
- Python中list,tuple,dict,set的区别和用法
Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置数据类型功不可没,他们即是list, tuple, dict, set.这里对他们进行一个简明的总结. List ...
- Python中list,tuple,dict,set的区别和用法(转)
原文地址:http://www.cnblogs.com/soaringEveryday/p/5044007.html Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个 ...
- python中__init__()、__new__()、__call__()、__del__()用法
关于__new__()的用法参考: http://www.myhack58.com/Article/68/2014/48183.htm 正文: 一.__new__()的用法: __new__()是在新 ...
随机推荐
- InetSim配置使用
参考网址: http://techanarchy.net/2013/08/installing-and-configuring-inetsim/ https://blog.csdn.net/isins ...
- Altium Designer 放置机械孔
先放置一个圆弧,将圆选中:执行Tools -> Convert -> Create Board Cutout from Selected Primitives
- awk --- 常用技巧
一.每隔几行取出一个数,输出到另外一个文件 awk '{ if (NR % 9 ==1) {print NR, " => ", $0 } }' kp.txt > xy_ ...
- springboot学习四:整合mybatis
在application.properties加入配置 ## Mybatis 配置 mybatis.typeAliasesPackage=org.spring.springboot.domain my ...
- layer.open参数;layer.open关闭事件;layer.open关闭刷新;layer.open获取子页的值;layer.open调用子页面的方法
父页面 function layerOpen() { layer.open({ type: 2, shade: [0], title: "验收申请", area: ['1024px ...
- 文件系统扫描工具-fsck
文件系统扫描工具-fsck 注意的是fsck扫描文件系统时一定要在单用户模式.修复模式或把设备umount后进行.建议在单用户模式下运行.如果扫描正常运行中的系统,会造成系统文件损坏. fsck不仅可 ...
- react源码第一天
1.下载源码:github 16.7版本 2.找到笔记:https://react.jokcy.me/book/api/react.html#
- [HTML]音乐自动播放(兼容微信)
文件下载:音乐自动播放(兼容微信).zip <!DOCTYPE html> <html> <head> <meta charset="utf-8 ...
- 【手记】MTK之TASK创建及使用
首先来看看task的数据类型声明,在config\include\hal\task_config.h中对task和module类型进行了定义. /*************************** ...
- Python学习日记 --day2
Python学习日记 --day2 1.格式化输出:% s d (%为占位符 s为字符串类型 d为数字类型) name = input('请输入姓名') age = int(input('请输入年龄 ...