{"id":8237,"date":"2021-06-07T18:08:52","date_gmt":"2021-06-07T12:38:52","guid":{"rendered":"https:\/\/python-programs.com\/?p=8237"},"modified":"2021-11-22T18:38:38","modified_gmt":"2021-11-22T13:08:38","slug":"python-program-to-accept-three-digits-and-print-all-possible-combinations-from-the-digits","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-accept-three-digits-and-print-all-possible-combinations-from-the-digits\/","title":{"rendered":"Python Program to Accept Three Digits and Print all Possible Combinations from the Digits"},"content":{"rendered":"
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.<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example3:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several methods to print all the possible combinations of the three numbers some of them are:<\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Note:<\/strong><\/p>\n This method also works for three numbers as shown below<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Python has built-in techniques for finding permutations and combinations of a sequence. These techniques are included in the itertools package.<\/p>\n 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.<\/p>\n We will pass the digitsList as argument to permutations function<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Note:<\/strong><\/p>\n This method also works for three numbers as shown below<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\ngiven three numbers = 1 9 2<\/pre>\n
1 9 2\r\n1 2 9\r\n9 1 2\r\n9 2 1\r\n2 1 9\r\n2 9 1<\/pre>\n
given three numbers = 3 7 5<\/pre>\n
Enter first digit = 3\r\nEnter second digit = 7\r\nEnter third digit = 5\r\n3 7 5\r\n3 5 7\r\n7 3 5\r\n7 5 3\r\n5 3 7\r\n5 7 3<\/pre>\n
given three numbers = 44 389 72<\/pre>\n
44 389 72\r\n44 72 389\r\n389 44 72\r\n389 72 44\r\n72 44 389\r\n72 389 44<\/pre>\n
Python Program to Print all Possible Combinations of the three Digits<\/h2>\n
\n
General Approach<\/h3>\n
\n
Method #1: Using Nested loops (Static input)<\/h3>\n
\n
# given three digits here we took the input as static\r\nfirstDigit = 1\r\nsecondDigit = 9\r\nthirdDigit = 2\r\n# Taking a empty list to store the given three digits\r\ndigitsList = []\r\n# appending all the three digits to the digitsList\r\ndigitsList.append(firstDigit)\r\ndigitsList.append(secondDigit)\r\ndigitsList.append(thirdDigit)\r\n# Using nested loops\r\nfor i in range(3):\r\n for j in range(3):\r\n for k in range(3):\r\n if(i != j & j != k & k != i):\r\n print(digitsList[i], digitsList[j], digitsList[k])\r\n<\/pre>\n
1 9 2\r\n1 2 9\r\n9 1 2\r\n9 2 1\r\n2 1 9\r\n2 9 1<\/pre>\n
# given three digits here we took the input as static\r\nfirstDigit = 44\r\nsecondDigit = 53\r\nthirdDigit = 456\r\n# Taking a eempty list to store the given three digits\r\ndigitsList = []\r\n# appending all the three digits to the digitsList\r\ndigitsList.append(firstDigit)\r\ndigitsList.append(secondDigit)\r\ndigitsList.append(thirdDigit)\r\n# Using nested loops\r\nfor i in range(3):\r\n for j in range(3):\r\n for k in range(3):\r\n if(i != j & j != k & k != i):\r\n print(digitsList[i], digitsList[j], digitsList[k])\r\n<\/pre>\n
44 53 456\r\n44 456 53\r\n53 44 456\r\n53 456 44\r\n456 44 53\r\n456 53 44<\/pre>\n
Method #2: Using Nested loops (User input)<\/h3>\n
\n
# given three digits here we took the input as static\r\nfirstDigit = int(input(\"Enter first digit = \"))\r\nsecondDigit = int(input(\"Enter second digit = \"))\r\nthirdDigit = int(input(\"Enter third digit = \"))\r\n# Taking a eempty list to store the given three digits\r\ndigitsList = []\r\n# appending all the three digits to the digitsList\r\ndigitsList.append(firstDigit)\r\ndigitsList.append(secondDigit)\r\ndigitsList.append(thirdDigit)\r\n# Using nested loops\r\nfor i in range(3):\r\n for j in range(3):\r\n for k in range(3):\r\n if(i != j & j != k & k != i):\r\n print(digitsList[i], digitsList[j], digitsList[k])\r\n<\/pre>\n
Enter first digit = 3\r\nEnter second digit = 7\r\nEnter third digit = 5\r\n3 7 5\r\n3 5 7\r\n7 3 5\r\n7 5 3\r\n5 3 7\r\n5 7 3<\/pre>\n
Method #3:Using permutations() function<\/h3>\n
# importing permutations from itertools\r\nfrom itertools import permutations\r\n# given three digits here we took the input as static\r\nfirstDigit = 4\r\nsecondDigit = 3\r\nthirdDigit = 7\r\n# Taking a eempty list to store the given three digits\r\ndigitsList = []\r\n# appending all the three digits to the digitsList\r\ndigitsList.append(firstDigit)\r\ndigitsList.append(secondDigit)\r\ndigitsList.append(thirdDigit)\r\n# Using permutations function\r\n\r\ntotalcombs = permutations(digitsList, 3)\r\n# printing all combinations\r\nfor i in totalcombs:\r\n print(*i)\r\n<\/pre>\n
4 3 7\r\n4 7 3\r\n3 4 7\r\n3 7 4\r\n7 4 3\r\n7 3 4<\/pre>\n
# importing permutations from itertools\r\nfrom itertools import permutations\r\n# given three digits here we took the input as static\r\nfirstDigit = 44\r\nsecondDigit = 389\r\nthirdDigit = 72\r\n# Taking a eempty list to store the given three digits\r\ndigitsList = []\r\n# appending all the three digits to the digitsList\r\ndigitsList.append(firstDigit)\r\ndigitsList.append(secondDigit)\r\ndigitsList.append(thirdDigit)\r\n# Using permutations function\r\n\r\ntotalcombs = permutations(digitsList, 3)\r\n# printing all combinations\r\nfor i in totalcombs:\r\n print(*i)\r\n<\/pre>\n
44 389 72\r\n44 72 389\r\n389 44 72\r\n389 72 44\r\n72 44 389\r\n72 389 44<\/pre>\n
\n