Python Lambda Function
Tags: python, programming, tutorial, development
Published: 2023-06-19 15:42:04

In Python, developers use lambda functions as anonymous, inline functions that can be defined and called on the fly without needing a separate function. Programmers commonly use lambda functions to pass functions as arguments to higher-order functions or to define simple, one-time-use functions.

In this article, we’ll be exploring Python lambda functions in-depth and how they work. We’ll cover their syntax, use cases, advantages, and limitations. Furthermore, we’ll provide practical examples to help us understand how to use lambda functions effectively in Python.

## Python Lambda Function Syntax
People know the Lambda function in Python as an anonymous function that can have any number of arguments but can only contain expressions in its body.

Let’s take a closer look at the syntax of a lambda function and explore each characteristic in detail:
```
lambda arguments: expression
```
### Expressions Only
A lambda function can only contain expressions, which means it cannot include statements, such as print statements or assignments, in its body.

If we try to add a statement in a lambda function, it will raise a SyntaxError.
```
# Invalid syntax
lambda x, y: print(x + y)

# Valid syntax
lambda x, y: x + y
```

## One Line, One Shot
We write a lambda function as a single line of execution, which means we cannot include multiple lines of code in it.

However, we can include multiple expressions by separating them with commas.

Example of invalid Python lambda function syntax:
```
# Invalid syntax
lambda x, y:
    x += 1
    y += 1
    return x + y

# Valid syntax
lambda x, y: (x + 1, y + 1)
```
### Inference over Annotation
A lambda function does not support type annotations, which means we cannot specify the type of arguments or the return type of a lambda function.

However, Python can infer the type of arguments and the return type of a lambda function.
```
# Invalid syntax
lambda x: int: x + 1

# Valid syntax
lambda x: x + 1
```
### Lambda on the Fly (IIFE)
We can immediately invoke or execute a lambda function, which means we can call it as soon as we define it.

This is also known as an Immediately Invoked Function Expression (IIFE) in Python.
```
# Invalid syntax
(lambda x, y: x + y)(10, 20)

# Valid syntax
result = (lambda x, y: x + y)(10, 20)
print(result)
```
### Python Lambda Function Arguments
Python lambda functions can take any number of arguments, but they can only have a single expression. Developers often use lambda functions as a substitute for named functions when the function’s functionality is simple and a named function would be excessively complex.

The arguments to a lambda function are passed in the same way as they would be for a named function. They can be positional arguments, default arguments, variable-length arguments, or keyword arguments.

### No Arguments, No Problem
A lambda function in Python can have any number of arguments, including zero arguments. Lambda function with no arguments performs a simple operation without needing any inputs.
```
# a lambda function that returns the string "Hello, World!"
hello = lambda: "Hello, World!"
print(hello())  # Output: Hello, World!
```
We use lambda functions with no arguments when we need to perform a simple operation that does not require any input from the user. We can use them in scenarios where we need to pass a function as an argument to another function, but the function itself does not require any arguments.

### Positional Arguments
The lambda function takes positional arguments based on their position. This means that the first argument in the lambda function definition corresponds to the first argument passed to the function, the second argument in the lambda function definition corresponds to the second argument passed to the function, and so on.
```
# a lambda function that takes two positional arguments and returns their product
product = lambda x, y: x * y
print(product(2, 3))  # Output: 6
```
In the above example, we have defined a lambda function product that takes two positional arguments and returns their product. We have called this lambda function and passed two arguments 2 and 3.

### Default Arguments
The lambda function specifies a default value for an argument using default arguments when the caller does not pass the argument. If the caller does not pass an argument to a lambda function that has a default value, the function uses the default value.
```
# a lambda function that takes one positional argument and one default argument
power = lambda x, y=2: x ** y
print(power(3))  # Output: 9
print(power(3, 3))  # Output: 27
```
In the above example, we have defined a lambda function power that takes one positional argument and one default argument. If we do not pass the second argument, the default value will be 2. We have called this lambda function with only one argument in the first case, and with both arguments in the second case.

### Variable-Length Arguments
In a lambda function, we use variable-length arguments to accept an arbitrary number of arguments. We denote the variable-length argument by an asterisk (*) before the argument name. This enables the lambda function to accept any number of arguments that someone passes to it.
```
# a lambda function that takes a variable-length argument and returns the sum of all the numbers
sum_lambda = lambda *args: 0 if len(args) == 0 else args[0] + sum_lambda(*args[1:])
print(sum_lambda(1, 2, 3, 4))  # Output: 10
```
In the above example, we have defined a lambda function sum that takes a variable-length argument and returns the sum of all the numbers passed to it.

Continue Reading: [Python Lambda Function](https://hackthedeveloper.com/python-lambda-function/). [Find Blog on Google](https://www.google.com/search?q=python+lambda+function+site%3Ahackthedeveloper.com).