gpt4 book ai didi

python - 谁能解释为什么 True, True == (True, True) 的输出是 (True, False)?

转载 作者:行者123 更新时间:2023-12-03 23:07:29 24 4
gpt4 key购买 nike

我试图用 Python 进行类似下面的代码的比较,但对产生的输出感到困惑。

谁能解释为什么输出是这样的?

>>> True, True == True, True
(True, True, True)
>>> True, True == (True, True)
(True, False)

最佳答案

因为运算符优先。 ==具有比 , 更高的优先级,所以第一个表达式被当作你写的

True, (True == True), True

您的第二个表达式被视为
True, (True == (True, True))

如果要比较 ==的两侧元素方面,你需要括号两边:
(True, True) == (True, True)

这将返回 True .

请注意,逗号是 not strictly an operator ,所以为了理解这种行为,它已经足够接近了。

关于python - 谁能解释为什么 True, True == (True, True) 的输出是 (True, False)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61482786/

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