Permutations Using Python

How’s it going everyone?!?

I hope your week has been great and staying healthy. Today I am going to talk about permutations using Python. The version that I am going to use for this program is Python 3.8.3.

Well, what are permutations????? Permutations in mathematics basically are just different combinations of a sequence, and that is exactly what we are going to do. We are going generate a random sequence of integers and find different combos for this sequence!

Obviously, there are always different ways to tackle a problem, but for this one, since Python already has a package for us to utilize, we can just import it instead! One of my number one rules is to never reinvent the wheel, it makes our jobs as programmers easier.

The code below demonstrates how you could write a simple permutations function and list out everything for that sequence. Remember, another rule of mine is this, always test your code! If your code lines are like hundreds to thousand lines, it’d be wise for you to test out different chunks of code and see if everything is working as you expect it to be!


# import the necessary package
from itertools import permutations

# defining the function
def findPermutations(combo):
    
    #use permutations method
    my_list = permutations(combo)

    for i in my_list:
        print(i)


# ask user to input sequence of numbers
my_list = input("Enter a list of numbers separated by a space: ")
my_list = my_list.split()

# convert all to intengers
for i in range(0, len(my_list)):
    my_list[i] = int(my_list[i])

# calling on the function findPermutations
findPermutations(my_list)

I hope this small program makes sense and helps you in some way. If you have any question, please feel free to ask! Stay tuned for more and have a blessed day!

Written by Alex Mercer

Alex Mercer is a writer and contributor at Project Immerse, covering web design, WordPress, web development, SEO, digital strategy, emerging technology, and the evolving world of the web. His work focuses on practical ideas, useful tools, and approachable guidance that help creators, developers, and business owners build better digital experiences.

Alex writes with a hands-on, curiosity-driven approach, exploring both established web practices and new technologies shaping how websites are designed, built, and discovered.