๐ Lesson 16: Python Lambda Functions – Anonymous & Quick Functions
Welcome to Lesson 16! Today we’ll explore lambda functions — small anonymous functions that can be defined in a single line. They are useful for short, one-time tasks that don’t require a formal function definition.
⭐ What You Will Learn in This Lesson
- Understand what lambda functions are
- Learn how to define and use lambda functions
- Use lambda functions inside other functions
- Leverage lambda with Python’s
map(),filter(), andreduce() - Keep your code clean, concise, and reusable
๐ฅ Who Is This Lesson For?
- Beginners who want to write quick, efficient functions
- Anyone working with higher-order functions like
map(),filter(), andreduce() - Developers who want to keep their code clean and concise
๐ฆ What Is a Lambda Function?
A lambda function is a function without a name, defined using the lambda keyword. Syntax:
lambda arguments: expression
๐งฉ 1. Basic Example
square = lambda x: x**2
print(square(5)) # Output: 25
๐งฉ 2. Lambda with Multiple Arguments
add = lambda a, b: a + b
print(add(3, 7)) # Output: 10
๐งฉ 3. Using Lambda Inside Functions
Lambda functions are often used as quick functions inside other functions:
def operate(x, func):
return func(x)
result = operate(5, lambda x: x*2)
print(result) # Output: 10
๐งฉ 4. Using Lambda with map(), filter(), reduce()
# map() example
numbers = [1, 2, 3, 4]
squared = list(map(lambda x: x**2, numbers))
print(squared) # [1, 4, 9, 16]
# filter() example
even = list(filter(lambda x: x % 2 == 0, numbers))
print(even) # [2, 4]
๐งฉ 5. Why Lambda Functions Matter
- Write short functions quickly without the need for a formal definition
- Useful with
map(),filter(),sort(), and other functional programming operations - Keep your code clean and concise, especially for one-time operations
- Improve code readability and avoid unnecessary function definitions
๐งช Practice
- Create a lambda function to cube a number.
- Use
map()and a lambda to double all numbers in a list. - Use
filter()and a lambda to extract all numbers greater than 5 from a list. - Write a lambda function inside another function that multiplies input by 3.
- Combine
map()with lambda to convert a list of strings to uppercase.
❌ Common Mistakes
- Misunderstanding lambda syntax and using it where a normal function is required
- Not using parentheses when necessary (e.g., with multiple arguments)
- Using lambda functions for complex operations (they are best for simple tasks)
❓ Frequently Asked Questions (FAQ)
1. Can a lambda function have more than one expression?
No, lambda functions can only contain a single expression. If you need more functionality, use a standard function.
2. What is the advantage of using lambda over a regular function?
Lambda functions are quick to define, useful for simple operations, and make the code more concise.
3. Where should I use lambda functions?
Use lambda functions when you need a simple operation for a short time, such as in map(), filter(), or sorting functions.
๐ What’s Next?
In the next lesson, you’ll learn about:
- Using decorators to modify functions
- Advanced function features
- Practical use cases for Python decorators
Comments
Post a Comment