Python for Introductory StatisticsXYZ Homework Edition

⇩ Download ▾

Factorial, Permutation, Combination

Watch demo video

The number of ways we can arrange nn distinct objects is given by:

n ! = n × ( n 1 ) × ( n 2 ) . . .2 × 1 n!=n\times (n-1)\times (n-2)...2 \times 1

The number of ways we can arrange 5 distinct objects is:

5 ! = 5 × 4 × 3 × 2 × 1 5!=5\times 4\times 3\times 2 \times 1

Syntax for factorial:

import math
math.factorial(n)

The number of ways we can arrange nn distinct objects, taking them rr at a time is given by:

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

import math
math.factorial(n)/math.factorial(n-r)

The number of distinct combinations of nn distinct objects that can be formed by taking rr at a time is given by:

n C r = n ! r ! ( n r ) ! nCr = \frac{n!}{r!(n-r)!}

import math
math.factorial(n)/(math.factorial(r)*math.factorial(n-r))

Example: For n=6n=6 and r=3r=3, compute the following:

  1. n ! n!
  2. n P r nPr
  3. n C r nCr
import math
math.factorial(6)
Show expected output
720

Interpretation: 6!=720. There 720 different arrangement of 6 distinct objects.

import math
math.factorial(6)/math.factorial(6-3)
Show expected output
120.0

Interpretation: there are 120 permutations of 6 objects taking 3 at a time.

import math
math.factorial(6)/(math.factorial(3)*math.factorial(6-3))
Show expected output
20.0

Interpretation: There are 20 combinations (without regard to order) of 6 objects taking 3 at a time.

Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.

These eBooks are a prerelease and are not yet certified conformant with WCAG 2.1 AA or ADA Title II. Every page is built against an automated accessibility gate, and the published editions will meet ADA Title II requirements when they release in late September 2026. If something is unusable, please tell us.