You can write your very own Python functions using the def keyword, function headers, docstrings, and function bodies. However, there's a quicker way to write functions on the fly, and these are called lambda functions because you use the keyword lambda.
Some function definitions are simple enough that they can be converted to a lambda function. By doing this, you write fewer lines of code, which is pretty awesome and will come in handy, especially when you're writing and maintaining big programs.
lambda Function
Here we rewrite our function raise_to_power as a lambda function. 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.
raise_to_power =lambda x, y: x ** y
raise_to_power(2,3)
As mentioned, the lambda functions allow you to write functions in a quick and dirty way, so we wouldn't advise you to use them all the time, but there are situations when they come in handy, like the example below.
map() and lambda Function
The map function takes two arguments, a function and a sequence such as a list and applies the function over all the elements of the sequence. We can pass lambda function to the map without even naming them, and in this case, we refer to them as anonymous functions.
In this example, we use map() on the lambda function, which squares all elements of the list, and we store the result in square_all.
nums =[48,6,9,21,1]
square_all =map(lambda num: num **2, nums)print(square_all)
The below function echo_word takes 2 parameters: a string value, word1, and an integer value echo. It returns a string that is a concatenation of echo copies of word1.
You will convert the above simple function into a lambda function.
defecho_word(word1, echo):"""Concatenate echo copies of word1."""
words = word1 * echo
return words
Learn the basics of AWS Lambda, how to set up your first function, and how to integrate it with core AWS services for application serverless deployment.
Discover how to write reusable and efficient Python functions. Master parameters, return statements, and advanced topics like lambda functions. Organize your code better with main() and other best practices.
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
You can write your very own Python functions using the def keyword, function headers, docstrings, and function bodies. However, there's a quicker way to write functions on the fly, and these are called
lambdafunctions because you use the keywordlambda.Some function definitions are simple enough that they can be converted to a lambda function. By doing this, you write fewer lines of code, which is pretty awesome and will come in handy, especially when you're writing and maintaining big programs.
lambdaFunctionHere we rewrite our function
raise_to_poweras a lambda function. After the keywordlambda, 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.As mentioned, the
lambdafunctions allow you to write functions in a quick and dirty way, so we wouldn't advise you to use them all the time, but there are situations when they come in handy, like the example below.map()andlambdaFunctionThe
mapfunction takes two arguments, a function and a sequence such as a list and applies the function over all the elements of the sequence. We can passlambdafunction to themapwithout even naming them, and in this case, we refer to them as anonymous functions.In this example, we use
map()on thelambdafunction, which squares all elements of the list, and we store the result insquare_all.Printing
square_allreveals that its amapobject, so to see what it returns, we use list to turn it into a list and print the result.Interactive Example of Writing a
lambdaFunctionThe below function
echo_wordtakes 2 parameters: a string value,word1, and an integer valueecho. It returns a string that is a concatenation ofechocopies ofword1.You will convert the above simple function into a lambda function.
In the following example, you will:
echo_wordusing the variablesword1andecho. Replicate what the original function definition forecho_word()does above.echo_word()with the string argument'hey'and the value5, in that order. Assign the call toresult. Finally, print theresultvariable.When we run the above code, it produces the following result:
Try it for yourself.
To learn more about Lambda functions, please see this video from our course Python Data Science Toolbox (Part 1).
This content is taken from DataCamp’s Python Data Science Toolbox (Part 1) course by Hugo Bowne-Anderson.
Check out DataCamp's Python Functions Tutorial.