StatisticsSciPy 1.18 · pandas 3.0 · statsmodels 0.15 · Python 3.12 or 3.13
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
57 small wins to finish your pathNext lesson →

Permutations and combinations

Permutations and combinations are counting rules: a permutation counts the ordered arrangements of r items chosen from n, and a combination counts the unordered selections.

Last updated: 07 Oct, 2026 · SciPy 1.18

Every probability in Probability basics was a count of outcomes. With a die you can list them, but with 6 chocolates, 52 cards or 100 coin tosses you need a formula for the count. Permutations and combinations are those formulas.

Permutations and combinations with the chocolate factory · from the Complete Statistics for Data Science in 6 Hours video · 2:51:31 to 2:56:13

Counting ordered lists of chocolates

The video's school trip goes to a chocolate factory that makes 6 kinds: Dairy Milk, 5 Star, Milky Bar, Eclairs, Gems and Silk. A student's assignment is to write down the first three chocolates they see, in the order they see them.

The first name can be any of 6 chocolates. Once it is written, 5 are left for the second name, then 4 for the third. So there are 6 × 5 × 4 = 120 possible lists. "Dairy Milk, Gems, Milky Bar" and "Milky Bar, Gems, Dairy Milk" use the same chocolates but are different lists. Each ordered list is a permutation.

Permutations of r items from n

Here n = 6 is the number of chocolates and r = 3 is the number of names written. n! (n factorial) is n × (n − 1) × … × 1, and the (n − r)! cancels the part of 6! that is never used.

Counting unordered groups of chocolates

In a combination only the group matters, not the order. Dairy Milk, Gems and Eclairs is one combination however the three are arranged; swapping them around does not make a new one.

Combinations of r items from n

The two counts are tied together. Each group of three can be put in order in 3! = 3 × 2 × 1 = 6 ways, so each group appears 6 times among the 120 lists. Dividing removes the repeats: 120 ÷ 6 = 20. In general nCr = nPr ÷ r!.

Top, three slots with 6, 5 and 4 choices give 6 × 5 × 4 = 120 ordered lists of three chocolates. Bottom, the single group {Dairy Milk, Gems, Eclairs} can be ordered in 3! = 6 ways, so the 120 ordered lists hold 120 ÷ 6 = 20 combinations.

The question decides which to use. "The first three chocolates you see, in order" is a permutation. "Which three chocolates go into a gift box" is a combination. Both formulas assume n different items and no item used twice. When repeats are allowed, there are nʳ ordered lists (6³ = 216) and C(n + r − 1, r) unordered ones (C(8, 3) = 56).

Counting with math and itertools

math.perm and math.comb give the counts directly; itertools builds the lists themselves, so the counts can be checked one by one.

ExampleFrom the video, run on Python 3.12
import math
from itertools import permutations, combinations, combinations_with_replacement

chocolates = ["Dairy Milk", "5 Star", "Milky Bar", "Eclairs", "Gems", "Silk"]
print("6P3 =", math.perm(6, 3), "  6C3 =", math.comb(6, 3), "  3! =", math.factorial(3))

lists = list(permutations(chocolates, 3))       # ordered
groups = list(combinations(chocolates, 3))      # unordered
print("ordered lists:", len(lists), "  groups:", len(groups), "  120 / 6 =", len(lists) // 6)
print("first two lists:", lists[:2])

one = {"Dairy Milk", "Gems", "Eclairs"}
print("lists using exactly that group:", sum(1 for l in lists if set(l) == one))

print("with repeats, ordered:", 6 ** 3, "  unordered:", len(list(combinations_with_replacement(chocolates, 3))))
print("P(a random group of 3 is that group) =", 1 / math.comb(6, 3))
print("5-card hands from 52 cards:", math.comb(52, 5))

What the chocolate counts show

  • 6P3 = 120 and 6C3 = 20: the two counts worked out on the board.
  • itertools builds 120 lists and 20 groups, and 120 ÷ 6 = 20 matches.
  • The group {Dairy Milk, Gems, Eclairs} appears in 6 of the lists: its 3! orderings.
  • Repeats change the counts: 216 ordered lists and 56 groups when a chocolate may appear more than once.
  • A random group is that one group with probability 0.05, 1 out of 20, and there are 2,598,960 five-card poker hands.

Permutation vs combination

PermutationCombination
Order matters?YesNo
Formulan! ÷ (n − r)!n! ÷ (r! (n − r)!)
6 chocolates, choose 312020
Question it answersHow many ordered lists or rankings?How many groups or teams?
Pythonmath.perm(n, r)math.comb(n, r)

Where you use permutations and combinations

  • The binomial distribution: C(n, k) counts the ways to get k heads in n tosses, the first step of the Binomial distribution formula.
  • Odds of a draw: lottery tickets, card hands and passwords are counts of combinations or permutations.
  • Model search: choosing 3 of 10 features gives C(10, 3) = 120 subsets to try.
Watch out. Counting orders when only the group matters. Using 6P3 = 120 for "which three go in the gift box" counts every box 6 times; divide by r! or use nCr.
Try it yourself
  • A race has 8 runners. Count the ways to award gold, silver and bronze with math.perm(8, 3), and the ways to pick 3 finalists with math.comb(8, 3).
  • Check that math.comb(6, 2) == math.comb(6, 4): both are 15. Why does choosing 2 chocolates to take equal choosing 4 to leave behind?
  • Print len(list(permutations(chocolates))). Is it 6! = 720?

Every expert started right here.