03 judgment and loop | introduction to Python

This chapter discusses two basic program structures, judgment and loop.

Condition control

if statement

When the condition is true, execute the code after if; Otherwise, do not execute.

if condition:
    code

When condition is True, execute code.

cel = int(input('Enter today's temperature:'))
if cel >= 30:
    print('Turn on the air conditioner')

Operation results

Enter today's temperature: 32
 Turn on the air conditioner

If else statement

When the condition is true, execute the code after if; Otherwise, execute the code after else.

if condition:
    code1
else:
    code2

When condition is True, execute code1; Otherwise, execute code2.

cel = int(input('Enter today's temperature:'))
if cel >= 30:
    print('Turn on the air conditioner')
else:
    print('No air conditioning')

Operation results

Enter today's temperature: 13
 No air conditioning

If elif else statement

if condition1:
    code1
elif condition2:
    code2
···
else:
    code

When the first condition = true, the code corresponding to condition 2 will not be checked in turn (when the first condition = true, n will not be checked in turn); When all conditions are False, execute the code after else.

x = 0.5
if x < 1:
    print('x Less than 1')
elif x > 0:
    print('x Greater than 0')  # Skipped when x = 0.5
else:
    print('x Less than or equal to 0 or greater than or equal to 1')  # It can never be implemented

Operation results

x Less than 1

In the above example, x < 1 is True, print('x < 1 ') is executed, and subsequent elif and else are skipped.

Nested if statement

Conditional control statements can contain conditional control statements, which is called nesting

cel = int(input('Enter today's temperature:'))
if cel >= 30:
    print('Turn on refrigeration mode')
    if cel >= 35:
        print('Go buy ice cream~')
elif cel <= 10:
    print('On heating mode')
    if cel <= 0:
        print('Drink more hot water!')
else:
    print('No air conditioning')

Operation results

Enter today's temperature: 40
 Turn on refrigeration mode
 Go buy ice cream~

Circular statement

for loop

For loop (counting loop), execute the code after for n times.

for x in range(5):
    print(x)

Operation results

0
1
2
3
4

In the above example, the statement in the for loop is executed five times. The value of x starts at 0.

while loop

While loop (until loop), execute the code after while repeatedly until the condition is false.

x = 0
while x < 5:
    print(x)
    x += 1

Operation results

0
1
2
3
4

As long as the condition after while is True, execute the statement after while repeatedly; End the loop until the condition after while is False.

Loop control statement

continue statement

continue is used to skip the rest of this cycle and enter the next cycle.

for x in range(10):
    if x == 5:
        continue
    print(x)

Operation results

0
1
2
3
4
6
7
8
9

When continue is encountered, skip the rest of this cycle and execute the next cycle. In the above example, when x = 5, the subsequent print(x) is skipped.

continue can also be used for while loops.

x = 0
while x < 10:
    if x == 5:
        x += 1
        continue
    print(x)
    x += 1

Operation results

0
1
2
3
4
6
7
8
9

In the above example, if there is no x += 1 before continue and x = 5, the self increment of X is skipped again. After that, X is always 5, which forms an dead cycle. The solution to the dead loop is to use the keyboard interrupt and press CTRL+C at the same time. Please try it yourself.

break statement

break is used to end the execution loop body.

for x in range(10):
    if x == 5:
        break
    print(x)

Operation results

0
1
2
3
4

When a break is encountered, skip the rest of the loop and end the loop. You can try to write print(x) in front of if x == 5: and compare the running results of the two writing methods.

break can also be used for while loops.

x = 0
while True:
    if x == 5:
        break
    print(x)
    x += 1

Operation results

0
1
2
3
4

For else and while else

for loop and while loop can be followed by else. After the loop ends, execute the statement after else.

for x in range(5):
    print(x)
else:
    print('End of cycle')

Operation results

0
1
2
3
4
 End of cycle

If a break occurs in the middle of the loop and terminates, the statement after else will not be executed.

x = 0
while True:
    if x == 5:
        break
    print(x)
    x += 1
else:
    print('End of cycle')

Operation results

0
1
2
3
4

loop nesting

Loops can also be nested. The following example outputs the 99 multiplication formula table

for y in range(1, 10):  # The value of y is from 1 to 9
    for x in range(1, y + 1):  # The value of x is from 1 to y
        print(x, '*', y, '=', int(x * y), end=' ')
        if x * y < 10:
            print(' ', end='')  # Alignment (optional)
    print()  # It's OK at the end of the line

Operation results

1 * 1 = 1  
1 * 2 = 2  2 * 2 = 4  
1 * 3 = 3  2 * 3 = 6  3 * 3 = 9  
1 * 4 = 4  2 * 4 = 8  3 * 4 = 12 4 * 4 = 16 
1 * 5 = 5  2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25 
1 * 6 = 6  2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36 
1 * 7 = 7  2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49 
1 * 8 = 8  2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64 
1 * 9 = 9  2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81 

Keywords: Python Back-end

Added by greggustin on Mon, 31 Jan 2022 08:53:00 +0200