Day 07 - Lambdas¶
Lambda functions are also known as anonymous functions, they can, as a normal function, take different arguments and return a single value, but lambdas are one-line functions.
The syntax is as follows:
lambda arguments: expresion
def normal_multiply(num_a, num_b): return num_a * num_b lambda_multiply = lambda num_a, num_b: num_a * num_b print(normal_multiply(5, 2)) print(lambda_multiply(5, 2)) # Output: # 10 # 10
As you can see both
lambda_multiply functions, another difference you can notice is that lambda functions return a value without explicitly using the
Why use Lambda functions?¶
We can create functions that perform a simple actions or operations using lambda functions, so that we can make our code more compact and simple.
Let’s try using the lambda functions with one of the previous examples.
def multiple(number): return number % 2 == 0 numbers = range(1, 101) filter_numbers = filter(multiple, numbers) print(list(filter_numbers))