Lesson 4 - Permutations and Combinations

Welcome to Permutations and Combinations

Many probability questions come down to one deceptively simple task: counting. How many ways can a race finish? How many three-person committees can a team form? How many possible lottery tickets are there? Once you can count the outcomes, the probability is usually just a ratio. But counting by hand falls apart fast — the numbers explode — so you need a few precise tools to do it for you.

In this lesson you will meet those tools. You will learn to multiply choices with the counting principle, scale them up with factorials, and then split into the two cases that matter most: when order matters (permutations) and when it does not (combinations). By the end you will compute the exact number of possible lottery tickets and see why winning is so unlikely.

By the end of this lesson, you will be able to:

  • Use the fundamental counting principle to count multi-step outcomes
  • Compute factorials and explain why 0!=1 0! = 1
  • Calculate permutations (ordered arrangements) with math.perm
  • Calculate combinations (unordered selections) with math.comb and turn counts into probabilities

You only need Python’s built-in math module. Let’s begin.


The Fundamental Counting Principle

Start with the rule that powers everything else. The fundamental counting principle says that if one choice can be made in a a ways and a second independent choice in b b ways, then the two choices together can be made in a×b a \times b ways. Extend it to as many stages as you like: multiply the number of options at each stage.

Suppose you are getting dressed and own 3 shirts, 4 pairs of pants, and 2 pairs of shoes. How many different outfits can you assemble?

shirts, pants, shoes = 3, 4, 2
outfits = shirts * pants * shoes
print(outfits)
24

There are 24 outfits, because every shirt pairs with every pair of pants, and every one of those pairings works with either pair of shoes. The principle is just multiplication, but recognizing when to multiply is the real skill — and it is the foundation of the formulas that follow.


Factorials

When you arrange a whole set of distinct items in order, the counting principle produces a special pattern. Imagine lining up 5 different books on a shelf. The first slot can be any of 5 books; once it is filled, the second slot has 4 remaining choices, then 3, then 2, then the last book is forced. Multiply those down: 5×4×3×2×1 5 \times 4 \times 3 \times 2 \times 1 .

That product has a name. The factorial of a non-negative integer n n , written n! n! , is the product of all positive integers up to n n :

n!=n×(n1)×(n2)××2×1 n! = n \times (n-1) \times (n-2) \times \cdots \times 2 \times 1

Python’s math module computes it directly:

import math

print(math.factorial(5))
120

So there are 120 ways to arrange 5 books — and factorials grow ferociously: 10! 10! is already over three million. Factorials count the orderings of a full set, and they are the building block of both permutations and combinations.

Why 0!=1 0! = 1

One value looks strange at first: 0!=1 0! = 1 , not 0.

print(math.factorial(0))
1

There is exactly one way to arrange nothing — the empty arrangement. Defining 0!=1 0! = 1 is not an arbitrary convention; it keeps the permutation and combination formulas below working when you select everything or nothing. Without it, those formulas would divide by zero. Treat 0!=1 0! = 1 as a fact you can rely on.


Permutations: When Order Matters

A permutation is an arrangement of items in which the order matters. “Gold, silver, bronze” is a different outcome from “bronze, silver, gold” even with the same three people, so medal standings are permutations.

The number of ways to arrange r r items chosen from n n distinct items is:

P(n,r)=n!(nr)! P(n,r) = \frac{n!}{(n-r)!}

The logic mirrors the bookshelf: the first position has n n choices, the next n1 n-1 , down through r r positions. Dividing n! n! by (nr)! (n-r)! cancels off the orderings of the items you did not pick.

Suppose 10 runners enter a race and the top three earn gold, silver, and bronze. How many different ways can the medals be awarded? Order matters — who comes first is not the same as who comes third — so this is a permutation:

import math

print(math.perm(10, 3))
720

There are 720 possible medal outcomes. You can see the counting principle inside it: 10×9×8=720 10 \times 9 \times 8 = 720 . The first medal has 10 candidates, the second 9, the third 8. math.perm(n, r) does this for you, and math.perm(n) with one argument returns n! n! .


