Skip to main content

Command Palette

Search for a command to run...

Python: How to use the Lambda Function

Published
1 min read
Python: How to use the Lambda Function
A

On my free time, I like to learn more about hardware, software and different technologies. I publish articles about my recent learnings or the projects I have done.

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