Python math.comb() Function

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

Definition

math.comb(n, k) returns the number of ways to choose k items from n items without repetition and without order (the binomial coefficient), as an exact int.

Added in Python 3.8, it returns 0 when k > n and avoids building huge intermediate factorials.

math.comb() is a function in Python's standard math module: import it with import math and call math.comb(...), or import the name directly with from math import comb. Added in Python 3.8.

Syntax

Import math and pass two non-negative integers (Python 3.8+).

python
import math

ways = math.comb(n, k)
CallWhat it does
math.comb(5, 2)Returns 10.
math.comb(5, 0)Returns 1.
math.comb(5, 8)Returns 0 (k > n gives 0, not an error).
math.comb(-1, 2)Raises ValueError: inputs must be non-negative.

When order matters, use math.perm(). Both arguments must be integers (floats raise TypeError).

Function signatures
python
math.comb(n, k, /)
# added in Python 3.8; returns exact int n! // (k! * (n - k)!)

Parameters

ParameterDescription
nRequired positional non-negative integer total count.
kRequired positional non-negative integer selection size.

Return Value

Return ValueDescription
intExact number of combinations; 0 if k > n.

Python Examples

Example 1 — Count Team Lineups and Lottery Combinations

Compute unordered selections across small teams, full lotteries, and oversized k.

python
import math

print("5 choose 2 :", math.comb(5, 2))
print("49 choose 6:", math.comb(49, 6))
print("5 choose 0 :", math.comb(5, 0))
print("5 choose 8 :", math.comb(5, 8))

How It Works

  1. Two-person pairs. Five people form 10 unique unordered pairs.
  2. Large lottery odds. Choosing 6 numbers out of 49 gives nearly fourteen million combinations cleanly without float overflow.
  3. Zero ways if k > n. Choosing 8 from 5 returns 0, while choosing 0 items always has 1 way.

Example 2 — Generate Row 6 of Pascal's Triangle

Build a full binomial row with a list comprehension and verify its sum equals 2 ** 6.

python
import math

row = [math.comb(6, k) for k in range(7)]
print("row 6:", row)
print("sum  :", sum(row), sum(row) == 2 ** 6)

How It Works

  1. Symmetric binomial row. Values mirror cleanly around the middle term 20.
  2. Exact subset count. Summing all binomial coefficients for n = 6 yields 64, matching total subsets 2**6.
  3. Zero-overhead formula. List comprehension stays concise and completely integer-exact.

Example 3 — Compute Card Pair Odds and Catch Negative/Float Errors

Divide combinations for a two-ace hand probability and trap invalid argument types.

python
import math

prob = math.comb(4, 2) / math.comb(52, 2)
print(f"two aces probability: {prob:.6f}")

for bad_n, bad_k in [(-1, 2), (5.0, 2)]:
    try:
        math.comb(bad_n, bad_k)
    except (ValueError, TypeError):
        print(f"rejected: ({bad_n!r}, {bad_k!r})")

How It Works

  1. Combinatorial odds. Dividing 6 ace pairs by 1326 two-card hands gives 1/221 (0.004525).
  2. Negatives rejected. Negative -1 raises ValueError.
  3. Floats rejected. Passing 5.0 raises TypeError because only integer arguments are valid.

Frequently Asked Questions

math.comb() was added in Python 3.8.
comb(n, k) counts unordered selections (AB and BA count once); perm(n, k) counts ordered arrangements (AB and BA count twice).
Mathematically there are zero ways to choose more distinct items than are available, so Python returns 0.
No, it returns an exact arbitrary-precision integer and cancels factors as it multiplies.
No. Both arguments must be integers, otherwise TypeError is raised.

Next: math.copysign()

Return a float with the magnitude of x and sign of y.

copysign() →

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.

8 people found this page helpful