Lesson 2 of 20 · about 14 min
Functions
In math, a function is a rule that gives every input exactly one output. You'll draw functions as arrows, learn the three kinds that matter most (one-to-one, onto, and bijections), and see why bijections are how mathematicians count and compare sizes.
🧭 What you need first: Sets
A coat check gives every coat a ticket number. That’s a function: each coat gets exactly one number. A good coat check also never gives two coats the same number (that would end badly at closing time). In math words, its function is one-to-one.
Now picture a theater where every seat must be filled for a show to start. Assigning people to seats so that no seat is empty is an onto function. Do both at once (everyone gets their own seat, and no seat is empty) and you have a bijection: a perfect pairing.
These ideas sound abstract, but you use them all the time. A password hash should be “almost one-to-one” (F04 lesson 15). A database key must be one-to-one, or two people get each other’s records. And bijections are how we know two sets have the same size without counting them, which leads to the surprising lesson about infinity coming up next.
Another way: a vending machine
Press a button, get exactly one snack. That’s a function from buttons to snacks. If two buttons give the same snack, it isn’t one-to-one. If some snack has no button, it isn’t onto.
Draw the arrows
Each challenge asks for a different kind of function. Draw the arrows and watch the three badges.
🎯 1/3: Give every coat its own ticket: one-to-one, but not onto.
Tap an input on the left, then an output on the right, to draw its arrow. Every input needs exactly one arrow.
- … One-to-one (injective)no two inputs share an output
- … Onto (surjective)every output gets at least one arrow
- … Bijectionboth: a perfect pairing
Did you notice? With 3 inputs and 4 outputs you can’t be onto, and with 4 inputs and 3 outputs you can’t be one-to-one. Sizes decide what’s possible.
The real words
- A function assigns to every exactly one .
- is the domain, the codomain. The range (or image) is the set of outputs actually used.
- Every input needs an arrow, and no input may have two. A Python dictionary is a function from its keys to its values.
- One-to-one and onto:
- Injective (one-to-one): only when . No output is shared. Then .
- Surjective (onto): every equals for some . The range is all of . Then .
- A bijection is both. Then , and it can be reversed: the inverse sends each output back to its input.
- Composition: , “do , then ”. Exactly like calling one Python function on the result of another.
Check a function's kind
A function is given as a dictionary from inputs to outputs, plus its codomain (a set). Write is_injective(f) and is_surjective(f, codomain).
Undo a bijection
A simple secret code shifts each letter 3 places (a → d, …, x → a). Write make_shift(k) that returns the code as a dict from letters to letters, inverse(f) that reverses any bijection, and decode(message, code) using the inverse.
Swapping the arrows only gives a function because the shift code is a bijection. If two letters encoded to the same letter, the “inverse” wouldn’t know which one to send back.
A function goes from a set of 5 students to a set of 3 project groups. What must be true?
Got it?
Finish the lesson to color it in on your map and collect its concept cards.
Did this lesson click?