Need some clarification on the ** operator in Python -
i wanted start asking here this. given me part of exercise @ codeacademy.com , confused me better part of hour.
take @ following code block:
bool_one = 40 / 20 * 4 >= -4**2
now, evaluated being "8 >= 16" false.
however, codeacademy.com terminal says it's true. when started writing debug code lines, found issue in how "-4**2" gets evaluated. when run on terminal @ codeacademy on local linux system, "-4**2" in python comes out "-16"... contrary everything have learned in math classes every single calculator have run on. whether run "-4 * -4" or "-4^2" or even, using "x^y" key, "-4 [x^y] 2", still comes out "16". so, how python coming out "-16" "-4**2"?
can please clarify me?
tia.
if have -4 without parentheses, negative sign considered unary operator "multiply negative one." (-4)**2
16, because negative 4 squared, -4**2
uses normal order of operations (exponentiation before multiplication) , treats -(4**2)
.
hope helps!
edit: understand operator precedence, take @ handy list in docs:
http://docs.python.org/2/reference/expressions.html#operator-precedence
as can see, -
has less precedence **
Comments
Post a Comment