# How to use Python for Permutations and Combinations?

## Python string permutations

The permutations() function makes it simple to complete a task like discovering every possible arrangement of the letters in a Python string.

**EXAMPLE **

write your code here: Coding Playground

**OUTPUT **

The itertools object is returned by the function permutations() in exchange for a String parameter. If we attempt to print the variable "per" directly, we will obtain the results shown below:

So, in order to print each entry, a loop must be run.

## Multiple-number permutations

We must supply the numbers as a list, set, or tuple in order to find their permutations of them because the permutations() function accepts an iterable input.

**EXAMPLE **

write your code here: Coding Playground

**OUTPUT**

We include all the digits or characters in the permutation calculation methods mentioned above. The permutations can have a maximum or a minimum number of elements.

## Combinations with a specific number of components

This can be done by passing an integer after the set of elements, much like the concept of "nPr," which states "Arranging r elements out of n."

**EXAMPLE **

write your code here: Coding Playground

**OUTPUT**

The permutations() method is instructed to arrange only two elements at a time from the given list of integers in the code snippet above.

## Words with multiple letter combinations

combinations() is the function to use if you need to find all combinations that contain exactly 2 letters from a given word.

**EXAMPLE**

write your code here: Coding Playground

**OUTPUT**

## Sets of numbers combined

It is possible to achieve numbers in a list, similar to the combos result we got for letters in a word.

**Example **

**OUTPUT**

## Repetition of numerical combinations

Let's run an example of the aforementioned Note to further illustrate it.

**EXAMPLE**

**OUTPUT**

Given that the numbers in the list are repeated, the outcome is obvious.

## Number combinations that include themselves

Combinations with replacement is a further function in the itertools package that relates to permutations and combinations (). This function is a variant of combinations(), with the minor difference that it also takes into account combinations of the elements with each other.

**EXAMPLE **

**OUTPUT**

When identical arguments are supplied to the combinations() function, we can clearly distinguish between the output from the above query and the output from that query.

Numbers can be combined with one another such that more than one instance of them appears in the list. This basically occurs because the aforementioned function sets the same value when we select an element from the list in order to create combinations with other elements.

### Subscribe to our Newsletter

Receive latest industry news and updates, exclusive offers directly in your inbox.