Explore Lambda Functions
00:11 This means that you can get several input arguments, but you cannot have multiple variables to be returned directly from the lambda function. It’s used when you need a quick function for a short period of time and don’t want to define a separate function for it.
Lambda functions are defined using the
lambda keyword and are commonly used as another function’s argument. You can see an example here. It’s a lambda function that doubles a number,
x * 2. So you start with the
lambda keyword, then specify your input, which is
Then you put a colon and type what you want to do with your input. In this case, it’s
x * 2, which means you’re doubling your input. In the next lessons, you’ll learn all about functions like
reduce() that often take lambda functions as their argument.
Let’s do a quick example. Let’s take two numbers and add them together. Before writing up the lambda function, let’s create a regular function that does this.
def add_numbers() taking
num2 as input … and
return. We want to add them together, so
num1 + num2.
You could also directly plug in
3. Let’s do that now.
result = … So you’re using the same lambda function here as before, but just you’re putting it into a parentheses.
(lambda num1, num2: num1 + num2).
To summarize what you just learned, lambda functions are nameless functions that you use when you need a function for a short period of time. They use the
lambda keyword and are often used as other functions’ arguments, like
map(), which you’ll learn about in the next lessons.
Become a Member to join the conversation.