Menu
Coddy logo textTech

Lambda Functions Part 1

Part of the Logic & Flow section of Coddy's Python journey — lesson 57 of 78.

A lambda function is a small, anonymous function defined using the lambda keyword. Lambda functions can take any number of arguments but can only have one expression. They are useful for creating simple, one-line functions without the need for a full function definition using the def keyword.

The syntax of a lambda function is:

lambda arguments: expression

Here's a breakdown of the syntax:

  • lambda: The keyword that indicates the start of a lambda function definition.
  • arguments: A comma-separated list of arguments, similar to the parameters in a regular function definition.
  • expression: A single expression that is evaluated and returned as the result of the lambda function.

Here's an example of a lambda function that adds two numbers:

add = lambda x, y: x + y
result = add(5, 3)
print(result)
# Output: 8

In this example, the lambda function takes two arguments, x and y, and returns their sum. The lambda function is assigned to the variable add, which can then be called like a regular function.

Lambda functions are often used in situations where a short, throwaway function is needed, such as with higher-order functions like map, filter, and reduce.

challenge icon

Challenge

Easy

Create a lambda function named multiply that takes three arguments, x, y, and z, and returns their product. After defining the lambda function, call it with the values 2, 3, and 4, and print the result.

Cheat sheet

A lambda function is a small, anonymous function defined using the lambda keyword. Lambda functions can take any number of arguments but can only have one expression.

Syntax:

lambda arguments: expression

Example:

add = lambda x, y: x + y
result = add(5, 3)
print(result)
# Output: 8

Lambda functions are often used with higher-order functions like map, filter, and reduce.

Try it yourself

# Create a lambda function that multiplies three numbers
multiply = 

# Call the lambda function with the values 2, 3, and 4
result = 

# Print the result
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow