Python entry to proficient (2): process control

Python's flow control can be divided into three categories: sequential structure, branch structure , loop structure

One, the sequence structure

As the name implies, the sequence structure determines the execution order of the code, and the execution flow of Python code is from top to bottom. If the intermediary is blocked during execution, it will not continue to execute, and will continue to execute until the end of the blocking .

 

Second, the branch structure

When the code with a simple branch structure is executed to a certain position, a selection judgment is made to absolutely continue the direction of execution. A frequently used branch structure is the if statement. An if statement is a simple logical execution sequence that selects the execution location of the code through a judgment condition.

 

2.1 bool type

boolean type

There are only two possibilities for this type of value, that is, his value is only true and false.

There are several ways to represent truth in python

True: Indicates that the judgment result is true, that is, the condition is established
1: Indicates that the judgment result is true, that is, the condition is established

There are several ways to represent false in python

False: indicates that the judgment result is false, that is, the condition is not established
0: indicates that the judgment result is false, that is, the condition is not established
"": This is an empty string, which also means that the judgment result is false, that is, the condition is not established

 

2.2 if syntax format

Single branch: only judge one condition, and only specify the code to be executed when the condition is true

Syntax format:

if a < b :             #判断a是否小于b条件满足执行下边代码
    print("a小于b")    #输出a小于b

 

Double branch: only judges one condition, but not only specifies the code to be executed when the condition is established, but also specifies the code to be executed when the condition is not established

Syntax format:

if a < b :              #判断a是否小于b条件满足执行下边代码
    print("a小于b")     #输出a小于b
else :                  #上面代码执行正常执行完毕执行else中的内容
    print("a大于b")

 

Multi-branch: You can judge multiple conditions, meet different conditions, and execute different codes

Syntax format:

if a < b :
    print("a小于b")
elif a > b :
    print("a大于b")
elif a == b :
    print("a等于b")
elif a + b > i :
    print("a加b大于i")

Third, the loop structure

    When the code is executed in sequence, the condition is judged when it reaches a certain position. When the condition is met, the loop body of the statement is executed, and when the condition is not met, the loop body is skipped and the code continues to be executed. There are two built-in loop structures, while and for. For can be used to perform loop logic. It is often used to traverse a container and process the elements in the container through the loop body in turn.

3.1 while loop

while loop is suitable for not sure how many times to use the loop

Format 1:

while a > b:              #判断是否满足条件,满足条件执行下边代码
   代码代码代码

Working logic [execution process]

Step 1: First determine whether the conditions are met

If it is satisfied: start executing the code in the loop, after the code is executed, judge whether the condition is satisfied again
If not satisfied: skip the loop

Format 2:

while 判断条件:
代码1代码1代码1 
else:            # else中的代码当while循环中的代码执行完成以后,才会执行
代码2代码2

When the condition is met, code 1 will be executed.
When the condition is not met, code 2 will be executed.

 

3.2 for loop

The difference between the use of for and while loops

The while loop is suitable for use in infinite loops or when the number of loops is uncertain. The
for loop is suitable for use when the number of loops is clear.

Format:

for  变量  in  
列表:代码1代码1
else:          # 当for循环结束的时候,才会去执行else中的代码2代码2代码2

range method

range() returns an iterable object (the type is an object), not a list type, so the list will not be printed when printing

range(start, stop, step)

3.3 Loop Control

break: When in the loop, execute to break, the loop stops immediately
continue: When in the loop, execute to continue, terminate the current loop, and directly start the next loop

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_67281781/article/details/123423119