+ 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
2 odpowiedzi
+ 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.
+ 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.