Python math.comb() Method
Python math.comb(n, k) Used to compute combinations, which represent the total number of ways to choose k elements from n distinct elements.
The arguments passed in must be positive integers.
Python version: 3.8
Syntax
The syntax of the math.comb() method is as follows:
math.comb(n, k)
Parameter Description:
- n -- required, a positive integer number.
- k -- required, a positive integer number.
Return Value
Returns an integer representing the total number of combinations, that is, the number of ways to choose k elements from n distinct elements.
Example
The following example returns the total number of ways to choose 5 items from 7:
Example
Import the math package
import
math
Initialize the number of items to choose from
n
=
7
Initialize the number of possibilities to choose from
k
=
5
Total number of combinations choosing 5 items from 7 items
(
math
.
comb
(
n
,
k
)
)
Output result:
21
Python math Module