Home Python math Functions comb() Python math.comb() Function 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+).
import math
ways = math.comb(n, k) Call What 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 math.comb(n, k, /)
# added in Python 3.8; returns exact int n! // (k! * (n - k)!) Parameters Parameter Description nRequired positional non-negative integer total count. kRequired positional non-negative integer selection size.
Return Value Return Value Description intExact number of combinations; 0 if k > n.
Python Examples 1. Committees and lotteries 2. Pascal triangle row 3. Card odds and errors Example 1 — Count Team Lineups and Lottery Combinations Compute unordered selections across small teams, full lotteries, and oversized k.
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)) 5 choose 2 : 10
49 choose 6: 13983816
5 choose 0 : 1
5 choose 8 : 0 How It Works Two-person pairs. Five people form 10 unique unordered pairs.Large lottery odds. Choosing 6 numbers out of 49 gives nearly fourteen million combinations cleanly without float overflow.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.
import math
row = [math.comb(6, k) for k in range(7)]
print("row 6:", row)
print("sum :", sum(row), sum(row) == 2 ** 6) row 6: [1, 6, 15, 20, 15, 6, 1]
sum : 64 True How It Works Symmetric binomial row. Values mirror cleanly around the middle term 20.Exact subset count. Summing all binomial coefficients for n = 6 yields 64, matching total subsets 2**6.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.
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})") two aces probability: 0.004525
rejected: (-1, 2)
rejected: (5.0, 2) How It Works Combinatorial odds. Dividing 6 ace pairs by 1326 two-card hands gives 1/221 (0.004525).Negatives rejected. Negative -1 raises ValueError.Floats rejected. Passing 5.0 raises TypeError because only integer arguments are valid.Frequently Asked Questions Which Python version added math.comb()? math.comb() was added in Python 3.8.
What is the difference between math.comb() and math.perm()? comb(n, k) counts unordered selections (AB and BA count once); perm(n, k) counts ordered arrangements (AB and BA count twice).
Why does math.comb(5, 8) return 0 instead of raising ValueError? Mathematically there are zero ways to choose more distinct items than are available, so Python returns 0.
Does math.comb() overflow on large inputs like math.comb(100, 50)? No, it returns an exact arbitrary-precision integer and cancels factors as it multiplies.
Can I pass floats to math.comb()? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion