布尔'和'在Python [英] boolean 'and' in Python

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

问题描述

有人能解释一下下面的测试??背后的原因

 >>> 1和True
真正
>>> {'富':'富'}和True
真正
>>> {}和True
{}
>>>


解决方案

在布尔运算的上下文,还当前pressions通过控制流语句中,以下值PTED为假间$ P $ :虚假,无,所有类型的数字零,空字符串和容器(包括字符串,元组,列表,字典,集和frozensets)。所有其他值都PTED为真际$ P $。

这位前pression x和y首先计算X;如果x是假的,其值被返回;否则,y求值并返回结果值。

这位前pression x或y首先计算X;如果x为真,其值被返回;否则,y求值并返回结果值。

有关进一步的参考阅读更多的布尔操作:的http:/ /docs.python.org/reference/ex$p$pssions.html#boolean-operations

Can somebody explain the reasoning behind the following tests ??

>>> 1 and True
True
>>> {'foo': 'Foo'} and True
True
>>> {} and True
{}
>>>

解决方案

In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true.

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.

For further reference read more on Boolean operations: http://docs.python.org/reference/expressions.html#boolean-operations

这篇关于布尔'和'在Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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