Lambda Functions: The Secret Functions
Hello, young coder! Today, we’re going to uncover a secret in Python called lambda functions. Imagine a superhero with a special power that they use only when needed, without anyone knowing their real identity. That’s exactly what a lambda function is – a small, anonymous (secret) function that you can create quickly to do something specific.
What is a Lambda Function?
In Python, a lambda function is a tiny, unnamed function. Instead of using the def
keyword to define a function, we use the lambda
keyword. It’s like a quick, one-time use function.
Here’s how we normally define a function:
def add(a, b):
return a + b
And here’s how we do the same thing with a lambda function:
add = lambda a, b: a + b
Both of these do the same thing – they add two numbers together. The difference is that the second one is using a lambda function.
Why Use Lambda Functions?
Lambda functions are great for small tasks where you don’t need to define a full function. They are often used when you need a simple function for a short period.
How to Create a Lambda Function
A lambda function in Python looks like this:
lambda arguments: expression
- lambda: This keyword is used to declare a lambda function.
- arguments: These are the inputs to the function, just like in a regular function.
- expression: This is what the function returns. It can only be a single line of code.
Examples of Lambda Functions
Let’s look at some examples to understand lambda functions better.
- Adding Two Numbers
add = lambda x, y: x + y
print(add(5, 3)) # Output: 8
- Multiplying Two Numbers
multiply = lambda x, y: x * y
print(multiply(4, 2)) # Output: 8
- Checking if a Number is Even
is_even = lambda x: x % 2 == 0
print(is_even(4)) # Output: True
print(is_even(7)) # Output: False
- Finding the Length of a String
length = lambda s: len(s)
print(length("hello")) # Output: 5
Using Lambda Functions with Built-in Functions
Lambda functions are often used with Python’s built-in functions like map()
, filter()
, and sorted()
.
- map(): Applies a function to all items in an input list.
numbers = [1, 2, 3, 4]
squared = list(map(lambda x: x * x, numbers))
print(squared) # Output: [1, 4, 9, 16]
- filter(): Filters the input list 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]
- sorted(): Sorts the input list based on a key.
points = [(1, 2), (3, 1), (5, 7), (2, 3)]
sorted_points = sorted(points, key=lambda point: point[1])
print(sorted_points) # Output: [(3, 1), (1, 2), (2, 3), (5, 7)]
Practice Questions
Now, let’s practice writing some lambda functions. Try to solve these problems on your own!
- Create a lambda function that subtracts two numbers.
- Create a lambda function that checks if a number is positive.
- Use
map()
with a lambda function to double all the numbers in a list. - Use
filter()
with a lambda function to keep only the strings with more than 3 characters.
Solutions
Here are the solutions to the practice questions:
- Subtracting Two Numbers
subtract = lambda x, y: x - y
print(subtract(10, 3)) # Output: 7
- Checking if a Number is Positive
is_positive = lambda x: x > 0
print(is_positive(5)) # Output: True
print(is_positive(-1)) # Output: False
- Doubling All Numbers in a List
numbers = [1, 2, 3, 4]
doubled = list(map(lambda x: x * 2, numbers))
print(doubled) # Output: [2, 4, 6, 8]
- Filtering Strings with More than 3 Characters
words = ["hi", "hello", "hey", "goodbye"]
long_words = list(filter(lambda word: len(word) > 3, words))
print(long_words) # Output: ['hello', 'goodbye']
Lambda functions are like little secret agents in Python. They’re quick, efficient, and perfect for small tasks. Now that you know how to use them, you can add a new tool to your Python toolkit. Keep practicing, and soon you’ll be a lambda function master!