You can create small anonymous functions in Python using the lambda keyword.
By anonymous I mean it doesn't need to have a name, normal functions start with the def keyword and require a name.A lambda expression is not a statement, it returns a value.
Lets say you want a function that returns the sum of its two arguments
Imagine you have a simple function like this:
def sum(a,b): return a+b
That is quite a lot to write.
Instead you can create a lambda expression like the example below.
A lambda function that adds two variables as an argument and prints the result:
lambda a, b: a+b
You can then use it like this:
>>> f = lambda a,b : a+b >>> f(2,3) 5 >>> f(10,15) 25 >>>
You can create all kinds of lambda expressions, this is a lot shorter to write than creating functions for tiny operations.
# A lambda function that adds 10 to the argument x = lambda a : a + 10 print(x(8)) # A lambda function that multiplies two variables x = lambda a, b : a * b print(x(8, 9)) # A lambda function that sums its arguments x = lambda a, b, c : a + b + c print(x(1, 2, 3))