Python boolean comparison and is(Python布尔比较和是)
问题描述
Python 3.6.2 控制台:
Python 3.6.2 console:
>>> 11 > 0 is True
False
但是
>>> 0 is True
False
>>> 11 > False
True
那么,为什么 11 >0 是真
是 假
?
So, why 11 > 0 is True
is False
?
推荐答案
这是一个比较链接 因为 >
和 is
都是比较运算符.
This is an example of comparison chaining since both >
and is
are comparison operators.
比较可以任意链接,例如,x <y <= z
是等价的到 x <y 和 y <= z
,除了 y
只计算一次(但在这两个当 x
x
时根本不计算
被发现是假的).z
y
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布尔比较和是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python布尔比较和是


基础教程推荐
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 包装空间模型 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01