以下比较产生True
:
>>> '1' in '11'
True
>>> ('1' in '11') == True
True
如果使用括号,我会得到一个 TypeError:
>>> '1' in ('11' == True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable
那么我怎样才能得到False
没有括号?
>>> '1' in '11' == True
False
Python手册说in
and ==
是平等的优先级 https://docs.python.org/reference/expressions.html#operator-precedence。因此,默认情况下,它们是从左到右评估的,但也需要考虑链接。您在上面输入的表达式 ('1' in '11' == True
) is actually被评价为...
('1' in '11') and ('11' == True)
这当然是False
。如果您不知道什么是链接,那么它可以让您做类似的事情......
if 0 < a < 1:
在 Python 中,这意味着你所期望的(“a 大于 0 但小于 1”)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)