+ 1
#named function def polynomial(x): return x**2 + 5*x + 4 print(polynomial(-4)) #lambda print((lambda x: x**2 + 5*x + 4) (-4
how does it works ? the line of processing please?
1 Réponse
+ 3
I guess you mean how lambda functions work.
If you have some background on mathematical functions,
lambda x: x**2 + 5*x + 4
could be though as a single variable function:
f(x) = x² + 5x + 4
Now (lambda x: x**2 + 5*x + 4)(-4) is like evaluating f(-4).
If you don't have this math knowledge, think that the lambda function is like defining a procedure to compute some value, depending on a dummy variable x (the one that's left of the colon :), and the procedure itself is the one that's right of the colon. So to compute (lambda x: x**2 + 5*x + 4)(-4), just replace -4 wherever x appears:
(lambda x: x**2 + 5*x + 4)(-4) =
(-4)**2 + 5*(-4) + 4 =
16 - 20 + 4 =
0