Python math.perm() Method



The Python math.perm() method is used to calculate the number of permutations of "k" items chosen from a set of "n" distinct items in a specific order. Mathematically, it is denoted as −

P(n,\:k)\:=\:\frac{n!}{(n\:-\:k)!}

Where, n! represents the factorial of "n", which is the product of all positive integers from 1 to n, and (n−k)! represents the factorial of (n-k).

For example, if you have a set of "5" distinct items and you want to arrange "3" of them, "math.perm(5, 3)" will return the number of permutations, which is 5!/(5-3)! = 5!/2! = 5 × 4 × 3 × 2 × 1/2 × 1 =60.

Syntax

Following is the basic syntax of the Python math.perm() method −

math.perm(n, k)

Parameters

This method accepts the following parameters −

  • x − It is the total number of items or elements.

  • y − It is the number of items to be arranged (permutations).

Return Value

The method returns the permutation of the given values "n" and "k".

Example 1

In the following example, we are calculating the number of permutations of selecting "3" elements from a set of "5" distinct elements −

import math
result = math.perm(5, 3)
print("The result obtained is:",result)         

Output

The output obtained is as follows −

The result obtained is: 60

Example 2

Here, we are calculating the number of permutations of selecting "2" elements from a set of "4" elements with replacement allowed −

import math
result = math.perm(4, 2)
print("The result obtained is:",result)  

Output

Following is the output of the above code −

The result obtained is: 12

Example 3

Now, we are calculating the number of permutations of arranging the letters of the word "MISSISSIPPI" while considering the duplicate letters −

import math
word = "MISSISSIPPI"
n = len(word)
result = math.perm(n)
print("The result is:",result)  

Output

We get the output as shown below −

The result is: 39916800

Example 4

In this example, we are calculating the number of permutations of selecting "0" elements from an empty set

import math
result = math.perm(0, 0)
print("The result obtained is:",result)  

Output

The result produced is as shown below −

The result obtained is: 1
python_maths.htm
Advertisements