According to docs, **
has higher precedence than -
, thus your code is equivalent to -(2 ** 2)
. To get the desired result you could put -2
into parentheses
>>> (-2) ** 2
4
or use built-in pow
function
>>> pow(-2, 2)
4
or math.pow
function (returning float
value)
>>> import math
>>> math.pow(-2, 2)
4.0