Python布尔比较,现为 [英] Python boolean comparison and is
问题描述
Python 3.6.2控制台:
Python 3.6.2 console:
>>> 11 > 0 is True
False
但是
>>> 0 is True
False
>>> 11 > False
True
那么,为什么11 > 0 is True
是False
?
推荐答案
This is an example of comparison chaining since both >
and is
are comparison operators.
比较可以任意链接,例如
x < y <= z
是等效的 到x < y and y <= z
,只对y
进行一次评估(但在两次评估中均 发现x < y
为假时完全不评估z
的情况.)
Comparisons can be chained arbitrarily, e.g.,
x < y <= z
is equivalent tox < y and y <= z
, except thaty
is evaluated only once (but in both casesz
is not evaluated at all whenx < y
is found to be false).
通常,如果a, b, c, …, y, z
是表达式,而op1, op2, …, opN
是表达式
比较运算符,则a op1 b op2 c ... y opN z
等效于
a op1 b and b op2 c and ... y opN z
,除了每个表达式都是
最多评估一次.
Formally, if a, b, c, …, y, z
are expressions and op1, op2, …, opN
are
comparison operators, then a op1 b op2 c ... y opN z
is equivalent to
a op1 b and b op2 c and ... y opN z
, except that each expression is
evaluated at most once.
因此,它等效于:
>>> (11 > 0) and (0 is True)
False
这篇关于Python布尔比较,现为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!