Vas*_* Ch 4 python boolean-expression python-3.x
在 Python 中,我2>3 == False给出了False. 但我很期待True。如果我使用括号 ie(2>3) == False那么我得到True. 这背后的理论是什么?
2>3 == False
False
True
(2>3) == False
kay*_*ya3 6
这是因为 Python 的一个特性与其他编程语言相比非常不寻常,即您可以在一个序列中编写两个或多个比较,并且它具有数学家直观的含义。例如,像0 < 5 < 10isTrue因为0 < 5 and 5 < 10是这样的表达式True。
0 < 5 < 10
0 < 5 and 5 < 10
从文档:
比较可以任意链接;例如,x < y <= z等价于x < y and y <= z,除了y只计算一次(但在这两种情况下z,当x < y发现为假时根本不计算)。
x < y <= z
x < y and y <= z
y
z
x < y
因此,该表达式2 > 3 == False等价于2 > 3 and 3 == False,即False。
2 > 3 == False
2 > 3 and 3 == False
归档时间:
5 年,10 月 前
查看次数:
67 次
最近记录: