Wednesday, July 3, 2024
HomeLanguagesPythonPython – math.perm() method

Python – math.perm() method

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




# Python Program to explain math.perm() method
  
# Importing math module
import math
  
n = 10
k = 2
  
# Get the number of ways to choose
# k items from n items without
# repetition and with order
nPk = math.perm(n, k)
print(nPk)
  
n = 5
k = 3
  
# Get the number of ways to choose
# k items from n items without
# repetition and with order
nPk = math.perm(n, k)
print(nPk)


Output:

90
60

Code #2: When k > n




# Python Program to explain math.perm() method
  
# Importing math module
import math
  
# When k > n 
# math.perm(n, k) returns 0.
n = 3
k = 5
  
# Get the number of ways to choose
# k items from n items without
# repetition and with order
nPk = math.perm(n, k)
print(nPk)


Output:

0

Code #3: If k is not specified




# Python Program to explain math.perm() method
  
# Importing math module
import math
  
# When k is not specified
# It defaults to n and 
# math.perm(n, k) returns n ! n = 5
  
nPk = math.perm(n)
print(nPk)


Output:

120

Reference: Python math library

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments