Python flow control statement in-depth explanation _python_ script home

Python flow control statement in-depth explanation

Updated: June 15, 2020 08:50:56 Author: I have a candy in my pocket
This article mainly introduces the relevant information about Python process control statements, the article introduces very detailed through the example code, for everyone to learn or use Python has a certain reference learning value, need friends to learn it together

1. Program structure

When the computer solves the problem, it executes all statements sequentially, selects part of the statement to execute, and iteratively executes part of the statement, respectively: sequence structure, selection structure, and loop structure. As shown below:

2. Select a statement

2.1 The simplest if statement

Python uses the reserved word if to form a selection statement, which has the following format:

if expression: Block of code

The expression can be a simple Boolean value or variable, a comparison expression or a logical expression, and if the expression value is true, the "code block" is executed; If the value is false, skip the "code block" and execute the following statement, as shown in the figure

Note: 1. In Python, if statements are also considered conditional (i.e. true) when the value of an expression is a non-zero number or a non-empty string.

·· 2. When using the if statement, if there is only one statement, then the statement block can be written directly to the right of the colon ":". This is not recommended for code readability.

3. Common mistakes:

1.if the statement is not followed by a colon number = 3 if number == 3 # is not followed by a colon, the correct is to add an English half corner colon at the end: if number == 3: print(number) 2. When using the if statement, if you need to execute multiple statements when the conditions are met, you must remember to indent the code in a logical order, otherwise the intention of the program will change, but the program will not report errors, and bugs are not easy to find. Code 1: number = 3 if number == 3: print(number) print(' This is 5') Code 2: number = 3 if number == 3: print(number) print(' This is 5')

2.2.if...... else statement

Python provides if... The else statement solves the two choice problems in the following format:

if expression: statement block 1 else: statement block 2

Use if... In the else statement, the expression can be a simple Boolean value or variable, or a comparison expression or logical expression. If the expression value is true, the "code block" is executed. If the value is false, execute the block of code after else. As shown in the picture:

Skill:

if… The else statement can be simplified using conditional expressions as follows: a = 5 if a > 0: b = a else: b = -a print(b) Simplification: a = 5 b = a if a > 0 else -a print(b)

Note: 1. When using else statements, else must not be used alone; it must be used with the reserved word if.

2. Use if... if there are more if statements than else statements in the case of else statements, the else statement will determine which IF statement the else statement belongs to.

2.3. If... elif… else statement

In the case of multiple choices when developing a program, you can use the if... elif… else statement, as follows:

if expression 1: statement block 1 elif expression 2: statement block 2 elif expression 3: statement block 3... else: statement block n

Use if... elif… In the else statement, the expression can be a simple Boolean value or variable, or a comparison expression or logical expression. If the expression value is true, the statement is executed. If the value is false, the execution statement is skipped and the next elif judgment is performed. Only if all expressions are false, the code block after the else is executed. As shown in the picture:

Note: 1. if and elif both need to judge the true or false expression, while else does not need to judge; In addition, both elif and elif need to be used together with if, and cannot be used alone.

2. Avoid the following principles when using the if statement:

(1) When a variable of Boolean type is used as the judgment condition, it is assumed that the variable of Boolean type is flag, which is a more standardized format; if flag: # is True if not flag # is False The format is not consistent with the specification: if flag == True: if flag == False: (2). Using a writing format like "if 1 == a:" prevents errors in the form "if a = 1:"

2.4 Nesting of if statements

There are three forms of if statements, all of which can be nested within each other:

(1). Nested if in the simplest if statement... The else statement has the following form:

if expression 1: if expression 2: statement block 1 else: statement block 2

(2) if... Nested if... in else The else statement has the following form:

if expression 1: if expression 2: statement block 1 else: statement block 2 else: if expression 3: statement block 3 else: statement block 4

Note: The if selection statement can have a variety of nesting methods, you can choose the appropriate nesting method according to your own needs when developing, but must strictly control the amount of indentation at different levels of code.

3. Conditional expression

In the process of program development, it is often conditional to assign values according to the results of expressions, such as returning the maximum value:

a = 6
b = 3
if a > b:
 c = a
else:
 c = b

For the above code, use conditional expressions to simplify, as follows:

a = 6
b = 3
c = a if a > b else b

4. Loop statements

4.1 while Loop

A while loop is a condition that controls whether to continue executing statements in a loop body (a body is a set of statements that are executed repeatedly).

Copy codeThe code is as follows:
while conditional expression: loop body

When the return value of the conditional expression is true, the statement in the loop body is executed. After the execution is complete, the return value of the conditional expression is judged again until the result returned by the expression is false.

Note: When using a while loop statement, do not forget to add code that changes the loop condition to Flase, otherwise, a dead loop will occur. However, the development is also inseparable from the dead loop, which can be written according to the situation.

4.2. for loop

A for loop is a loop that repeats in sequence and is typically used to enumerate, iterate over sequences, and elements in objects. The syntax is as follows:

for iteration variable in Iterable object: body of a loop

The iterated variable is used to hold the read value. The object is an iterated or iterated object. The object can be any ordered sequence object, such as string, list, tuple, etc., and the body of the loop is a group of repeated statements.

The most basic application of a for loop statement is to loop numbers and traverse strings. You can also traverse lists, tuples, collections, and dictionaries.

4.3. Loop nesting

In Python, it is allowed to nest another loop inside a loop body.

(1). Nested a while loop inside a while loop

while conditional expression 1: while conditional expression 2: loop body 2 Loop body 1

(2). Nested a for loop within a for loop

for iteration variable 1 in object 1: for iteration variable 2 in object 2: loop body 2 Loop body 1

(3). Nested a for loop inside a while loop

while conditional expression: for iteration variable in object: loop body 2 Loop body 1

(4). Nested while loops within a for loop

while conditional expression: for iteration variable in object: loop body 2 Loop body 1

Special case: Multiplication table

For I in range (0, 10) : for j in range (1, I + 1) : print (STR (j) + "*" + STR (I) + "=" + STR (I * j) + "\ t", end = "" print (" ")

5. Jump statement

When the loop meets a certain condition, the program will continue to execute if it needs to leave the loop in between, before the for loop ends and repeats, or before the while loop finds an ending condition, i.e., a break statement and a continue statement.

5.1 break Statement

break terminates all control statements in the current loop, including the for loop and while loop.

Use the break statement in while:

while conditional expression 1: execute statement if conditional expression 2: break

Use a break statement in for

for iterating variable in object: if conditional expression: break

break is used in the while statement. break is used in the for statement

5.2.continue statement

The continue statement is not as powerful as the break statement; it can only terminate this loop and enter the next loop ahead of time.

Use a continue statement in a while

while conditional expression 1: executes the code if conditional expression 2: continue

Use a continue statement in for

for iteration variable in object: if conditional expression: continue

The while statement uses continue in the for statement

Note: The difference between break and continue

The break statement is usually used in combination with the if statement to indicate that under certain conditions, the loop is broken. If nested loops are used, the break statement will break out of the innermost loop.

continue statements are also commonly used in conjunction with if statements to indicate that under certain conditions, the remaining statements in the current loop are jumped and the next loop is continued. If nested loops are used, the continue statement skips only the remaining statements in the innermost loop.

6.pass statement

In Python, the pass statement represents an empty statement, it does not do anything, generally plays a standing role, commonly used in code debugging.

# Example: for i in range(1,10): # Output numbers from 1 to 10 do not contain 10 if i % 2 == 0: # Determine if it is an even number print(i,end="") # Print an even number on the same line else: # Not an even number pass # placeholders, do nothing, just skip # output result: 2 4 6 8

Sum up

To this article about Python process control statement is introduced to this, more related Python process control statement content please search the script home previous articles or continue to browse the following related articles hope that you will support the script home in the future!

Related article

Latest comments