Table of Contents
Generating Permutations with Python: A Complete Guide and Examples
Permutations
Did you know that the lock and key passwords work based on Permutations?
In lock patterns, order is what matters.
Combinations
In Combinations, order does not matter .
Permutations and Combinations in Python
Question:
Find the number of permutations and combinations that can be formed from the word HORSE taking two letters at a time.
Python Code
from itertools import combinations
from itertools import permutations
import numpy as np
import math
arr=np.array(['H','O','R','S','E'])
print(len(list(combinations(arr, 2)) ))
print(len(list(permutations(arr,2) )))
Result
10
20
Combinations with Factorial
Question:
In how many ways can 10 balls be picked, from 7 red out of 10, and 3 blue out of 8?
Python code
import math
red=math.factorial(10)/((math.factorial(7))*math.factorial(3))
blue=math.factorial(8)/((math.factorial(3))*math.factorial(5))
print(red*blue)
Result
6720