1math.perm(5, 3)2math.factorial(3)3math.comb(5, 3)42 ** 55math.comb(5, 2)1st2nd3rd0101A01B01C01DE0
Item lamps: a lit lamp is an item that is chosenSlot cells: 1st, 2nd, 3rd place of an ordered pickCalculator: the running countPython card: the math functions that count for youFive items, A to E, are five lamps; a lit lamp is a chosen item. Three slot cells hold an ordered pick, and the calculator keeps the count. Items hop into the slots: 5, then 4, then 3 choices, so 5 × 4 × 3 = 60 ordered picks. The three slots shuffle through 3! = 6 orders, so 60 ÷ 6 = 10 teams, C(5, 3). The lamps then step through all ten teams, flip to show C(5, 3) = C(5, 2), and split by whether E is in (6 + 4 = 10). If items may repeat, like the digits of a PIN, every slot keeps all its choices: a 4-digit PIN has 10 × 10 × 10 × 10 = 10,000 possibilities.

Ordered picks

  1. Counting means finding how many ways something can happen, without listing every way. Our example: five items, A to E. We pick three of them to form a team. How many different teams are there?

Combinations

Patterns

In code

Try it in the Python prompt

;; step 1/10 · Five items, pick three
>>> items = "ABCDE"
>>> len(items)
5

The product rule needs the same number of choices at each stage whatever came before (4 remain after the first pick, whichever item it was). The sum rule needs cases that cannot both happen, so no outcome is counted twice. 0! is defined to be 1. Then the formula also works at the edges: C(n, 0) = C(n, n) = 1, because there is exactly one way to choose nothing, and one way to choose everything.