Write a Python Program to print all Possible Combinations from the three Digits


The program is generating all the possible combinations of 3 elements from the list "a" using three nested for loops. The outermost for loop iterates through the elements of the list "a" and assigns the values to the variable "i". The middle for loop assigns the values to the variable "j" and the innermost for loop assigns the values to the variable "k". The program is also checking the condition if (i!=j and j!=k and i!=k) in the innermost loop, this is ensuring that the elements in the combination are distinct. If the condition is true, it will print the combination (i, j, k) using the print statement. This program will generate all possible combinations of 3 elements of the list "a".

Source Code

a = [1, 2, 3]
for i in range(3):
    for j in range(3):
        for k in range(3):
            if (i!=j and j!=k and i!=k):
                print(a[i], a[j], a[k])		

Output

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Example Programs