一直以来,我觉得python中and
和or
的返回值是True
或False
。python
看 《Dive into Python》这本书时,发现他们并不返回布尔值,而是返回他们实际进行比较的值之一。linux
根据本身已经掌握的知识,设想下以下代码的运算结果:bash
'a' and 'b' '' and 'b' 'a' and 'b' and 'c'
根据python的定义,在没有看这部份内容以前,我认为答案分别为True
,False
,True
。其实运行以后才发现应该是这样的:code
Python 2.7.13+ (default, Jul 19 2017, 18:15:03) [GCC 6.4.0 20170704] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 'a' and 'b' 'b' >>> '' and 'b' '' >>> 'a' and 'b' and 'c' 'c' >>>
缘由以下:orm
0
, ''
, []
, {}
, None在布尔表达式环境下为假,其余任何东西都为真'a' and 'b'
'' and 'b'
相似的。or
的规则以下:get
本文做者: Yarving Liu
本文连接: http://yarving.historytale.co...
版权声明: 本博客全部文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!博客