Python 2.7布尔运算符逻辑 [英] Python 2.7 Boolean Operators Logic
问题描述
我目前正在学习Python 2.7,并且遇到过Equality和Boolean运算符
I am currently in the course of learning Python 2.7 and have come across the Equality and Boolean operators
我的问题是:
为什么 False and 1
是 False
,但是 True and 1
是 1
同样, False或1
是 1
,但是 True或1
是 True
有人能解释一下为什么会这样吗
Can someone kindly explain why this is happening
非常感谢
推荐答案
and 会返回它看到的第一个"falsy"(假,零,空字符串或列表等)值,或者如果没有错误,则为最终值.甚至无法评估其他值,因为它们无法更改结果.
and returns the first 'falsy' (False, zero, empty string or list, etc.) value it sees, or the final value if none were falsy. Further values are not even evaluated, since they can't change the result.
或同样返回它看到的第一个真实"值(True,非零,非空字符串或列表等)(如果没有,则返回最后一个),并且不评估其余部分.
or likewise returns the first 'truthy' (True, non-zero, non-empty string or list, etc.) value it sees (or the final one if there were none), and doesn't evaluate the rest.
这种行为有时比仅严格返回True或False更为方便.
This behavior is sometimes more convenient than strictly returning only True or False.
这篇关于Python 2.7布尔运算符逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!