Everything else is ready: study the parts, the key terms and the quiz below.
Lambdas and Functions as Values
Pass functions around, the pattern behind `sorted(key=...)` and `apply`.
In this lesson8 parts
- 01Sorting the menu by price
- 02A function is an object
- 03Passing a function as an argument
- 04lambda
- 05Sorting counts, and ties
- 06map and filter
- 07Where they mislead
- 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:
sortedwithkey=,map,filter. - iterator
- An object that gives its values one at a time, when asked for the next one.
mapandfilterreturn iterators, which are used up after one pass. - key function
- A function passed as
key=, whose result is whatsorted,minandmaxcompare. - 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.
sortedandlist.sortare 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.