Python 3.8+mathterm
math.comb
combinatorics
The binomial coefficient C(n, k): the number of ways to choose k items out of n, without repetition and without regard to order.
Syntax
math.comb(n, k)
It computes the result exactly and without blowing up intermediate values, unlike the textbook factorial formula that first builds numbers thousands of digits long and then cancels them. When k exceeds n the answer is 0 rather than an error, so swapping the arguments by mistake gives a silent zero instead of a crash; only negative values raise ValueError and non-integers raise TypeError. The symmetry C(n, k) = C(n, n - k) is often worth exploiting.
Examples
import math
print(math.comb(5, 2)) # → 10
print(math.comb(10, 3)) # → 120
print(math.comb(5, 0)) # → 1
print(math.comb(5, 5)) # → 1
print(math.comb(6, 3)) # → 20
print(math.comb(52, 5)) # → 2598960 (покер)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy