Login
📚 Python for Introductory Statistics
Chapters ▾

Factorial, Permutation, Combination

Watch demo video

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

n ! = n × ( n 1 ) × ( n 2 ) . . .2 × 1

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

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

Syntax for factorial:

import numpy as np
np.math.factorial(n)

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

n P r = n ! ( n r ) !

import numpy as np
np.math.factorial(n)/np.math.factorial(n-r)

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

n C r = n ! r ! ( n r ) !

import numpy as np
np.math.factorial(n)/(np.math.factorial(r)*np.math.factorial(n-r))

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

  1. n !
  2. n P r
  3. n C r
import numpy as np
np.math.factorial(6)
Show expected output
720

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

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

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

import numpy as np
np.math.factorial(6)/(np.math.factorial(3)*np.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.