Classic python 100 examples 001 python3.x version

Question: There are 1, 2, 3, and 4 numbers. How many three-digit numbers can be composed of different and non-repeating numbers? How much are they?
Program analysis: The numbers that can be filled in the hundreds, tens, and ones are 1, 2, 3, and 4. After forming all the arrangements, remove the arrangements that do not satisfy the conditions.

Reference procedure:

for i in range(1,5):
    for j in range(1,5):
        for k in range(1,5):
            if i!=j and i!=k and j!=k:
                print(i,j,k)

The result of the operation is
obvious at the end of the article . This problem becomes very simple through the for loop, and there are not many changes in python2 and python3. Through this program, you should know the looping statements in Python and the corresponding nesting.
There is also a loop structure in python while loop, while loop is more complicated than for loop, this problem can also be solved by while loop.

Exercise: Enter two positive integers to get the greatest common divisor and the least common multiple.

x=int(input('x='))
y=int(input('y='))
p=x*y
while x%y!=0:
    print(x%y)
    x,y=y,x%y
print(y,p//y)

When it comes to loops, you must not mention break or continue statements. It's easy to distinguish between break terminator and continue intentionally letting you go. If you just want to be a mashup, use pass. Pass is an empty statement, which has no practical meaning, just for "draining".
Title result:

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
Published 19 original articles · Likes2 · Visits 1090

Guess you like

Origin blog.csdn.net/qq_42692319/article/details/103848454
Recommended