Python: How to use the Lambda Function

Python: How to use the Lambda Function

Overview

This article will explain what a Lambda function is in Python, the syntax for it and how to use it.

What is the Lambda Function

The Lambda function is an expression that can be customized to take the keyword “lambda”, variables and a statement to use those variables to perform a calculation. Other variables can also be assigned to use a lambda function.

Syntax of a Lambda Function

Below shows the syntax of a lambda function and examples how to use it:

lambda name_of_variable1, name_of_variable2...... : mathematical statement

The keyword “lambda” is needed, then all the names of the variables after. Then the colin or “:“ is needed and then a statement to perform an operation with those variables.

Lambda Example

add_two_variables = lambda var1, var2: var1 + var2

print(add_two_variables(3, 5))
# Final Result: 8