Write the Python Program to Print All Possible Combination of Integers

In this tutorial, we will write the Python program that prints all possible combinations of the list elements. We will take the three distinct integers as an input and print all possible combinations from the digits. It is quite an easy program; it may ask in the interview for the entry-level job.

Problem Solving Approach

  • First, accepts the three unique integer values.
  • Then, add to the list using the append() method.
  • Now, use the three for loops and print the integer number in the list if none of their indexes are equal to each other.
  • Terminate the loop.

Let's see the following example.

Python Program

Program

Output:

Enter first number: 1
Enter second number: 2
Enter third number: 3
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3

Explanation -

In the above code, we take the three unique integer numbers and append to the list. The for loop iterated 0 to 2 which shows basically the indexes of the three elements in the list. We used the two conditions in if statement, if none of indexes are equal or indexes are equal the element associated with the particular element in the list is printed.

In the below example, we will only print the combination of the distinct elements. Let's understand the following example.

Example - 2: Print the combination of unique elements.

Output 1:

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

Output 2:

Enter first number: 11
Enter second number: 12
Enter third number: 13
11 12 13
11 13 12
12 11 13
12 13 11
13 11 12
13 12 11