We will learn how to print all combinations of three different numbers in this python programming lesson. The application will accept three digits from the user / we give input as static and print out every possible combination of the three digits.
Examples:
Example1:
Input:
given three numbers = 1 9 2
Output:
1 9 2 1 2 9 9 1 2 9 2 1 2 1 9 2 9 1
Example2:
Input:
given three numbers = 3 7 5
Output:
Enter first digit = 3 Enter second digit = 7 Enter third digit = 5 3 7 5 3 5 7 7 3 5 7 5 3 5 3 7 5 7 3
Example3:
Input:
given three numbers = 44 389 72
Output:
44 389 72 44 72 389 389 44 72 389 72 44 72 44 389 72 389 44
Python Program to Print all Possible Combinations of the three Digits
There are several methods to print all the possible combinations of the three numbers some of them are:
Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.
General Approach
- To print out the combination of all digits, we shall utilize three loops.
- Take the user’s numbers as input. These values should be saved in three distinct variables.
- Put all of these numbers in a list.
- Print out the combination of these numbers using three for loops.
- While printing the numerals, all three loops represent the three-position. As a result, if the current index of these loops is not the same, we shall print any value.
Method #1: Using Nested loops (Static input)
Approach:
- We give the three digits input as static
- For ease of comparison, all of the items are appended to a list.
- The for loops have a value between 0 and 2, which corresponds to the indexes of the three elements in the list.
- If none of the indexes match, the element associated with the specific element in the list is printed.
Below is the implementation:
# given three digits here we took the input as static firstDigit = 1 secondDigit = 9 thirdDigit = 2 # Taking a empty list to store the given three digits digitsList = [] # appending all the three digits to the digitsList digitsList.append(firstDigit) digitsList.append(secondDigit) digitsList.append(thirdDigit) # Using nested loops for i in range(3): for j in range(3): for k in range(3): if(i != j & j != k & k != i): print(digitsList[i], digitsList[j], digitsList[k])
Output:
1 9 2 1 2 9 9 1 2 9 2 1 2 1 9 2 9 1
Note:
This method also works for three numbers as shown below
Below is the implementation:
# given three digits here we took the input as static firstDigit = 44 secondDigit = 53 thirdDigit = 456 # Taking a eempty list to store the given three digits digitsList = [] # appending all the three digits to the digitsList digitsList.append(firstDigit) digitsList.append(secondDigit) digitsList.append(thirdDigit) # Using nested loops for i in range(3): for j in range(3): for k in range(3): if(i != j & j != k & k != i): print(digitsList[i], digitsList[j], digitsList[k])
Output:
44 53 456 44 456 53 53 44 456 53 456 44 456 44 53 456 53 44
Method #2: Using Nested loops (User input)
Approach:
- The first, second, and third digits must be entered by the user.
- For ease of comparison, all of the items are appended to a list.
- The for loops have a value between 0 and 2, which corresponds to the indexes of the three elements in the list.
- If none of the indexes match, the element associated with the specific element in the list is printed.
Below is the implementation:
# given three digits here we took the input as static firstDigit = int(input("Enter first digit = ")) secondDigit = int(input("Enter second digit = ")) thirdDigit = int(input("Enter third digit = ")) # Taking a eempty list to store the given three digits digitsList = [] # appending all the three digits to the digitsList digitsList.append(firstDigit) digitsList.append(secondDigit) digitsList.append(thirdDigit) # Using nested loops for i in range(3): for j in range(3): for k in range(3): if(i != j & j != k & k != i): print(digitsList[i], digitsList[j], digitsList[k])
Output:
Enter first digit = 3 Enter second digit = 7 Enter third digit = 5 3 7 5 3 5 7 7 3 5 7 5 3 5 3 7 5 7 3
Method #3:Using permutations() function
Python has built-in techniques for finding permutations and combinations of a sequence. These techniques are included in the itertools package.
To implement the permutations function in Python, first import the itertools package. This method accepts a list as input and returns an object list of tuples containing all permutations in list form.
We will pass the digitsList as argument to permutations function
Below is the implementation:
# importing permutations from itertools from itertools import permutations # given three digits here we took the input as static firstDigit = 4 secondDigit = 3 thirdDigit = 7 # Taking a eempty list to store the given three digits digitsList = [] # appending all the three digits to the digitsList digitsList.append(firstDigit) digitsList.append(secondDigit) digitsList.append(thirdDigit) # Using permutations function totalcombs = permutations(digitsList, 3) # printing all combinations for i in totalcombs: print(*i)
Output:
4 3 7 4 7 3 3 4 7 3 7 4 7 4 3 7 3 4
Note:
This method also works for three numbers as shown below
Below is the implementation:
# importing permutations from itertools from itertools import permutations # given three digits here we took the input as static firstDigit = 44 secondDigit = 389 thirdDigit = 72 # Taking a eempty list to store the given three digits digitsList = [] # appending all the three digits to the digitsList digitsList.append(firstDigit) digitsList.append(secondDigit) digitsList.append(thirdDigit) # Using permutations function totalcombs = permutations(digitsList, 3) # printing all combinations for i in totalcombs: print(*i)
Output:
44 389 72 44 72 389 389 44 72 389 72 44 72 44 389 72 389 44
Related Programs:
- Python Program to Find the Largest Among Three Numbers
- Print all Integers that Aren’t Divisible by Either 2 or 3 and Lie between 1 and 50 in C++ and Python
- Python Program to Form an Integer that has the Number of Digits at Ten’s Place and the Least Significant Digit of the Entered Integer at One’s Place
- Python Program to Check if a Date is Valid and Print the Incremented Date
- Program to Print Collatz Conjecture for a Given Number in C++ and Python