Lambda Functions
A lambda function, also known as an anonymous function, is a small and concise function that is defined without a name. It is a way to create a function on-the-fly without the need to define a formal function using the def keyword.
lambda arguments: expression
Lambda functions are typically used when you need a simple function for a short period of time, often as an argument to higher-order functions or in cases where a named function would be cumbersome.
addition = lambda x, y: x + y
result = addition(3, 5)
print(result)
In this example, we define a lambda function addition that takes two arguments x and y and returns their sum. We then invoke the lambda function by passing 3 and 5 as arguments, and the result is assigned to the result variable.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
list_of_numbers = list(even_numbers)
print(list_of_numbers)
In this example, we have a list...