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: expressionHere'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: 8In 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
EasyCreate 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: expressionExample:
add = lambda x, y: x + y
result = add(5, 3)
print(result)
# Output: 8Lambda 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter