1

在 python2**3中等于pow(2,3)
但不知何故-1**0不等于pow(-1,0)
第一个给出了-1的意外输出?

有人可以解释为什么吗?

4

1 回答 1

13

**优先于-,因此您的代码的评估方式如下:

  -(1**0)
= -(1)
= -1

要获得相同的答案,请添加括号:

(-1)**0

该文档在这里对**运算符进行了更多解释:http: //docs.python.org/2/reference/expressions.html#the-power-operator

于 2013-02-06T00:06:43.503 回答