**What is “lambda” in Python**

**Basic Syntax**

`lambda arguments: expression`

**Easy Guide**

In Python, a `lambda`

function is a small, anonymous function that can take any number of arguments but can only have one expression.

The term “anonymous” means that the function is without a name.

**Unlike a regular function that you define using the def keyword, a lambda**

**function is defined without a name.**

While this might sound strange at first, it allows for quick, throwaway function definitions that can make your code more streamlined.

**For example, a lambda function that adds two numbers together would look like this:**

```
add = lambda x, y: x + y
print(add(5, 3)) # Outputs: 8
```

**In this example, x and y are the arguments, and x + y is the expression that gets evaluated and returned.**

This `lambda`

function behaves just like a regular function that is defined with `def`

. You can use it to encapsulate functionality that you don’t want to repeat, or to create small, one-off functions that you only need in one place in your code.

**Why Use Lambda?**

**The beauty of lambda functions is that they help to keep your code concise and readable, especially when used in conjunction with functions like map(), filter(), and reduce(), which can take a function as an argument.**

However, they can also make your code harder to understand if overused, as `lambda`

functions lack a function name, which can provide a hint about what the function does.

**Difference from Regular Functions**

The main difference between a `lambda`

function and a regular function in Python is that a `lambda`

function is anonymous, meaning it doesn’t have a name.

This is unlike a regular function, which is defined using the `def`

keyword and must be given a name.

Here’s an example of a regular function that does the same thing as our `lambda`

function from earlier:

```
def add(x, y):
return x + y
print(add(5, 3)) # Outputs: 8
```

**As you can see, the def function requires more code to do the same thing as the lambda function.**

```
add = lambda x, y: x + y
print(add(5, 3)) # Outputs: 8
```

**However, def functions are more flexible than lambda functions.**

They can contain multiple expressions and statements, including complex control flow structures like loops and `if`

/`else`

branches, whereas a `lambda`

function can only contain a single expression.

**Example of “lambda”**

Let’s look at a few more examples of `lambda`

functions in Python.

**1. Function that squares a number**

```
square = lambda x: x ** 2
print(square(4)) # Outputs: 16
```

In this example, `x`

is the argument, and `x ** 2`

is the expression that gets evaluated and returned.

**2. Function that concatenates two strings**

```
concat = lambda a, b: a + b
print(concat("Hello, ", "World!")) # Outputs: Hello, World!
```

Here, `a`

and `b`

are the arguments, and `a + b`

is the expression that gets evaluated and returned.

**3. Function used in conjunction with the **`filter()`

function

`filter()`

function```
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers)) # Outputs: [2, 4, 6]
```

In this example, the `lambda`

function is used as the first argument to `filter()`

.

**The lambda function returns True for even numbers and False for odd numbers.**

`filter()`

uses this function to filter out the odd numbers from the list.