Combinations: When Order Doesn’t Matter

A combination is a selection of items in which the order does not matter. If you are choosing a committee, the group {Ann, Bilal, Chen} is the same committee no matter what sequence you named them in. Here, order is irrelevant.

Three people A, B, C. On the left, permutations list six distinct ordered arrangements: ABC, ACB, BAC, BCA, CAB, CBA. On the right, those six orderings collapse into a single unordered group, the set {A, B, C}, because order does not matter.
With three people there are six ordered arrangements (permutations), but since a committee does not care about order they all collapse into the single unordered group {A, B, C} — one combination.

The number of ways to choose r r items from n n distinct items is:

C(n,r)=(nr)=n!r!(nr)! C(n,r) = \binom{n}{r} = \frac{n!}{r!\,(n-r)!}

This is the permutation formula with one extra division by r! r! . That r! r! erases the orderings: every group of r r people can be arranged in r! r! sequences, but as a combination they all count once.

Take the same 10 people, but now choose a 3-person committee with no roles or ranking:

import math

print(math.comb(10, 3))
120

There are 120 possible committees. Compare that to the 720 medal outcomes from the same 10 people: the committee count is exactly 720/6=120 720 / 6 = 120 , because 3!=6 3! = 6 orderings of each trio collapse into a single committee.

The key contrast

This factor of r! r! is the whole difference between the two ideas. Permutations always count at least as many outcomes as combinations, and for r2 r \ge 2 they count strictly more:

P(n,r)=C(n,r)×r! P(n,r) = C(n,r) \times r!

The figure below makes the gap visible. Both start equal at r=1 r = 1 (choosing one item is the same whether or not order matters), then permutations shoot upward as r r grows because every selection can be ordered in more and more ways.

Bar chart comparing the number of permutations P(10, r) and combinations C(10, r) for r from 1 to 6, showing permutations growing far faster as r increases.
Choosing r items from 10. Permutations and combinations agree at r = 1, then permutations pull far ahead because each unordered selection can be arranged in r! different orders.

The practical takeaway: always ask “does order matter here?” before you count. Treating a combination as a permutation (or vice versa) is the single most common counting mistake.


From Counts to Probabilities

Counting is not the goal in itself — it is how you compute probabilities for situations with equally likely outcomes. Recall the basic definition of probability for equally likely outcomes:

P(event)=number of favorable outcomestotal number of outcomes P(\text{event}) = \frac{\text{number of favorable outcomes}}{\text{total number of outcomes}}

When outcomes are unordered selections, both the numerator and denominator are combinations. Suppose a box holds 10 light bulbs, 2 of which are defective. You grab 3 bulbs at random. What is the probability that none of them is defective?

The denominator is every way to choose 3 bulbs from 10. The numerator is every way to choose 3 bulbs from the 8 good ones:

import math

total = math.comb(10, 3)
good = math.comb(8, 3)
print(good, "/", total, "=", round(good / total, 4))
56 / 120 = 0.4667

So there is about a 46.7% chance all three bulbs work. Notice you never had to list the outcomes — you just counted them with combinations and divided.

Counting the lottery

Now scale this up to a real lottery. In a classic “6/49” game, you pick 6 distinct numbers from 1 to 49, and order does not matter — your ticket is the set of numbers, however you wrote them. The total number of possible tickets is therefore a combination:

import math

tickets = math.comb(49, 6)
print(f"{tickets:,}")
13,983,816

There are exactly 13,983,816 possible tickets. Since only one of them matches the drawn numbers, your probability of winning the jackpot with a single ticket is:

P(jackpot)=1C(49,6)=113,983,816 P(\text{jackpot}) = \frac{1}{C(49,6)} = \frac{1}{13{,}983{,}816}
print(1 / math.comb(49, 6))
7.151123842018516e-08

