One advanced Python concept is lambda functions, which are anonymous functions that are defined using the
lambda keyword. Lambda functions have the following basic form:
lambda params: expression
- The keyword
lambdasignifies that you are defining a lambda function.
paramsrefers to the parameters that the lambda function will use. The number of the parameters can be zero to multiple.
expressionrefers to the expression that the lambda function will operate.
At their core, lambda functions are just like other Python functions, which can be invoked using the call operator (the parentheses). The following code shows you related facts.
>>> multiply = lambda x, y: x*y
>>> multiply(5, 3)
>>> multiply(8, 5)
Although we assigned the lambda function to the variable
multiply, it’s just for showing you the fact that lambdas are functions. In real-life projects, this practice is strongly discouraged, because as known by its alternative name, lambdas are anonymous functions, which should be used in a place where the explicit definition of a function is not needed.
After knowing how to define a lambda function, let’s explore its actual use cases to learn this technique more thoroughly.
Continue reading: https://towardsdatascience.com/python-lambda-functions-three-practical-examples-sort-map-and-apply-286593792cb4?source=rss—-7f60cf5620c9—4