gpt4 book ai didi

python - 无法理解 ** 运算符(operator)的行为

转载 作者:太空宇宙 更新时间:2023-11-03 12:21:38 25 4
gpt4 key购买 nike

我突然遇到这个,我无法理解为什么会这样!

在 python 提示符下,在 3 之后使用 ** 运算符会给出错误的结果,如下所示。即,

>>> 2**2**2
16
>>> 3**3**3
7625597484987L
>>> 4**4**4
13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096L

然后我想我必须使用括号,所以我使用了它并给出了正确的结果。

>>>(3**3)**3
19683

但是 // 运算符支持并给出正确的结果 在这种操作中,就是

>>> 4//4//4
0
>>> 40//4//6
1

请帮助我理解。

最佳答案

** 是右结合的。从数学上讲,这是有道理的:333 等于 327,而不是 273

The documentation说明它是右结合的:

In an unparenthesized sequence of power and unary operators, the operators are evaluated from right to left.

关于python - 无法理解 ** 运算符(operator)的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14861094/

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