Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python math.comb() Method

Python math.comb() Method

Python math Module Python math Module

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:

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

print

(

math

.

comb

(

n

,

k

)

)

Output result:


21

Python math Module Python math Module