Related Articles

# Python | SymPy Permutation.cycles() method

• Last Updated : 26 Aug, 2019

Permutation.cycles() : cycles() is a sympy Python library function that returns the number of cycles present in the permutation. This also includes the singleton.

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

Return :
number of cycles present in the permutation

Code #1 : cycles() Example

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

Output :

Permutation a – cycles form : 2
Permutation b – cycles form : 4

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

 `# Python code explaining``# SymPy.Permutation.cycles()`` ` `# importing SymPy libraries``from` `sympy.combinatorics.partitions ``import` `Partition``from` `sympy.combinatorics.permutations ``import` `Permutation`` ` `# Using from ``# sympy.combinatorics.permutations.Permutation.cycles() method `` ` `# creating Permutation``a ``=` `Permutation([[``2``, ``4``, ``0``], ``                 ``[``3``, ``1``, ``2``],``                 ``[``1``, ``5``, ``6``]])`` ` `# SELF COMMUTATING    ``print` `(``"Permutation a - cycles form : "``, a.cycles)`

Output :

Permutation a – cycles form : 1

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