Python Tutorial Python Advanced Python References Python Libraries

Python math - comb() Function



The Python math.comb() function returns the number of ways to choose k items from n items without repetition and without order. It is also known as combinations and mathematically can be expressed as:

combination

Where k ≤ n. If k > n, the function returns zero.

Syntax

#New in version 3.8
math.comb(n, k)   

Parameters

n Required. Specify the number of item to choose from.
k Required. Specify the number of item to choose.

Return Value

Returns the number of ways to choose k items from n items without repetition and without order.

Exceptions

  • Throws TypeError if either of the arguments are not integers.
  • Throws ValueError if either of the arguments are negative.

Example:

The example below shows the usage of comb() function.

import math

#choosing 2 items from 3 items
#without repetition and without order
print(math.comb(3, 2))

#choosing 5 items from 10 items
#without repetition and without order
print(math.comb(10, 5))

The output of the above code will be:

3
252

❮ Python Math Module