# A lambda function is a small anonymous function which
# can take any number of arguments, but can only have only one expression.
# Define a lambda function that takes in two numbers and returns their sum
sum = lambda x, y: x + y
# Test the function
print(sum(3,4)) # 7
# Using lambda function with filter
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers) # [2, 4, 6, 8, 10]
# Using lambda function with map
squared_numbers = list(map(lambda x: x**2, even_numbers))
print(squared_numbers) # [4, 16, 36, 64, 100]
No comments:
Post a Comment