Unit 11.4A · Term 4

Lambda & Higher-Order Functions

In this lesson, we explore Lambda functions — lightweight, anonymous functions used inline for short-term operations, paired with map(), filter(), and sorted(key=...) to solve real-world problems elegantly.

Learning Objectives

  • 11.3.1.1 Write code in a programming language using functions
  • 11.3.3.4 Solve applied problems of various subject areas

Lesson Presentation

11.4A-lesson-22-lambda-functions.pdf · Slides for classroom use

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)) # 7

When 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.

Pro-Tips for Exams

Exam Checklist

  • Custom sorting dictionaries by value: sorted(d.items(), key=lambda item: item[1]).
  • Sorting objects or strings ignoring case: sorted(words, key=lambda s: s.lower()).
  • Remember: in Python 3, map() and filter() return iterator objects. Wrap them in list() to display or index their values.

Graded Tasks

Remember

What keyword creates an anonymous function? How many expressions can a lambda have?

Understand

Rewrite this function as a lambda: def double(x): return x * 2.

Apply

Use `filter()` and a lambda to extract all words starting with "A" from a list of names.

Apply

Physics Problem: Given a list of distances (m) and times (s), calculate the speed for each pair using `map()` and lambda.

Create

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.

Self-Check Quiz

1. What is the output of (lambda x, y: x + y)(3, 4)?
Click to reveal answer
2. Does a lambda function use the return keyword?
Click to reveal answer
3. What does list(filter(lambda x: x % 2 == 0, [1, 2, 3, 4])) produce?
Click to reveal answer