与操作符混淆 [英] Confusion found with and operator

查看:77
本文介绍了与操作符混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用and运算符

代码

>>>0 and []
0
>>>[] and 0
[]
>>> 0 and ''
0
>>>'' and 0
''

我不知道在什么基础上我会根据元素的放置获得不同的结果.

I could not figure out about on what basis I m getting different result on the basis of placing of elements..

推荐答案

来自 上的文档:

From the docs on and:

表达式x和y首先计算x;如果x为假,则其值为 回来;否则,将评估y并得出结果值为 返回.

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.

在您的情况下,由于0''[]都计算为False,因此将返回每个表达式中的第一个值.

In your case, because 0, '', and [] all evaluate to False, the first value in each of your expressions is being returned.

这篇关于与操作符混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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