Shashank Shekhar
Shashank Shekhar

Reputation: 4178

Why is exponentiation ** different from pow() ? - Python

In python 2**3 equates to pow(2,3)
but somehow -1**0 is not equal to pow(-1,0)
The first gives an unexpected output of -1 ?

Can somebody explain why ?

Upvotes: 1

Views: 169

Answers (1)

Blender
Blender

Reputation: 298226

** takes precedence over the -, so your code is being evaluated like this:

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

To get the same answer, add parentheses:

(-1)**0

The documentation explains the ** operator a little more right here: http://docs.python.org/2/reference/expressions.html#the-power-operator

Upvotes: 13

Related Questions