CKTIS / Python
math.comb
Counts ways to choose distinct positions without replacement and without regard to order.
Version: Python 3.14; repository image, production runtime not attested
Syntax
math.comb(n, k, /)Counts ways to choose distinct positions without replacement and without regard to order.
Choosing more positions than available yields zero. Both arguments must be nonnegative integers.
Selecting two positions from five gives ten pairs because each unordered pair has two orderings. When k exceeds n no selection exists. Negative integers raise ValueError, and a float such as 5.0 raises TypeError.
import math
assert math.comb(5, 2) == 5 * 4 // 2
assert math.comb(5, 0) == 1 and math.comb(2, 5) == 0
Examples
import math
print(math.comb(5, 2))
print(math.comb(2, 5))
Result
10 0
Selecting two positions from five gives ten pairs because each unordered pair has two orderings. When k exceeds n no selection exists. Negative integers raise ValueError, and a float such as 5.0 raises TypeError.