Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Exercise Example 1

Python Exercise Example 1

Python 100 Examples Python 100 Examples

**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

)

:

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 2

Python 100 Examples Python 100 Examples