1. What is a lambda function in Python?
- A lambda function is an anonymous function defined using the lambda keyword, which can take any number of arguments but can only have one expression.
2. How do you define a lambda function in Python?
- The syntax for defining a lambda function is: lambda arguments: expression
3. What is the purpose of using lambda functions?
- Lambda functions are used to create small, one-line functions without a name that can be used as arguments in higher-order functions.
4. Can a lambda function have multiple expressions?
- No, a lambda function can only have a single expression.
5. How do you call a lambda function?
- You can call a lambda function just like any other function by using its name followed by parentheses, passing the required arguments.
6. What is the difference between a lambda function and a regular function?
- Lambda functions are anonymous and can only have one expression, while regular functions have a name and can have multiple expressions.
7. Can you assign a lambda function to a variable?
- Yes, you can assign a lambda function to a variable and use that variable to call the lambda function.
8. What is the benefit of using lambda functions over regular functions?
- Lambda functions are often used in scenarios where a small function is required for a short period and defining a regular function would be unnecessary and less readable.
9. Can you use lambda functions with map(), filter(), and reduce() functions?
- Yes, lambda functions are commonly used with these higher-order functions to perform operations on iterables.
10. How do you sort a list using a lambda function?
- You can use the sorted() function and pass a lambda function as the key parameter to define the sorting criteria.
11. Can lambda functions have default arguments?
- No, lambda functions cannot have default arguments. They can only have the arguments specified in their definition.
12. How do you pass lambda functions as arguments to other functions?
- You can directly pass a lambda function as an argument to other functions, similar to passing regular functions.
13. Can you use lambda functions to define recursive functions?
- No, lambda functions are limited to single expressions and cannot be used to define recursive functions.
14. How do you create a lambda function that takes multiple arguments?
- You can specify multiple arguments separated by commas in the argument list of a lambda function.
15. Can you use lambda functions to modify global variables?
- No, lambda functions are limited to their own scope and cannot modify global variables directly.
16. How do you return a lambda function from another function?
- You can simply return the lambda function from the enclosing function, and it can be assigned to a variable or called directly.
17. Can you use lambda functions with conditional statements?
- Yes, you can use conditional expressions within a lambda function to perform different operations based on conditions.
18. How do you access variables from the enclosing scope inside a lambda function?
- Lambda functions can access variables from the enclosing scope, similar to regular functions, using closures.
19. Can lambda functions raise exceptions?
- Yes, lambda functions can raise exceptions just like regular functions.
20. What are the limitations of lambda functions in Python?
- Lambda functions are limited to single expressions, cannot contain statements or annotations, and are generally less readable for complex operations compared to regular functions.