.comb()
Published Jul 23, 2024
Contribute to Docs
In Python, the .comb()
function from the math
module calculates the number of ways to choose k
items from n
items without regard to order or repetition.
Syntax
math.comb(n, k)
n
: A non-negative integer that represents the total number of items.k
: A non-negative integer that represents the number of items to choose.
The result returned is a non-negative integer representing the number of unordered choices that can be made to select k
items from n
items.
Example
The following example shows the use of the .comb()
function:
import mathn = 10k = 4no_of_choices = math.comb(n,k)print("Total ways to choose: ", no_of_choices)
This bit of code gives out the following output:
Total ways to choose: 210
Codebyte Example
The below codebyte example uses the .comb()
function in order to find out in how many ways can someone choose k
number of flowers from n
number of flowers:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours