SymPy | Permutation.parity() in Python

• Last Updated : 27 Aug, 2019

Permutation.parity() : parity() is a sympy Python library function that returns the parity of the permutation. It means the parity of the number of inversions in the permutation.

Syntax : sympy.combinatorics.permutations.Permutation.parity()

Return : next permutation in lexicographical parity

Code #1 : parity() Example

 # Python code explaining# SymPy.Permutation.parity()  # importing SymPy librariesfrom sympy.combinatorics.partitions import Partitionfrom sympy.combinatorics.permutations import Permutation  # Using from sympy.combinatorics.permutations.Permutation.parity() method   # creating Permutationa = Permutation([[2, 0], [3, 1]])  b = Permutation([1, 3, 5, 4, 2, 0])    print ("Permutation a - parity form : ", a.parity())print ("Permutation b - parity form : ", b.parity())

Output :

Permutation a – parity form : 0
Permutation b – parity form : 1

Code #2 : parity() Example – 2D Permutation

 # Python code explaining# SymPy.Permutation.parity()  # importing SymPy librariesfrom sympy.combinatorics.partitions import Partitionfrom sympy.combinatorics.permutations import Permutation  # Using from sympy.combinatorics.permutations.Permutation.parity() method   # creating Permutationa = Permutation([[2, 4, 0],                  [3, 1, 2],                 [1, 5, 6]])    print ("Permutation a - parity form : ", a.parity())

Output :

Permutation a – parity form : 0

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

My Personal Notes arrow_drop_up