Code Explanation:
Line 1: Define combine function
def combine(f, g):
return lambda x: f(g(x))
Explanation:
This function takes two functions f and g as inputs.
It returns a new anonymous function (lambda) that takes an input x, applies g(x) first, and then applies f() to the result of g(x).
In other words, it returns f(g(x)) — this is called function composition.
Line 2: Define f
f = lambda x: x ** 2
Explanation:
f is a lambda function that squares its input.
Example: f(4) = 4 ** 2 = 16
Line 3: Define g
g = lambda x: x + 2
Explanation:
g is a lambda function that adds 2 to its input.
Example: g(3) = 3 + 2 = 5
Line 4: Compose functions using combine
h = combine(f, g)
Explanation:
h is now a new function created by combining f and g.
h(x) will compute f(g(x)), which is:
First: g(x) → add 2
Then: f(g(x)) → square the result
Line 5: Call and print h(3)
print(h(3))
Step-by-step Evaluation:
h(3) = f(g(3))
g(3) = 3 + 2 = 5
f(5) = 5 ** 2 = 25
So, h(3) = 25
Final Output:
25
.png)

0 Comments:
Post a Comment