Python Programming - Lambda Functions

Exercise : Lambda Functions - General Questions
  • Lambda Functions - General Questions
16.
In Python, what does the following code represent?
lambda x: x ** 2
A lambda function squaring its input
A lambda function with two parameters
A lambda function taking the square root of its input
A lambda function doubling its input
Answer: Option
Explanation:
The lambda function lambda x: x ** 2 represents a function that squares its input (multiplies it by itself).

17.
What is the purpose of the following code using the enumerate() function and a lambda function?
my_list = [1, 2, 3, 4, 5]
result = list(map(lambda x: x[0] * x[1], enumerate(my_list)))
To apply the lambda function to each element of an iterable
To filter elements from an iterable based on the lambda function's condition
To create a sorted list based on the lambda function's values
To create a list of products of each element and its index in the iterable
Answer: Option
Explanation:
The code uses enumerate() to get tuples of index and element, and the lambda function multiplies the index (x[0]) by the element (x[1]).

18.
What is the result of the following Python code?
my_list = [1, 2, 3, 4, 5]
result = list(map(lambda x: x * 2, my_list))
print(result)
[1, 2, 3, 4, 5]
[2, 4, 6, 8, 10]
[1, 4, 9, 16, 25]
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
Answer: Option
Explanation:
The map() function applies the lambda function lambda x: x * 2 to each element of my_list, doubling each value.

19.
Which built-in function can be used to find the minimum value in an iterable using a lambda function?
sum()
min()
reduce()
filter()
Answer: Option
Explanation:
The min() function can be used with a lambda function to find the minimum value in an iterable based on the lambda function's criteria.

20.
How is a lambda function different from a regular function in terms of defining and using it?
Lambda functions are defined using the keyword lambda and can be used immediately, while regular functions are defined with def.
Regular functions are defined using the keyword lambda and can be used immediately, while lambda functions are defined with def.
Both lambda and regular functions are defined using the keyword func and can be used interchangeably.
Lambda functions are defined using the keyword def and require parentheses around parameters, while regular functions use lambda without parentheses.
Answer: Option
Explanation:
Lambda functions are defined using the lambda keyword and are typically used for short-term, specific tasks without the need for a formal function definition.