Python – math.perm() method
Last Updated :
23 Jan, 2020
Math module in Python contains a number of mathematical operations, which can be performed with ease using the module. math.perm()
method in Python is used to get the number of ways to choose k items from n items without repetition and with order. It Evaluates to n! / (n – k)! when k <= n and evaluates to 0 when k > n.
This method is new in Python version 3.8.
Syntax: math.perm(n, k = None)
Parameters:
n: A non-negative integer
k: A non-negative integer. If k is not specified, it defaults to None
Returns: an integer value which represents the number of ways to choose k items from n items without repetition and with order. If k is none, method returns n!.
Code #1: Use of math.perm()
method
import math
n = 10
k = 2
nPk = math.perm(n, k)
print (nPk)
n = 5
k = 3
nPk = math.perm(n, k)
print (nPk)
|
Code #2: When k > n
import math
n = 3
k = 5
nPk = math.perm(n, k)
print (nPk)
|
Code #3: If k is not specified
import math
nPk = math.perm(n)
print (nPk)
|
Reference: Python math library
Share your thoughts in the comments
Please Login to comment...