1A = {1, 2, 3, 4}2def f(x): return x % 33f(4)4{f(x) for x in A}5d = {x: f(x) for x in A}x % 3
Input cells (A) at the back, output cells (B) at the frontArrows on the table: one ordered pair (input, output) eachRule f: the machine works out x mod 3Pair cells and Python card: the same function as data and as codeOne function, f(x) = x mod 3, from A = {1, 2, 3, 4} to B = {0, 1, 2}. Each input has exactly one arrow out; the arrows are the ordered pairs (1, 1), (2, 2), (3, 0), (4, 1). Tokens hop from an input into the rule and out to its output. Rings mark the outputs that are hit (the range), and a dashed red arrow shows the extra pair (1, 2) that would stop it being a function. A function that is both one-to-one and onto is a bijection. Only a bijection has an inverse function that undoes it. Example: g(x) = x + 1 from {1, 2, 3} to {2, 3, 4}, whose inverse subtracts 1.

A rule for inputs

  1. A function needs two sets. A = {1, 2, 3, 4} holds the inputs; it is called the domain. B = {0, 1, 2} holds the allowed outputs; it is the codomain. We write f: A → B, read 'f maps A to B'.

Relations and functions

Onto and one-to-one

In code

Try it in the Python prompt

;; step 1/9 · Inputs and outputs
>>> A = {1, 2, 3, 4}
>>> B = {0, 1, 2}
>>> len(A), len(B)
(4, 3)
>>> 3 in A, 3 in B
(True, False)

f: A → B is read 'f from A to B'. If f(x) = y, then y is the image of x, and x is a preimage of y. The range is the set of all images. Python's % gives a result with the sign of the divisor, so -1 % 3 is 2, which matches maths mod. In C and Java, -1 % 3 is -1.