CKTIS / Python
math.perm
Counts ordered selections of distinct positions without replacement.
Version: Python 3.14; repository image, production runtime not attested
Syntax
math.perm(n, k=None, /)Counts ordered selections of distinct positions without replacement.
Order matters: two items taken from five admit twenty arrangements. Selecting too many items yields zero.
There are five choices for the first slot and four for the second, giving twenty ordered pairs. If k is omitted or None, all n positions are used. With nonnegative integer arguments k > n yields zero; a negative argument raises ValueError.
import math
assert math.perm(5, 2) == 5 * 4
assert math.perm(5) == math.factorial(5) and math.perm(2, 5) == 0
Examples
import math
print(math.perm(5, 2))
print(math.perm(2, 5))
Result
20 0
There are five choices for the first slot and four for the second, giving twenty ordered pairs. If k is omitted or None, all n positions are used. With nonnegative integer arguments k > n yields zero; a negative argument raises ValueError.