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
- Calculate permutations (ordered arrangements) with
math.perm - Calculate combinations (unordered selections) with
math.comband 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 ways and a second independent choice in ways, then the two choices together can be made in 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)24There 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: .
That product has a name. The factorial of a non-negative integer , written , is the product of all positive integers up to :
Python’s math module computes it directly:
import math
print(math.factorial(5))120So there are 120 ways to arrange 5 books — and factorials grow ferociously: 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
One value looks strange at first: , not 0.
print(math.factorial(0))1There is exactly one way to arrange nothing — the empty arrangement. Defining 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 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 items chosen from distinct items is:
The logic mirrors the bookshelf: the first position has choices, the next , down through positions. Dividing by 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))720There are 720 possible medal outcomes. You can see the counting principle inside it: . 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 .
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.
The number of ways to choose items from distinct items is:
This is the permutation formula with one extra division by . That erases the orderings: every group of people can be arranged in 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))120There are 120 possible committees. Compare that to the 720 medal outcomes from the same 10 people: the committee count is exactly , because orderings of each trio collapse into a single committee.
The key contrast
This factor of is the whole difference between the two ideas. Permutations always count at least as many outcomes as combinations, and for they count strictly more:
The figure below makes the gap visible. Both start equal at (choosing one item is the same whether or not order matters), then permutations shoot upward as grows because every selection can be ordered in more and more ways.
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:
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.4667So 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,816There 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:
print(1 / math.comb(49, 6))7.151123842018516e-08That 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 of items and order matters. Combination (math.comb): you are selecting of 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 . 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 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 so the formulas stay consistent. Permutations count ordered arrangements with , while combinations count unordered selections with — differing by exactly a factor of . 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.
- Factorial — ; the number of ways to order distinct items, with .
- Permutation — an ordered arrangement; , computed with
math.perm. - Combination — an unordered selection; , 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.