In this simple python program, we need to read three digits and print all of the combinations. It's an intermediate-level python program.
To understand this example, you should have knowledge of the following Python programming topics:
In this python program, we need to accept three digits, and we have to print all the combinations of those digits. So the only condition to check for this python program is there will not be any repeating in the digits. We assure that by using an if condition in python.
Let us make it clear by taking an example of three digits 1, 2, 3, then the possible combinations are 1 2 3, 1 3 2, 2 3 1, we will never get any number repeated like 1 1 2.
To get this python problem solved, we take the digits from the user and append the digits to a list by using the append
method in python language. We are using three for loops nested to take every digit and print all the combinations from the digits. If
condition in the nested for loop
will check for any repeat in the digits of the combination. If we found any repetition, then we will not print that combination.
STEP 1: Enter the 3 input digits and save the digits into the variables using the input method and convert that string to an integer using the int()
in the Python programming language.
STEP 2: Initialize a list with zero values.
STEP 3: Using the append
method, we are assigning the values to the python list.
STEP 4: Open three nested for loop
from zero to 3. The length of the digits is to take every digit and check every combination with the 3 digits.
STEP 5: Use an if
condition in python language to check the values of the digits are the same or not. If it is not the same, then print that combination in python.
a=int(input("Enter first number:"))
b=int(input("Enter second number:")) # accept the digits from the user
c=int(input("Enter third number:"))
d=[]
d.append(a)
d.append(b) # append the digits into the list
d.append(c)
for i in range(0,3):
for j in range(0,3): # nested for loop to take each combination
for k in range(0,3):
if(i!=j&j!=k&k!=i):
print(d[i],d[j],d[k])
Enter first number : 1 Enter second number : 2 Enter third number : 3 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1