Python Lambda Functions

Lambda functions (also called anonymous functions) are tiny functions that have no name and only have one expression as their body.

In Python they are defined using the lambda keyword:
lambda <arguments> : <expression>
The body mu…


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

Lambda functions (also called anonymous functions) are tiny functions that have no name and only have one expression as their body.

In Python they are defined using the lambda keyword:

lambda <arguments> : <expression>

The body must be a single expression. Expression, not a statement.

This difference is important. An expression returns a value, a statement does not.

The simplest example of a lambda function is a function that doubles that value of a number:

lambda num : num * 2

Lambda functions can accept more arguments:

lambda a, b : a * b

Lambda functions cannot be invoked directly, but you can assign them to variables:

multiply = lambda a, b : a * b

print(multiply(2, 2)) # 4

The utility of lambda functions comes when combined with other Python functionality, for example in combination with map(), filter() and reduce().


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-01-07T05:00:00+00:00) Python Lambda Functions. Retrieved from https://www.scien.cx/2021/01/07/python-lambda-functions/

MLA
" » Python Lambda Functions." flaviocopes.com | Sciencx - Thursday January 7, 2021, https://www.scien.cx/2021/01/07/python-lambda-functions/
HARVARD
flaviocopes.com | Sciencx Thursday January 7, 2021 » Python Lambda Functions., viewed ,<https://www.scien.cx/2021/01/07/python-lambda-functions/>
VANCOUVER
flaviocopes.com | Sciencx - » Python Lambda Functions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/01/07/python-lambda-functions/
CHICAGO
" » Python Lambda Functions." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/01/07/python-lambda-functions/
IEEE
" » Python Lambda Functions." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/01/07/python-lambda-functions/. [Accessed: ]
rf:citation
» Python Lambda Functions | flaviocopes.com | Sciencx | https://www.scien.cx/2021/01/07/python-lambda-functions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.