Monday, July 1, 2024
HomeLanguagesPythonPython | sympy.nP() method

Python | sympy.nP() method

With the help of sympy.nP() method, we can calculate the number of permutation of a given length in SymPy.

Syntax: nP(items, k)

Parameter:
items ā€“ An integer, a list or a string upon which permutation is to be calculated.
k ā€“ An integer specifying the length of the permutation.

Returns: Returns the number of permutation of a given length.

Example #1:




# import sympyĀ 
from sympy import *Ā 
Ā Ā 
items = "abca"
k = 3
print("Value of k = {} and the items are = {}".format(k, items))
Ā Ā Ā 
# Use sympy.nP() methodĀ 
permutations = nP(items, k)Ā Ā 
Ā Ā Ā Ā Ā Ā 
print("Permutation : {}".format(permutations))Ā Ā 


Output:

Value of k = 3 and the items are = abca
Permutation : 12

Example #2:




# import sympyĀ 
from sympy import *Ā 
Ā Ā 
items = [2, 1, 3, 5, 4]
k = 3
print("Value of k = {} and the items are = {}".format(k, items))
Ā Ā Ā 
# Use sympy.nP() methodĀ 
permutations = nP(items, k)Ā Ā 
Ā Ā Ā Ā Ā Ā 
print("Permutation : {}".format(permutations))Ā Ā 


Output:

Value of k = 3 and the items are = [2, 1, 3, 5, 4]
Permutation : 60

Thapelo Manthata
Iā€™m a desktop support specialist transitioning into a SharePoint developer role by day and Software Engineering student by night. My superpowers include customer service, coding, the Microsoft office 365 suite including SharePoint and power platform.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments