Lambda functions, also referred to as anonymous functions, are functions which are nameless and have only one expression.

Structure

A lambda function contains the keyword lambda and is structured as follows:

lambda argument(s) : expression

NOTE: The reason why a lambda function contains an expression and not a statement is that an expression will return a value while a statement will not.

Examples

A lambda function that takes a number and triples its value:

lambda number : number * 3

A lambda function that takes two numbers and adds them:

lambda num1, num2 : num1 + num2

While you cannot call a lambda function, you can assign it to a variable:

divide = lambda num1, num2 : num1 / num2

print(divide(4, 2)) # 2.0

Lambda functions can be used with other methods such as reduce(), filter(), and map().