Exploring Lambda Functions

Tutorial 3 of 5

1. Introduction

The goal of this tutorial is to explore and understand Lambda functions in Python. A Lambda function is a small anonymous function that is defined using the lambda keyword, rather than the def keyword like standard functions.

By the end of this tutorial, you will be able to:
- Understand what a Lambda function is
- Learn how to define and use Lambda functions
- Appreciate the benefits of Lambda functions

Prerequisites: Basic understanding of Python programming and its syntax.

2. Step-by-Step Guide

A lambda function can take any number of arguments, but can only have one expression. The syntax of lambda function is:

lambda arguments: expression

Let's dissect the above syntax:
- lambda is the keyword that defines the lambda function.
- arguments are the inputs to the function (optional).
- : is used to separate the arguments and the expression.
- expression is a single line of code that the function is supposed to execute.

Lambda functions are used when a small anonymous function is required for a short period of time. They are very handy in scenarios where you need to pass a simple function as an argument.

Best Practices:

  • Use lambda functions when you need a small function for a short period of time.
  • Lambda functions are best used with functions like map(), filter(), reduce(), where you can leverage lambda's simplicity and conciseness.

3. Code Examples

Example 1: A simple Lambda function that adds 10 to the input number.

# Defining the lambda function
add_ten = lambda x: x + 10

# Using the lambda function
print(add_ten(5))  # Outputs: 15

Example 2: A Lambda function with multiple arguments.

# Defining the lambda function
multiply = lambda x, y: x * y

# Using the lambda function
print(multiply(4, 5))  # Outputs: 20

Example 3: Lambda function used with filter() function to filter out even numbers from a list.

# List of numbers
numbers = [1, 2, 3, 4, 5, 6]

# Using lambda function with filter()
even_numbers = filter(lambda x: x % 2 == 0, numbers)

# Converting the filter object to a list and printing
print(list(even_numbers))  # Outputs: [2, 4, 6]

In the above example, filter() function filters out the elements for which the lambda function returns True i.e., even numbers in this case.

4. Summary

In this tutorial, we learned about Lambda functions in Python. We explored their syntax, how to define them, and how to use them with functions like filter(). Lambda functions are a powerful feature of Python that allows us to create anonymous functions on the fly, and can make our code concise and efficient.

5. Practice Exercises

Exercise 1: Write a lambda function to square a number.

Exercise 2: Write a lambda function that takes two numbers and returns their product.

Exercise 3: Use a lambda function with the map() function to double all the numbers in a list.

Solutions:

Exercise 1:

square = lambda x: x ** 2
print(square(5))  # Outputs: 25

Exercise 2:

product = lambda x, y: x * y
print(product(4, 5))  # Outputs: 20

Exercise 3:

numbers = [1, 2, 3, 4, 5]
doubled = map(lambda x: x * 2, numbers)
print(list(doubled))  # Outputs: [2, 4, 6, 8, 10]

In the above code, map() function applies the lambda function to all elements in the list.