Lambda Functions (2)
Lambda Functions (2)
Lambda Functions
Topics to be covered:
What are Lambda Functions
Working Process of Lambda Function
Lambda Functions Example
Analogy of Lambda Function
Analogy with Lambda Functions Examples
Unlike regular functions defined using the def keyword, lambda functions are often used for short-term
operations and are defined in a single line.
Here, arguments are the input parameters, and expression is the operation to be performed using those
parameters. Lambda functions can have any number of input parameters but can only have one expression.
Example:
add = lambda x, y: x + y
2. Arguments:
4. Function Object:
5. Function Call:
When you want to use the lambda function, you call it like any other function, providing values for its
arguments
In our example, calling `add(3, 4)` means substituting `x` with `3` and `y` with `4` in the lambda expression.
6. Expression Execution:
7. Result:
The result of the lambda expression becomes the result of the function call
add = lambda x, y: x + y
result = add(3, 4)
Lambda functions are useful for short-term, specific tasks, where you don't need a full-fledged function with a
name and extensive logic.
add = lambda x, y: x + y
result = add(3, 4)
print(result) # Output: 7
is_even = lambda x: x % 2 == 0
result = square(5)
print(result) # Output: 25
print(sorted_words)
print(sequence)