Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Lambda vs Regular Functions
๐ Scenario: Imagine you are organizing a small party and want to quickly calculate the cost of snacks based on quantity. You will use Python functions to do this calculation in two ways: with a regular function and with a lambda function.
๐ฏ Goal: Build two functions that calculate the total cost of snacks by multiplying the quantity by the price per snack. One function will be a regular function, and the other will be a lambda function. Then, compare their outputs.
๐ What You'll Learn
Create a regular function called calculate_cost that takes quantity and price_per_snack as parameters and returns the total cost.
Create a lambda function called calculate_cost_lambda that does the same calculation.
Use both functions to calculate the cost for 5 snacks priced at 2 each.
Print the results from both functions.
๐ก Why This Matters
๐ Real World
Functions help automate repetitive calculations like pricing items in a store or calculating bills quickly.
๐ผ Career
Understanding both regular and lambda functions is important for writing clean, efficient code in many programming jobs.
Progress0 / 4 steps
1
Create a regular function to calculate cost
Write a regular function called calculate_cost that takes two parameters: quantity and price_per_snack. It should return the product of these two parameters.
Python
Hint
Use the def keyword to create a function. Multiply quantity by price_per_snack and return the result.
2
Create a lambda function to calculate cost
Create a lambda function called calculate_cost_lambda that takes quantity and price_per_snack as inputs and returns their product.
Python
Hint
Assign a lambda function to calculate_cost_lambda. The lambda should take two parameters and return their multiplication.
3
Calculate cost using both functions
Use the regular function calculate_cost and the lambda function calculate_cost_lambda to calculate the cost for 5 snacks priced at 2 each. Store the results in variables cost_regular and cost_lambda respectively.
Python
Hint
Call both functions with arguments 5 and 2, and save their results in cost_regular and cost_lambda.
4
Print the results
Print the values of cost_regular and cost_lambda on separate lines.
Python
Hint
Use two print statements to show the values of cost_regular and cost_lambda.
Practice
(1/5)
1. Which of the following best describes a lambda function in Python?
easy
A. A function defined using def that can have multiple statements.
B. A short, anonymous function defined with the lambda keyword.
C. A function that must always return None.
D. A function that can only be used inside classes.
Solution
Step 1: Understand lambda function definition
A lambda function is defined using the lambda keyword and is anonymous (no name).
Step 2: Compare with regular functions
Regular functions use def and can have multiple statements, unlike lambdas which are single expressions.
Final Answer:
A short, anonymous function defined with the lambda keyword. -> Option B
Quick Check:
Lambda = short anonymous function [OK]
Hint: Lambdas are short and nameless functions [OK]
Common Mistakes:
Thinking lambdas can have multiple statements
Confusing lambda with regular def functions
Believing lambdas must return None
2. Which of the following is the correct syntax for a lambda function that adds 5 to its input?
easy
A. def add_five(x): return x + 5
B. lambda x x + 5
C. lambda x: x + 5
D. lambda (x): return x + 5
Solution
Step 1: Recall lambda syntax
A lambda function is written as lambda parameters: expression without def or return.
Step 2: Check each option
lambda x: x + 5 matches the correct syntax: lambda x: x + 5. Others have syntax errors or use def.
Final Answer:
lambda x: x + 5 -> Option C
Quick Check:
Lambda syntax = lambda params: expression [OK]
Hint: Lambda uses colon, no def or return [OK]
Common Mistakes:
Including 'def' or 'return' in lambda
Missing colon after parameters
Using parentheses incorrectly
3. What is the output of the following code?
add = lambda x, y: x + y
def add_func(x, y):
return x + y
print(add(3, 4))
print(add_func(3, 4))
medium
A. 7\n7
B. 34\n34
C. TypeError\nTypeError
D. None\nNone
Solution
Step 1: Understand lambda and regular function behavior
Both add (lambda) and add_func (regular) add two numbers and return the sum.
Step 2: Evaluate the print statements
Calling add(3, 4) and add_func(3, 4) both return 7, so output is two lines with 7.
Final Answer:
7
7 -> Option A
Quick Check:
Both add functions return sum = 7 [OK]
Hint: Both lambda and def return same result if logic matches [OK]
Common Mistakes:
Thinking lambda returns string concatenation
Confusing output with error messages
Assuming lambda can't return values
4. Identify the error in this code snippet:
multiply = lambda x, y:
x * y
print(multiply(2, 3))
medium
A. No error; output is 6
B. SyntaxError due to missing colon after lambda parameters
C. TypeError because lambda cannot take two arguments
D. IndentationError because lambda body is on new line
Solution
Step 1: Check lambda syntax rules
Lambda functions must have their expression on the same line as the lambda keyword and parameters.
Step 2: Analyze the code structure
The code places the expression x * y on the next line, causing an IndentationError.
Final Answer:
IndentationError because lambda body is on new line -> Option D
Quick Check:
Lambda body must be on same line [OK]
Hint: Lambda body must be on same line as parameters [OK]
Common Mistakes:
Placing lambda body on next line
Adding colon after lambda parameters
Thinking lambda can't take multiple arguments
5. You want to sort a list of tuples by the second item using a function. Which is the best way to write the key function?
hard
A. list.sort(key=lambda t: t[1])
B. def get_second(t): return t[1]
list.sort(key=get_second(t))
C. list.sort(key=lambda t t[1])
D. list.sort(key=get_second())
Solution
Step 1: Understand sorting with key functions
The key parameter expects a function that takes one argument and returns the value to sort by.
Step 2: Evaluate options for correctness and efficiency
def get_second(t): return t[1]
list.sort(key=get_second(t)) defines a regular function but incorrectly calls get_second(t) (causing NameError: 't' not defined). list.sort(key=lambda t: t[1]) uses a lambda inline, which is concise and common. list.sort(key=lambda t t[1]) has syntax error (missing colon). list.sort(key=get_second()) calls get_second() instead of passing the function.
Step 3: Choose best practice
Using a lambda inline is best for simple, one-time use functions.