Lesson 1 of 20 · about 14 min
Functions are values
In Python a function is a value, just like a number. You can store it in a variable, put it in a list, and pass it to another function. Functions that take functions are called higher-order functions.
🧭 What you need first: Boss level: build a text adventure
Look at three tiny programs:
- add up 1 + 2 + 3 + … + 10
- add up
- add up
They’re almost identical. The only thing that changes is what you do to each number before adding it. You could copy-paste the loop three times… or you could write one “add up something” function and hand it the “something” as an input.
That’s the big idea of this lesson: a function is a value, so you can pass it around like a number. A recipe that says “cook the vegetables using your chosen method” works whether you hand it “steam,” “roast” or “fry.”
Another way: a power drill
A drill is one tool, but you snap in different bits: a screwdriver bit, a hole-cutting bit, a sanding pad. The drill (higher-order function) does the spinning; the bit (the function you pass in) decides what the spinning does.
Another way: a mail-merge
“For every customer, send them THIS letter.” The “for every customer” part stays the same; the letter is swapped in. The letter is the function being passed.
Pass a function in
Step through. Notice the f variable inside summation refers to a whole function, and f(k) calls whichever one was passed in.
Watch the code run, one step at a time
- 1def identity(x):
- 2 return x
- 3
- 4def cube(x):
- 5 return x * x * x
- 6
- 7def summation(n, f):
- 8 total = 0
- 9 for k in range(1, n + 1):
- 10 total += f(k)
- 11 return total
- 12
- 13print(summation(3, identity))
- 14print(summation(3, cube))
➜ yellow = the line about to run · ✓ = the line that just ran
The real words
- A first-class function can be used anywhere a value can: assigned to a name, stored in a list or dictionary, passed as an argument, returned from another function.
square(no parentheses) refers to the function object itself.square(3)calls it and gives back the result. Forgetting the brackets is a common bug:print(square)shows<function square …>.- A higher-order function takes a function as an argument or returns one. It expresses a general pattern; the function you pass in fills in the details.
- Python already has higher-order functions built in:
sorted(words, key=len)sorts by the result of callinglenon each word.max(people, key=get_age)finds the oldest person.map(f, items)andfilter(f, items)apply a function across a sequence (lesson 8).
- This is the start of abstraction over computation: not just naming values (variables) or steps (functions), but naming patterns of computation.
Write a higher-order function
Write apply_twice(f, x) that returns f(f(x)). Then write count_if(items, test) that returns how many items make test(item) return True.
Sort by anything
Using sorted(..., key=some_function), set by_length to the words sorted from shortest to longest, and by_last_letter to the words sorted by their LAST letter.
What's the difference between square and square(4)?
Got it?
Finish the lesson to color it in on your map and collect its concept cards.
Did this lesson click?