Python Lambda Functions Tutorial 🎯

beginner
25 min

Python Lambda Functions Tutorial 🎯

Welcome to our deep dive into Python Lambda Functions! 📝

In this tutorial, we'll explore:

  1. Understanding Lambda Functions 💡
  2. When to use Lambda Functions
  3. How to Define a Lambda Function
  4. Lambda Function Example
  5. Advanced Lambda Function Examples
  6. Exploring Lambda Function Types
  7. Lambda Function Quiz 💡

1. Understanding Lambda Functions 💡

Lambda functions, also known as anonymous functions, are small, standalone functions that are defined without a name. They are useful when we need to perform a small task without creating a full-blown function.

2. When to use Lambda Functions

  • When a function is small: If a function has only a few lines of code, it's better to use a lambda function for simplicity.
  • When you need a function for a short period: Lambda functions are ideal for one-off tasks or temporary functions.
  • When working with libraries: Many libraries expect functions to be passed as parameters, and lambda functions are a convenient way to create these.

3. How to Define a Lambda Function 💡

In Python, lambda functions are defined using the lambda keyword, followed by a list of arguments, an equal sign, and the function's body.

python
## Define a lambda function that adds two numbers add = lambda x, y: x + y

4. Lambda Function Example 💡

Let's create a lambda function that calculates the square of a number:

python
## Define a lambda function that calculates the square of a number square = lambda n: n * n ## Using the lambda function to calculate the square of 5 result = square(5) print(result) # Output: 25

5. Advanced Lambda Function Examples 💡

Example 1: Using Lambda Function with Built-in Functions

We can use lambda functions with built-in functions like map(), filter(), and reduce(). Let's create a lambda function that finds the sum of all odd numbers from a list:

python
## Define a lambda function that checks if a number is odd is_odd = lambda n: n % 2 != 0 ## Define a list of numbers numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] ## Using filter() with the lambda function to find odd numbers odd_numbers = filter(is_odd, numbers) ## Using list() to convert the filter object into a list odd_numbers_list = list(odd_numbers) ## Using sum() to calculate the sum of odd numbers sum_of_odds = sum(odd_numbers_list) print(sum_of_odds) # Output: 25

Example 2: Using Lambda Function with map() and reduce()

Let's create a lambda function that calculates the product of all numbers in a list:

python
## Define a lambda function that takes two arguments and returns their product multiply = lambda x, y: x * y ## Define a list of numbers numbers = [1, 2, 3, 4, 5] ## Using map() to apply the lambda function to each pair of numbers in the list product_pairs = map(multiply, numbers, numbers) ## Using list() to convert the map object into a list product_pairs_list = list(product_pairs) ## Using functools.reduce() to calculate the product of all numbers in the list import functools product = functools.reduce(lambda x, y: x * y, product_pairs_list) print(product) # Output: 120

6. Exploring Lambda Function Types 💡

Python lambda functions are always anonymous and do not have an explicit return statement. They are equivalent to a function defined with the def keyword, but have some restrictions:

  • Lambda functions can have up to one statement.
  • If a lambda function has multiple statements, they must be enclosed in parentheses and separated by commas.
  • Lambda functions can only be used as a simple function, a function argument, or inside another function.

7. Lambda Function Quiz 💡

Quick Quiz
Question 1 of 1

What is a lambda function in Python?

We hope you enjoyed learning about Python Lambda Functions! Stay tuned for more exciting topics at CodeYourCraft. Happy coding! 🚀💻🎉