[go: up one dir, main page]

0% found this document useful (0 votes)
7 views16 pages

Lambda Function

The document provides an overview of lambda functions in Python, explaining their syntax, usage with built-in functions like map, filter, and reduce, and their application in sorting and data processing. It also discusses when to use lambda functions, their limitations, and includes examples and use cases. The content serves as a guide for understanding and implementing lambda functions effectively in programming.

Uploaded by

Chit Su Hlaing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

Lambda Function

The document provides an overview of lambda functions in Python, explaining their syntax, usage with built-in functions like map, filter, and reduce, and their application in sorting and data processing. It also discusses when to use lambda functions, their limitations, and includes examples and use cases. The content serves as a guide for understanding and implementing lambda functions effectively in programming.

Uploaded by

Chit Su Hlaing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Lambda Function

By Dr. Chit Su Hlaing


Outlines
1. What is a Lambda Function?

2. Syntax of a Lambda Function

3. Using Lambda Functions with Built-In Functions


o map()
o filter()
o reduce()

4. Using Lambda Functions in Sorting and Data Processing

5. When to Use Lambda Functions

6. Limitations of Lambda Functions

7. Examples and Use Cases


What is lambda function?
• Lambda functions, also known as anonymous functions in Python, are small, unnamed functions defined
with the lambda keyword. These functions are typically used for short, simple operations that are passed as
arguments to higher-order functions (functions that take other functions as arguments) or are otherwise
only used once in the code.
Example

Regular Function Lambda Function


•def add(x, y): • add = lambda x, y: x + y
• return x + y
Syntax
•The syntax for a lambda function is:

•lambda arguments: expression


 lambda is the keyword.
 arguments are the inputs to the function (you can have multiple).
 expression is a single expression evaluated and returned. You cannot have statements like print or return
here; only a single expression is allowed.
•Example

•# Lambda function to square a number


•square = lambda x: x ** 2
•print(square(5)) # Output: 25
Using Lambda Functions with Built-In Functions

•Lambda functions are often used with functions like map(), filter(), and reduce().
•3.1 map()
•The map() function applies a function to all items in an iterable (like a list) and returns a map object.

•# Squaring numbers in a list


•numbers = [1, 2, 3, 4]
•squared = list(map(lambda x: x ** 2, numbers))
Using Lambda Functions with Built-In
Functions
•3.2 filter()
•The filter() function filters elements in an iterable based on a condition, returning a filter object.

•# Filtering even numbers


•numbers = [1, 2, 3, 4, 5, 6]
•even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
•print(even_numbers) # Output: [2, 4, 6]
Using Lambda Functions with Built-In
Functions
•3.3 reduce()
•The reduce() function, from the functools module, applies a function cumulatively to the items in an iterable,
reducing it to a single result.

•from functools import reduce



•# Summing up numbers
•numbers = [1, 2, 3, 4]
•total = reduce(lambda x, y: x + y, numbers)
•print(total) # Output: 10
Using Lambda Functions in Sorting and Data
Processing
•Lambda functions are helpful in custom sorting, especially with complex data structures.
•Example: Sorting by Key
•# Sorting a list of dictionaries by age
•students = [{'name': 'Alice', 'age': 20}, {'name': 'Bob', 'age': 25}, {'name': 'Charlie', 'age': 23}]
•students_sorted = sorted(students, key=lambda student: student['age'])
•print(students_sorted)
•# Output: [{'name': 'Alice', 'age': 20}, {'name': 'Charlie', 'age': 23}, {'name': 'Bob', 'age': 25}]

•Example: Sorting by Multiple Keys


•# Sorting by age and then by name
•students_sorted = sorted(students, key=lambda student: (student['age'], student['name']))
•print(students_sorted)
When to Use Lambda Functions
•One-off functions: When you need a function for a single, short use.
 Higher-order functions: As arguments to functions like map, filter, and sorted.
 Data processing pipelines: Lambda functions help in data transformation and custom sorting.
Limitations of Lambda Functions
 Single expression limitation: Lambda functions can only contain one expression and are unsuitable for
more complex logic.
 Readability: They can reduce readability when used excessively or in complex expressions.
 Lack of documentation: Lambda functions don't have names, making them difficult to document or debug
in larger codebases.
Examples and Use Cases

•Example 1: Basic Calculator •Example 2: Nested Lambda Functions

•# Lambda functions for basic math operations •# Using lambda for functions that return other
•add = lambda x, y: x + y functions
•subtract = lambda x, y: x - y •def multiplier(n):
•multiply = lambda x, y: x * y • return lambda x: x * n
•divide = lambda x, y: x / y if y != 0 else 'Undefined' •
• •double = multiplier(2)
•print(add(10, 5)) # Output: 15 •print(double(5)) # Output: 10
•print(subtract(10, 5)) # Output: 5
•print(multiply(10, 5)) # Output: 50
•print(divide(10, 0)) # Output: Undefined
Exercises
• Exercise 1: Basic LambdaCreate a lambda function that takes two
arguments, a and b, and returns their product. Test it with a few
numbers.

You might also like