Python

Function as Parameter in Python1 min read

A function can also receive another function as parameter. The following example includes the function calculuevalue which takes as parameters l and f. This function calculates for all the values x in the list l the value f (x). function_carre or function_cube are passed in parameters to the function calculuevalue which executes them.

>>>




The code you provided defines three functions: function_square, function_cube, and calculationnevalue. The function_square function takes in a variable x and returns the square of that variable. The function_cube function also takes in a variable x and returns the cube of that variable. The calculationnevalue function takes in a list l and a function f. It uses a list comprehension to apply the function f to every element in the list l, and assigns the result to the variable res.

In the last part of the code, a list l is defined as [0, 1, 2, 3] and printed. Then, the calculationnevalue function is used to square the elements of l and the result is assigned to l1, which is then printed. The same function is used again to cube the elements of l and the result is assigned to l2, which is then printed. The final output will be [0, 1, 4, 9] and [0, 1, 8, 27] respectively for l1 and l2.

Leave a Comment