Math module in Python contains a number of mathematical operations, which can be performed with ease using the module.
math.comb() method in Python is used to get the number of ways to choose k items from n items without repetition and without order. It basically evaluates to n! / (k! * (n – k)!) when k n. It is also known as binomial coefficient because it is equivalent to the coefficient of k-th term in polynomial expansion of the expression (1 + x)n.
This method is new in Python version 3.8.
Syntax: math.comb(n, k)
n: A non-negative integer
k: A non-negative integer
Returns: an integer value which represents the number of ways to choose k items from n items without repetition and without order.
Code #1: Use of
Code #2: When k > n
Code #3: Use of
math.comb() method to find coefficient of k-th term in binomial expansion of expression (1 + x)n
Reference: Python math library
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course