Python 3.8+mathterm
math.perm
combinatorics
The number of permutations P(n, k): how many ordered selections of k items out of n there are, without repetition.
Syntax
math.perm(n, k=None)
The difference from math.comb is that order counts: there are exactly k! times more permutations than combinations, so reach for perm when arrangements matter and comb when only the set does. Omitting the second argument returns n!, the same value math.factorial gives. As with comb, k greater than n quietly yields 0, while negative arguments raise ValueError.
Examples
import math
print(math.perm(5, 2)) # → 20
print(math.perm(10, 3)) # → 720
print(math.perm(5)) # → 120 (= 5!)
print(math.perm(5, 0)) # → 1
print(math.perm(5, 1)) # → 5
print(math.perm(4, 4)) # → 24 (= 4!)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy