Ordered picks
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?
Fill three slots in turn. The first has 5 choices. A is now used, so the second has 4, then the third has 3. The product rule: when a choice is made in stages, multiply. 5 × 4 × 3 = 60 ordered picks.
Order counts in those 60 picks. The same three items stand in 3 × 2 × 1 = 6 orders: ABC, ACB, BAC, BCA, CAB, CBA. We write 3!, said 'three factorial'. In general n! = n × (n − 1) × … × 1.
Combinations
A team ignores order: ABC and CBA are the same team. So each team was counted 3! = 6 times among the 60 picks. Divide: 60 ÷ 6 = 10 teams. This is a combination, written C(5, 3) and read '5 choose 3'.
Choosing k items from n gives C(n, k) = n! / (k! (n − k)!). The n! / (n − k)! part counts ordered picks; dividing by k! forgets the order. Here 5! = 120, and 120 / (3! × 2!) = 10.
Check by listing. A team is a pattern of five lamps with exactly three lit. Python's itertools.combinations lists them in order: ABC, ABD, ABE, and so on to CDE. There are ten, just as the formula said.
Patterns
Drop the size limit. Each item is in or out: two choices, five times, so 2⁵ = 32 subsets. Read the lamps as bits: only 10 of the 32 patterns have exactly three lit.
Picking the 3 who play is the same as picking the 2 who sit out. Flip every lamp and each team of three becomes a pair of two. So C(5, 3) = C(5, 2) = 10. In general, C(n, k) = C(n, n − k).
Split by cases. Is E in? Yes: pick 2 more from A to D, C(4, 2) = 6. No: pick 3 from A to D, C(4, 3) = 4. No team is in both, so the sum rule adds: 6 + 4 = 10.
In code
Python's math module counts for you: math.comb(5, 3) is 10 and math.perm(5, 3) is 60. Counting first tells you how big a search is before you run it: choosing 3 from 50 already gives 19,600 teams.
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.