Skip to content
Advertisement

python – weird results with exponent operator in idle

I’m getting a strange result when squaring -1 in idle. What’s going on?

Unexpected result:

>>>| -1 ** 2
>>>| -1

Expected result:

>>>| pow(-1,2)
>>>| 1

>>>| my_var = -1
>>>| my_var **= 2
>>>| my_var
>>>| 1

Advertisement

Answer

Operator precedence (the - is a unary minus operator):

>>> -1 ** 2
-1
>>> -(1 ** 2)
-1
>>> (-1) ** 2
1
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement