Python 2.7布尔运算符逻辑 [英] Python 2.7 Boolean Operators Logic

查看:69
本文介绍了Python 2.7布尔运算符逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