13
Lesson 13
Syntax of lambda functions
Lambda functions in Python are a concise way to create small, anonymous functions without the need to formally define them using the def keyword. They are particularly useful for short operations that can be defined in a single line. Here's a detailed explanation of the syntax and usage of lambda functions.
1. Syntax of Lambda functions:
The general syntax for a lambda function is:
1. Syntax of Lambda functions:
The general syntax for a lambda function is:
lambda arguments: expression
- lambda Keyword: This indicates the start of a lambda function.
- arguments: This is a comma-separated list of parameters that the lambda function can accept. Just like regular functions, lambda functions can take multiple arguments.
- expression: This is a single expression that the lambda function evaluates and returns. Unlike regular functions, lambda functions do not have a return statement; the result of the expression is automatically returned.
2. Example of lambda functions:
Here are a few examples to illustrate the syntax and usage of lambda functions:
a. Simple Lambda Function
# A lambda function that adds 10 to the input number add_ten = lambda x: x + 10 result = add_ten(5) # Calling the lambda function print(result) # Output: 15
In this example:
- The lambda function add_ten takes one argument x and returns x + 10.
- When called with 5, it returns 15.
b. Lambda function with multiple arguments:
# A lambda function that multiplies two numbers multiply = lambda x, y: x * y result = multiply(4, 5) # Calling the lambda function print(result) # Output: 20
In this case:
- The lambda function multiply takes two arguments x and y and returns their product.
- When called with 4 and 5, it returns 20.
c. Lambda function in higher-order functions:
Lambda functions are often used with functions like map(), filter(), and reduce(). Here’s an example using map():
# Using lambda with map to square each number in a list numbers = [1, 2, 3, 4, 5] squared = list(map(lambda x: x ** 2, numbers)) print(squared) # Output: [1, 4, 9, 16, 25]
In this example:
- The lambda function takes one argument x and returns x squared.
- map() applies this lambda function to each element in the numbers list, resulting in a new list of squared values.
3. When to use Lambda functions:
Short Functions: Use lambda functions when you need a small function for a short period and do not want to formally define it.
Functional programming: They are particularly useful in functional programming contexts, where functions are passed as arguments to other functions.
Summary
- Lambda functions allow you to define simple functions in a single line without the overhead of a formal function definition.
- They are often used where functions are required temporarily, and you don’t want to name them.
- A lambda function can only contain a single expression and cannot include statements or multiple expressions.
By understanding the syntax and appropriate use cases for lambda functions, you can write more concise and functional-style Python code.