math.perm(n, k=None) returns the number of ways to choose and order k items from n items without repetition and with order, as an exact int.
Added in Python 3.8, omitting k (or passing None) defaults to k = n, returning n!. Like math.comb(), passing k > n returns 0.
math.perm() is a function in Python's standard math module: import it with import math and call math.perm(...), or import the name directly with from math import perm. Added in Python 3.8.
Syntax
Import math and pass n and optional selection length k (Python 3.8+).
python
import math
p = math.perm(n, k)
all_orders = math.perm(n)
Call
What it does
math.perm(5, 2)
Returns 20 (5 * 4).
math.perm(4)
Returns 24 (4!, since k defaults to None).
math.perm(3, 5)
Returns 0 (k > n).
math.perm(5, -1)
Raises ValueError: k must be non-negative.
When order does not matter, use math.comb(). Note that math.perm(n, k) only counts arrangements; to generate actual tuples, use itertools.permutations().
Function signatures
python
math.perm(n, k=None, /)
# added in Python 3.8; returns exact int n! // (n - k)!
Parameters
Parameter
Description
n
Required positional non-negative integer total count.
k
Optional non-negative integer selection size. Defaults to n when omitted or None.
Return Value
Return Value
Description
int
Exact permutation count; 0 if k > n.
Python Examples
Example 1 — Count Race Podiums and Distinct-Digit Passcodes
Evaluate ordered arrangements for race medals, 4-digit unique PINs, and full orderings.
Pure integer arithmetic. Every term stays an exact int.
Example 3 — Handle k Greater Than n, k = 0, and Invalid Inputs
Check edge cases perm(3, 5) and perm(5, 0) while catching ValueError and TypeError.
python
import math
print(math.perm(3, 5), math.perm(5, 0))
for bad_n, bad_k in [(5, -1), (5.0, 2)]:
try:
math.perm(bad_n, bad_k)
except (ValueError, TypeError):
print(f"rejected: ({bad_n!r}, {bad_k!r})")
Output
0 1
rejected: (5, -1)
rejected: (5.0, 2)
How It Works
Oversized k yields zero. Cannot arrange five distinct items out of three, so math.perm(3, 5) returns 0.
Empty selection is one. One empty arrangement exists for k = 0.
Negative and float rejected. Negative -1 raises ValueError and float 5.0 raises TypeError.
Frequently Asked Questions
math.perm() was added in Python 3.8.
k defaults to n, so math.perm(n) computes n! (all full permutations of n items).
0, because you cannot pick 5 distinct items from 3.
math.perm(n, k) returns only the count (an integer) in O(k) steps without generating items, whereas itertools.permutations() yields every arrangement tuple.