Python math.perm() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Python 3.8+

Definition

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)
CallWhat 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

ParameterDescription
nRequired positional non-negative integer total count.
kOptional non-negative integer selection size. Defaults to n when omitted or None.

Return Value

Return ValueDescription
intExact 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.

python
import math

print("8 runners, 3 medals:", math.perm(8, 3))
print("4-digit unique PIN :", math.perm(10, 4))
print("all 4 items        :", math.perm(4))

How It Works

  1. Order changes the outcome. Gold, silver, and bronze from eight runners give 8 * 7 * 6 = 336 podiums.
  2. No repeated digits. Ten digits taken four at a time without reuse form 5040 passcodes.
  3. Single-argument shorthand. Omitting k evaluates math.perm(4) as full factorial 24.

Example 2 — Relate Permutations to Combinations Times k!

Verify the fundamental combinatorial identity perm(n, k) == comb(n, k) * factorial(k).

python
import math

p = math.perm(6, 3)
c = math.comb(6, 3)
print(f"perm={p}, comb={c}, c*3!={c * math.factorial(3)}")
print(p == c * math.factorial(3))

How It Works

  1. Pick then shuffle. Choosing 3 items from 6 gives 20 unordered groups; arranging each group adds 3! = 6 orders.
  2. Six-to-one factor. Multiplying 20 * 6 matches math.perm(6, 3) = 120 identically.
  3. 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})")

How It Works

  1. Oversized k yields zero. Cannot arrange five distinct items out of three, so math.perm(3, 5) returns 0.
  2. Empty selection is one. One empty arrangement exists for k = 0.
  3. 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.
No. Passing any float raises TypeError.

Next: math.pow()

Raise x to power y and return a float.

pow() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful