Python 4.4Code walkthrough8 parts

Lambdas and Functions as Values

Pass functions around, the pattern behind `sorted(key=...)` and `apply`.

Loading your progress…Next: 5.1 Modules and Packages

In this lesson8 parts

  1. 01Sorting the menu by price
  2. 02A function is an object
  3. 03Passing a function as an argument
  4. 04lambda
  5. 05Sorting counts, and ties
  6. 06map and filter
  7. 07Where they mislead
  8. 08Recap, your challenge, and the module

Key terms

The words this lesson introduces, each in one line. The module’s glossary collects them all.

higher-order function
A function that takes a function as an argument, or returns one: sorted with key=, map, filter.
iterator
An object that gives its values one at a time, when asked for the next one. map and filter return iterators, which are used up after one pass.
key function
A function passed as key=, whose result is what sorted, min and max compare.
lambda
A function written as one expression, without a name: lambda price: round(price * 1.08, 2).
stable sort
A sort that keeps items with equal keys in their original order. sorted and list.sort are stable.

Quiz 5 questions

Your first pick on each question is the one that counts, and a right one earns a coin. Getting one wrong here is how the lesson sticks.

Practice

Problems to solve in your own notebook. Each states the problem, not the steps: working out the steps is the exercise. Level A applies the lesson, B combines it with earlier ones, C stretches it.

The self-checking notebook for this lesson is Notebook 2 · Recursion and Functions as Values.

Common mistakes

What you will see when it goes wrong, why it happens, and the fix.

Where it’s used

Where this lesson’s ideas turn up in real work.