What is the output of the following code snippet?def apply_func(L, func): return [func(x) for x in L]apply_func([7.1, 17.9], lambda x: x**3)
Question
What is the output of the following code snippet?
def apply_func(L, func):
return [func(x) for x in L]
apply_func([7.1, 17.9], lambda x: x**3)
Solution
The given code defines a function apply_func
that takes a list L
and a function func
as arguments. It applies func
to each element in L
and returns a new list with the results.
The function apply_func
is then called with a list [7.1, 17.9]
and a lambda function lambda x: x**3
as arguments. The lambda function raises its input to the power of 3.
So, the output of the code will be a list where each element is the cube of the corresponding element in the input list.
Let's calculate it:
- The cube of 7.1 is
7.1**3 = 357.911
- The cube of 17.9 is
17.9**3 = 5733.259
So, the output of the code will be [357.911, 5733.259]
.
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 Python code?L = [lambda x: x ** 2, lambda x: x ** 3, lambda x: x ** 4] for f in L: print(f(3))
What is the output of the following function calldef fun1(name, age=20): print(name, age) fun1('Emma', 25)Emma 25Emma 20Emma 22
What is the output of the following code snippet?exp = lambda x: x % 3print(exp(2))
What would be the output of the following Python statement?def func(i): print('Hello world', i)
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.