math.comb() Method in Python:
The math. comb() method, also known as combinations, returns the number of ways to choose k unordered outcomes from n possibilities without repetition.
Note: It should be noted that the parameters passed in this method must be positive integers.
Syntax:
math.comb(n, k)
Parameters
n: This is Required. It is the positive integers of items from which to choose
k: This is Required. It is the positive integers of items to choose
Note:
- It should be noted that if the value of k is greater than the value of n, the result will be 0.
- A ValueError occurs if the parameters are negative. A TypeError occurs if the parameters are not integers.
Return Value:
Returns an integer value representing the total number of possible combinations.
Examples:
Example1:
Input:
Given n = 5 Given k = 3
Output:
The total number of combinations possible for the given n, k values = 10
Example2:
Input:
Given n = 6 Given k = 4
Output:
The total number of combinations possible for the given n, k values = 15
math.comb() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import math module using the import keyword.
- Give the number of items from which to choose(n) as static input and store it in a variable.
- Give the number of possibilities to choose(k) as static input and store it in another variable.
- Pass the given n, k values as the arguments to the math.comb() function to get the total number of combinations possible.
- Store it in another variable.
- Print the above result.
- The Exit of the Program.
Below is the implementation:
# Import math module using the import keyword
import math
# Give the number of items from which to choose(n) as static input and
# store it in a variable.
gvn_n_valu = 5
# Give the number of possibilities to choose as static input and
# store it in another variable.
gvn_k_valu = 3
# Pass the given n, k values as the arguments to the math.comb() function to get
# the total number of combinations possible.
# Store it in another variable.
totl_combintns = math.comb(gvn_n_valu, gvn_k_valu)
# Print the above result.
print("The total number of combinations possible for the given n, k values = ", totl_combintns)
Output:
The total number of combinations possible for the given n, k values = 10
Note:
This function works only in latest versions like 3.8
Method #2: Using Built-in Functions (User Input)
Approach:
- Import math module using the import keyword.
- Give the number of items from which to choose(n) as user input using the int(input()) function and store it in a variable.
- Give the number of possibilities to choose(k) as user input using the int(input()) function and store it in another variable.
- Pass the given n, k values as the arguments to the math.comb() function to get the total number of combinations possible.
- Store it in another variable.
- Print the above result.
- The Exit of the Program.
Below is the implementation:
# Import math module using the import keyword
import math
# Give the number of items from which to choose(n) as user input using
# the int(input()) function and store it in a variable.
gvn_n_valu = int(input("Enter some random number = "))
# Give the number of possibilities to choose(k) as user input using the int(input())
# function and store it in another variable.
gvn_k_valu = int(input("Enter some random number = "))
# Pass the given n, k values as the arguments to the math.comb() function to get
# the total number of combinations possible.
# Store it in another variable.
totl_combintns = math.comb(gvn_n_valu, gvn_k_valu)
# Print the above result.
print("The total number of combinations possible for the given n, k values = ", totl_combintns)
Output:
Enter some random number = 6 Enter some random number = 4 The total number of combinations possible for the given n, k values = 15