Python Exercise Example 1
**Question:** Given four digits: 1, 2, 3, 4, how many three-digit numbers can be formed that are different from each other and have no repeated digits? What are they?
**Program analysis:** The digits that can be filled in the hundreds, tens, and units places are all 1, 2, 3, and 4. After forming all permutations, remove those that do not meet 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
)
:
(
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 2
Python 100 Examples