Lambda & Applied Problems
In this lesson, we explore Lambda functions — small, anonymous functions used for short-term tasks. We also look at how to apply these and other tools to solve real-world problems in various subject areas.
Learning Objectives
- 11.3.2.1 Write code in a programming language using lambda functions
- 11.4.3.2 Solve applied problems of various subject areas
Conceptual Anchor
The Disposable Camera Analogy
A normal function (def) is a professional camera: robust, reusable, and named. A
lambda function is like a disposable camera: quick, used once for a specific
snapshot (calculation), and then discarded. You don't name a disposable camera; you just use it.
Rules & Theory
Lambda Functions (11.3.2.1)
# Syntax: lambda arguments: expression
# Normal function
def square(x):
return x * x
# Equivalent Lambda function
square_lambda = lambda x: x * x
print(square(5)) # 25
print(square_lambda(5)) # 25
# Lambda with multiple arguments
add = lambda a, b: a + b
print(add(3, 4)) # 7When to use Lambda?
Lambdas are best used as arguments to higher-order functions like map(),
filter(), and sorted(). They are rarely assigned to variables like
above.
Applied Contexts (11.4.3.2)
Solving applied problems means taking a real-world scenario (Physics, Math, Economics) and translating it into code.
# Sorting complex data (Applied usage)
students = [
{"name": "Ali", "score": 85},
{"name": "Dana", "score": 92},
{"name": "Marat", "score": 78}
]
# Sort by score using lambda
students.sort(key=lambda s: s["score"], reverse=True)
print(students)
# Output: [{'name': 'Dana', 'score': 92}, {'name': 'Ali', 'score': 85}, ...]Worked Examples
1 Physics: Kinetic Energy
Problem: Calculate Kinetic Energy ($KE = \frac{1}{2}mv^2$) for a list of objects.
# List of (mass, velocity) tuples
objects = [(10, 5), (50, 2), (5, 10)]
# Use map() with lambda to calculate KE for each
kinetic_energies = list(map(lambda obj: 0.5 * obj[0] * obj[1]**2, objects))
print(kinetic_energies)
# [125.0, 100.0, 250.0]2 Economics: Currency Conversion
Problem: Convert a list of prices in USD to KZT (Exchange rate: 450).
prices_usd = [10, 25, 5, 100]
rate = 450
# Using lambda for conversion
prices_kzt = list(map(lambda p: p * rate, prices_usd))
print(prices_kzt)
# [4500, 11250, 2250, 45000]3 Data Analysis: Filtering Outliers
Problem: Remove sensor readings that are below 0 or above 100.
readings = [15, -2, 45, 102, 88, -5, 120, 33]
# Using filter() with lambda
valid_readings = list(filter(lambda x: 0 <= x <= 100, readings))
print(valid_readings)
# [15, 45, 88, 33]Pitfalls & Common Errors
Lambda Complexity
Lambda functions are restricted to a single expression. You cannot write multi-line logic (if/else blocks, loops) inside a lambda. If you need complex logic, define a normal function.
Graded Tasks
What keyword creates an anonymous function? How many expressions can a lambda have?
Rewrite this function as a lambda: def double(x): return x * 2.
Use `filter()` and a lambda to extract all words starting with "A" from a list of names.
Physics Problem: Given a list of distances (m) and times (s), calculate the speed for each pair using `map()` and lambda.
Create a program that manages a shopping cart (list of dictionaries). Use lambda functions to sort the cart by price and filter out items cheaper than 1000 tenge.