Can someone explain to me the result of this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can someone explain to me the result of this code?

def polynomial(x): return x**2 + 5*x + 4 print(polynomial(-4)) The result gives zero But I do not understand why? It gives me -32 I appreciate the contributions; D

11th Jun 2019, 2:37 PM
Viviana Bernal
Viviana Bernal - avatar
2 Answers
+ 3
Let's see how the function works for -4 argument. First replace x with - 4 in function body code: return - 4**2+5*(-4)+4 And we know: -4^2+5×(-4)+4=16-20+4=0 So the function returns 0 and print function shows it.
11th Jun 2019, 2:52 PM
ΛM!N
ΛM!N - avatar
+ 6
Unary minus is slower than ** in Python. So if we write -4 ** 2 it is evaluated as -(4 ** 2). If we put -4 into a variable, that basically lets it be evaluated like it was in parentheses, so (-4) ** 2.
11th Jun 2019, 3:06 PM
HonFu
HonFu - avatar