In Python, a lambda
or anonymous function is a small, nameless function that can take any number of arguments, but can only have one expression. The syntax for lambda
function is as follows:
main.py29 chars2 lines
Here's an example to illustrate how lambda
functions can be used:
main.py241 chars11 lines
In the example above, we defined two functions addition
and addition_lambda
that both take two arguments and return the sum of them. We can see that the lambda
function is more concise and easier to define.
gistlibby LogSnag