Python Practice Example 1
Question:With the four digits 1, 2, 3, 4, how many three-digit numbers can be formed that are all different and have no repeated digits? What are they?
Program analysis:The digits that can be placed in the hundreds, tens, and units places are 1, 2, 3, and 4. Generate all permutations, then remove those that do not satisfy the condition.
Program source code:
Example
#!/usr/bin/python
# -*- coding: UTF-8 -*-
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if( i != k ) and (i != j) and (j != k):
print (i,j,k)
The output of the above example is:
1 2 3 1 2 4 1 3 2 1 3 4 1 4 2 1 4 3 2 1 3 2 1 4 2 3 1 2 3 4 2 4 1 2 4 3 3 1 2 3 1 4 3 2 1 3 2 4 3 4 1 3 4 2 4 1 2 4 1 3 4 2 1 4 2 3 4 3 1 4 3 2Other Extensions
Python 100 Examples