๐ฐ Machine Learning & Artificial Intelligence Free Resources
๐ฐ Learn Data Science, Deep Learning, Python with Tensorflow, Keras & many more
For Promotions: @love_data
Post #1865
3.01K
๐ Welcome back to our AI Engineer Roadmap! โค๏ธ
In the previous posts, we learned about functions and solved some tricky function-based MCQs. Now let's move to the next topic in Python fundamentals.
๐ Phase 1: Programming Fundamentals
๐ Topic 12: Lambda Functions
A Lambda Function is a small, anonymous function that can be written in a single line.
Unlike regular functions created using def, lambda functions are created using the lambda keyword.
Why Do We Need Lambda Functions?
Lambda functions are useful when:
โข You need a small function for a short task
โข You don't want to define a full function using def
โข You need a function temporarily
โข You're working with functions like map(), filter(), and sorted()
1. Creating a Lambda Function
A normal function:
The same function using lambda:
Output: 25
Lambda Syntax
For example:
โข
โข
โข
2. Lambda with Multiple Arguments
A lambda function can accept multiple arguments.
Output: 30
Output: 20
3. Lambda with if-else
Lambda functions can also contain conditional expressions.
Output:
4. Lambda with map()
map() applies a function to every item in an iterable.
Output: [1, 4, 9, 16, 25]
5. Lambda with filter()
filter() selects elements based on a condition.
Output: [2, 4, 6]
6. Lambda with sorted()
Lambda functions are very useful when sorting complex data.
Example:
Output: [('Amit', 70), ('Rahul', 80), ('Priya', 95)]
Here,
Lambda vs Regular Function
โข Regular function:
โข Lambda function:
Both produce the same result.
When Should You Use Lambda?
Use lambda when:
โ The function is very small
โ The operation is simple
โ You need the function temporarily
โ You're working with map(), filter(), or sorted()
Avoid lambda when:
โ The logic becomes complicated
โ The function needs multiple statements
โ A meaningful function name and documentation would improve readability
In those situations, a regular def function is usually better.
Real-World AI/Data Example
Lambda functions are commonly used while preprocessing data.
Output: [0.45, 0.67, 0.82, 0.91, 0.38]
This kind of transformation can be useful when preparing data before feeding it into a Machine Learning model.
Common Beginner Mistakes
โ Trying to put complex logic into a lambda
โ Forgetting that a lambda automatically returns its expression
โ Confusing map() and filter()
Key Takeaways
โข Lambda functions are small anonymous functions
โข They are created using the lambda keyword
โข They can accept multiple arguments
โข They return the result of a single expression
โข They're especially useful with map(), filter(), and sorted()
โข For complex logic, prefer a regular def function
โก๏ธ Double Tap โค๏ธ For More
In the previous posts, we learned about functions and solved some tricky function-based MCQs. Now let's move to the next topic in Python fundamentals.
๐ Phase 1: Programming Fundamentals
๐ Topic 12: Lambda Functions
A Lambda Function is a small, anonymous function that can be written in a single line.
Unlike regular functions created using def, lambda functions are created using the lambda keyword.
Why Do We Need Lambda Functions?
Lambda functions are useful when:
โข You need a small function for a short task
โข You don't want to define a full function using def
โข You need a function temporarily
โข You're working with functions like map(), filter(), and sorted()
1. Creating a Lambda Function
A normal function:
def square(x):
return x * x
The same function using lambda:
square = lambda x: x * x
print(square(5))
Output: 25
Lambda Syntax
lambda arguments: expression
For example:
lambda x: x + 10โข
lambda โ Keyword used to create the functionโข
x โ Argumentโข
x + 10 โ Expression that is returned2. Lambda with Multiple Arguments
A lambda function can accept multiple arguments.
add = lambda a, b: a + b
print(add(10, 20))
Output: 30
multiply = lambda x, y: x * y
print(multiply(5, 4))
Output: 20
3. Lambda with if-else
Lambda functions can also contain conditional expressions.
check = lambda x: "Even" if x % 2 == 0 else "Odd"
print(check(10))
print(check(7))
Output:
Even
Odd
4. Lambda with map()
map() applies a function to every item in an iterable.
numbers = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x * x, numbers))
print(squares)
Output: [1, 4, 9, 16, 25]
5. Lambda with filter()
filter() selects elements based on a condition.
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)
Output: [2, 4, 6]
6. Lambda with sorted()
Lambda functions are very useful when sorting complex data.
Example:
students = [
("Rahul", 80),
("Priya", 95),
("Amit", 70)
]
students.sort(key=lambda x: x[1])
print(students)
Output: [('Amit', 70), ('Rahul', 80), ('Priya', 95)]
Here,
lambda x: x[1] tells Python to sort using the second element of each tuple.Lambda vs Regular Function
โข Regular function:
def square(x):
return x * x
โข Lambda function:
square = lambda x: x * x
Both produce the same result.
When Should You Use Lambda?
Use lambda when:
โ The function is very small
โ The operation is simple
โ You need the function temporarily
โ You're working with map(), filter(), or sorted()
Avoid lambda when:
โ The logic becomes complicated
โ The function needs multiple statements
โ A meaningful function name and documentation would improve readability
In those situations, a regular def function is usually better.
Real-World AI/Data Example
Lambda functions are commonly used while preprocessing data.
scores = [45, 67, 82, 91, 38]
updated_scores = list(map(lambda x: x / 100, scores))
print(updated_scores)
Output: [0.45, 0.67, 0.82, 0.91, 0.38]
This kind of transformation can be useful when preparing data before feeding it into a Machine Learning model.
Common Beginner Mistakes
โ Trying to put complex logic into a lambda
โ Forgetting that a lambda automatically returns its expression
โ Confusing map() and filter()
Key Takeaways
โข Lambda functions are small anonymous functions
โข They are created using the lambda keyword
โข They can accept multiple arguments
โข They return the result of a single expression
โข They're especially useful with map(), filter(), and sorted()
โข For complex logic, prefer a regular def function
โก๏ธ Double Tap โค๏ธ For More
- โค 16
