Lambda Function
Lambda Function
•Lambda functions are often used with functions like map(), filter(), and reduce().
•3.1 map()
•The map() function applies a function to all items in an iterable (like a list) and returns a map object.
•# Lambda functions for basic math operations •# Using lambda for functions that return other
•add = lambda x, y: x + y functions
•subtract = lambda x, y: x - y •def multiplier(n):
•multiply = lambda x, y: x * y • return lambda x: x * n
•divide = lambda x, y: x / y if y != 0 else 'Undefined' •
• •double = multiplier(2)
•print(add(10, 5)) # Output: 15 •print(double(5)) # Output: 10
•print(subtract(10, 5)) # Output: 5
•print(multiply(10, 5)) # Output: 50
•print(divide(10, 0)) # Output: Undefined
Exercises
• Exercise 1: Basic LambdaCreate a lambda function that takes two
arguments, a and b, and returns their product. Test it with a few
numbers.