1. Lambda functions
You've written your very own Python functions using the def keyword, function headers, docstrings and function bodies.
2. Lambda functions
There's a quicker way to write functions on the fly and these are called lambda functions because you use the keyword lambda. Here we re-write our function raise_to_power as a lambda function. To do so, after the keyword lambda, we specify the names of the arguments; then we use a colon followed by the expression that specifies what we wish the function to return.
Lambda functions allow you to write functions in a quick and potentially dirty way so I wouldn't advise you to use them all the time but there are situations when they can come in very handy.
3. Anonymous functions
For example, check out the map function, which takes two arguments, a function and a sequence such as a list and applies the function over all elements of the sequence. We can pass lambda functions to map without even naming them and in this case we refer to them as anonymous functions.
In this example, we use map on a lambda function that squares all elements of a list and we'll store the result in square_all. Printing square_all reveals that it is actually a map object so to see what it contains we use the function list to turn it into a list and print the results to the shell. As expected, it's a list containing the squares of the elements in the original list!
4. Let's practice!
In the following interactive exercises, you'll become a boss at writing lambda functions and see a number of other cool uses for them. Get hacking!