What will be the output of the following Python code?L = [lambda x: x ** 2, lambda x: x ** 3, lambda x: x ** 4] for f in L: print(f(3))
Question
What will be the output of the following Python code?
L = [lambda x: x ** 2,
lambda x: x ** 3,
lambda x: x ** 4]
for f in L:
print(f(3))
Solution
The given Python code defines a list of lambda functions, each of which takes an argument x and raises it to the power of 2, 3, and 4 respectively. Then it iterates over this list, applying each function to the number 3.
Here's the step-by-step breakdown:
-
The first function in the list is
lambda x: x ** 2
. This function squares its input. So,f(3)
will output3 ** 2
which is9
. -
The second function in the list is
lambda x: x ** 3
. This function cubes its input. So,f(3)
will output3 ** 3
which is27
. -
The third function in the list is
lambda x: x ** 4
. This function raises its input to the power of 4. So,f(3)
will output3 ** 4
which is81
.
So, the output of the code will be:
9
27
81
Similar Questions
What does the following lambda function return? f = lambda x: x*2 print(f(3)) Answer( Please choose a correct answer ) CLEAR2369
What will be the output of the following statement?l = [1,2,3,4,5]ans = 3 in lprint(ans)
What will be the output of the following Python code?l=[[1, 2, 3], [4, 5, 6]]for i in range(len(l)): for j in range(len(l[i])): l[i][j]+=10print(l)
What will be the output of the following code?i = 3for i in range (4) print (i)
What will be the output of the following program:r = lambda q: q * 2s = lambda q: q * 3x = 2x = r(x)x = s(x)x = r(x)print (x)
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.