gpt4 book ai didi

python - 条件运算符不匹配

转载 作者:行者123 更新时间:2023-12-05 08:37:10 24 4
gpt4 key购买 nike

当我在 python 中执行以下代码时

print(0<5<2)

它给出 False 作为输出但在 C++ 中也是一样

std::cout<<(0<5<2);

返回真

为什么会出现这种矛盾?

最佳答案

在 Python 中,0 < 5 < 2相当于0 < 5 and 5 < 2 .
在 C++ 中,它等同于 static_cast<int>(0 < 5) < 2 .

Python 简写最初是受数学启发的,但是已经被广泛使用以至于你可以写出非常奇怪的东西,比如

>>> 1 < 5 in [2,3,4]
False
>>> 1 < 5 in [2,3,5]
True

让你所有的 friend 都感到困惑。

关于python - 条件运算符不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66337016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com