Skip to content

create lambda-function.md #553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion contrib/advanced-python/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# List of sections

- [OOPs](OOPs.md)
- [Decorators/\*args/**kwargs](decorator-kwargs-args.md)
- [Lambda Function](lambda-function.md)
- [Working with Dates & Times in Python](dates_and_times.md)
- [Regular Expressions in Python](regular_expressions.md)
- [JSON module](json-module.md)
- [OOPs](OOPs.md)
88 changes: 88 additions & 0 deletions contrib/advanced-python/lambda-function.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Lambda Function

Lambda functions in Python are small, anonymous functions that can be created on-the-fly. They are defined using the `lambda` keyword instead of the `def` keyword used for regular functions. Lambda functions are typically used for simple tasks where a full-blown function definition is not necessary.

Here's an example of a lambda function that adds two numbers:

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

The above lambda function is equivalent to the following regular function:

```python
def add(x, y):
return x + y

print(add(3, 5)) # Output: 8
```

The difference between a regular function and a lambda function lies mainly in syntax and usage. Here are some key distinctions:

1. **Syntax**: Lambda functions are defined using the `lambda` keyword, followed by parameters and a colon, while regular functions use the `def` keyword, followed by the function name, parameters, and a colon.

2. **Name**: Lambda functions are anonymous; they do not have a name like regular functions. Regular functions are defined with a name.

3. **Complexity**: Lambda functions are suitable for simple, one-liner tasks. They are not meant for complex operations or tasks that require multiple lines of code. Regular functions can handle more complex logic and can contain multiple statements and lines of code.

4. **Usage**: Lambda functions are often used in situations where a function is needed as an argument to another function (e.g., sorting, filtering, mapping), or when you want to write concise code without defining a separate function.

Lambda functions are used primarily for convenience and brevity in situations where a full function definition would be overkill or too cumbersome. They are handy for tasks that require a small, one-time function and can improve code readability when used judiciously.

## Use Cases

1. **Sorting**: Lambda functions are often used as key functions for sorting lists, dictionaries, or other data structures based on specific criteria. For example:

```python
students = [
{"name": "Alice", "age": 20},
{"name": "Bob", "age": 18},
{"name": "Charlie", "age": 22}
]
sorted_students = sorted(students, key=lambda x: x["age"])
```

2. **Filtering**: Lambda functions can be used with filter() to selectively include elements from a collection based on a condition. For instance:

```python
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
```

3. **Mapping**: Lambda functions are useful with map() to apply a transformation to each element of a collection. For example:

```python
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x**2, numbers))
```

4. **Event Handling**: In GUI programming or event-driven systems, lambda functions can be used as event handlers to execute specific actions when an event occurs. For instance:

```python
button.clicked.connect(lambda: self.on_button_click(argument))
```

5. **Callback Functions**: Lambda functions can be passed as callback functions to other functions, especially when a simple operation needs to be performed in response to an event. For example:

```python
def process_data(data, callback):
# Process data
result = ...
# Execute callback function
callback(result)

process_data(data, lambda x: print("Result:", x))
```

6. **Anonymous Functions in Higher-Order Functions**: Lambda functions are commonly used with higher-order functions such as reduce(), which applies a rolling computation to sequential pairs of values in a list. For example:

```python
from functools import reduce
numbers = [1, 2, 3, 4, 5]
sum_of_numbers = reduce(lambda x, y: x + y, numbers)
```

These are just a few examples of how lambda functions can be applied in Python to simplify code and make it more expressive. They are particularly useful in situations where a small, one-time function is needed and defining a separate named function would be excessive.

In conclusion, **lambda functions** in Python offer a concise and powerful way to handle simple tasks without the need for full function definitions. Their versatility, especially in scenarios like sorting, filtering, and event handling, makes them valuable tools for improving code readability and efficiency. By mastering lambda functions, you can enhance your Python programming skills and tackle various tasks with elegance and brevity.