That is roughly 1 in 14 million, or about 0.0000072%. You computed it from a single combination — no simulation, no guessing. In the next lesson you will build a full project around exactly this number, exploring lottery odds in depth and putting them in human terms.

Spot the question type

Before reaching for a formula, decide which tool you need. Counting principle: independent stages you multiply together. Permutation (math.perm): you are arranging r r of n n items and order matters. Combination (math.comb): you are selecting r r of n n items and order does not. Most probability questions reduce to one of these three.


Practice Exercises

Exercise 1: Count the passwords

A simple PIN has 4 digits, and each position can be any digit from 0 to 9, with repeats allowed. Use the fundamental counting principle to find how many distinct PINs are possible. Then explain why this is not a permutation of math.perm.

Hint

Each of the 4 positions independently has 10 choices, so multiply 10×10×10×10 10 \times 10 \times 10 \times 10 . Because digits can repeat, the number of choices does not shrink as you go — which is why the plain counting principle applies rather than math.perm, where items are used up.

Exercise 2: Permutation or combination?

A class of 12 students must elect a president, a vice-president, and a treasurer (all distinct roles), and separately must choose a 3-person cleanup crew. Compute both counts. Which one is larger, and why?

Hint

The three officer roles are ordered (president is not the same as treasurer), so use math.perm(12, 3). The cleanup crew has no roles, so use math.comb(12, 3). The officers count should be exactly 3!=6 3! = 6 times larger.

Exercise 3: A probability from combinations

A standard deck has 52 cards, 13 of them hearts. You are dealt 5 cards at random. What is the probability that all 5 are hearts? Express it as favorable combinations over total combinations.

Hint

The total number of 5-card hands is math.comb(52, 5). The favorable hands are 5 hearts chosen from the 13 hearts: math.comb(13, 5). Divide the two and round the result to see how small it is.


Summary

You learned to count outcomes precisely. The fundamental counting principle multiplies the choices at each stage. Factorials count the orderings of a full set, with 0!=1 0! = 1 so the formulas stay consistent. Permutations count ordered arrangements with P(n,r)=n!/(nr)! P(n,r) = n!/(n-r)! , while combinations count unordered selections with C(n,r)=n!/(r!(nr)!) C(n,r) = n!/(r!\,(n-r)!) — differing by exactly a factor of r! r! . Finally, you turned these counts into probabilities as favorable over total outcomes, computing the 13,983,816 possible lottery tickets that set up the next lesson.

Key Concepts

  • Fundamental counting principle — multiply the number of options at each independent stage.
  • Factorialn!=n×(n1)××1 n! = n \times (n-1) \times \cdots \times 1 ; the number of ways to order n n distinct items, with 0!=1 0! = 1 .
  • Permutation — an ordered arrangement; P(n,r)=n!(nr)! P(n,r) = \dfrac{n!}{(n-r)!} , computed with math.perm.
  • Combination — an unordered selection; C(n,r)=n!r!(nr)! C(n,r) = \dfrac{n!}{r!\,(n-r)!} , computed with math.comb.
  • Probability from counts — for equally likely outcomes, divide favorable outcomes by total outcomes, often both combinations.

Why This Matters

Counting is the quiet engine behind a huge range of probability and statistics. The same combinations that count lottery tickets also underpin the binomial distribution, A/B test calculations, and the feature subsets a random forest samples. Once you can decide whether order matters and reach for the right tool, problems that look impossible to enumerate become a single line of code.


Next Steps

Continue to Lesson 5 - Guided Project: Lottery Odds

Put permutations and combinations to work in a full project computing real lottery probabilities.

Back to Module Overview

Return to the Probability Fundamentals module overview


Continue Building Your Skills

You can now count the ways an outcome can happen and convert that count straight into a probability — including the genuinely tiny odds behind a lottery jackpot. In the next lesson you will turn that single number into a complete guided project, calculating real lottery odds and learning to communicate just how unlikely “you could win” really is.