Sequential structure
Programs in the computer are executed from top to bottom, that is, the previous statements are executed first and the subsequent statements are executed later. If the previous statements are abnormal, the subsequent statements will not be executed. This is the sequential structure, and Python syntax also follows this structure.
Select structure
In Python programs, sometimes which statement to execute later depends on the previous judgment condition p. if the condition is true, execute code block A and not execute code block B, there is only one execution case, which is the selection structure. There are three types of selection structure statements in Python: single branch selection statements, double branch selection statements and multi branch selection statements.
Single branch selection
if statement
Syntax format:
if condition: Code block#You can also put a blank placeholder for pass, which means that no operation is performed
Determine whether to execute the code block in the if through condition judgment. If the condition is true, execute the if code block, and then execute the subsequent code; If the condition is not true, execute the subsequent code directly.
Example:
a=10 b=-10 if a>0:#a> 0 condition holds, execute output print('a Greater than 0') if b>0:#b> 0 condition does not hold, output will not be executed print('b Greater than 0') print('a Value of:',a) print('b Value of:',b)
Double branch selection
If else statement
Syntax format:
if condition: Code block 1#You can also put a blank placeholder for pass, which means that no operation is performed else: Code block 2#You can also put a blank placeholder for pass, which means that no operation is performed
Execute the code block through condition judgment. If the condition is true, execute the code block 1 in if. If the condition is not true, execute the code block 2 in else. After the execution of code block 1 or code block 2, execute the subsequent code.
Example:
a=10 if a>0: print('a Is a positive number')#Execute when the condition of if is true else: print('a Is a negative number')#Execute when the condition of if does not hold
Ternary operation statement
Syntax format:
Code block 1 if condition else Code block 2
Python's ternary operation statement is a short version of if else statement. The principle is the same, that is, if the condition is true, execute code block 1 before if, if the condition is not true, execute code block 2 after else.
Example:
a=-10 a=a if a>0 else -a#A is a positive number that returns a, a is a negative number that returns - A print('a Value of:',a)#Value of a: 10
Multi branch selection
If elif else statement
Multi branch statements in most languages are implemented through switch statements, but Python does not support switch statements, so Python's multi branch selection is implemented through if elif else. There can only be one if or else in this statement, while elif can have multiple.
Syntax format:
if Condition 1: Code block 1 elif Condition 2: Code block 2 elif Code block 3: Code block 3 ... else: Code block 4
Judge which code block to execute through the conditions of if or elif. If the conditions in if or elif are true, execute the corresponding code block. If the conditions of if and elif are not true, execute the code block after else.
Example:
num=1 if num==1: print('Monday') elif num==2: print('Tuesday') elif num==3: print('Wednesday') elif num==4: print('Thursday') elif num==5: print('Friday') elif num==6: print('Saturday') elif num==7: print('Sunday') else: print('Not 1~7 Number of ranges')
Cyclic structure
Sometimes we need to do things repeatedly, such as printing 1 ~ 100. It is impossible for us to write 100 output statements. It may be possible when the data is small. If the data is large, we still need to do it manually, which is very inefficient. Therefore, a loop structure is introduced to help us solve the things that need repeated operations, There are two types of loop structure statements in Python: for loop and while loop.
for loop statement
Generally, the for loop is used for sequence traversal and loop operation. It is often used together with the range(n) function. The range() function generally plays the role of determining the number of cycles in the for loop.
Usage of range() function:
range([start,]end[,sep]) can generate a series of integers from [start, end])
start indicates the first integer at the beginning. It starts with 0 by default and can not be written.
end represents the last integer generated (not included, so the last integer is: end-1)
sep represents the difference between each two numbers in the generated series of numbers. It defaults to 1 and can not be written.
In the case of range parameter, one parameter is range(end), two parameters are range(start,end), and three parameters are range(start,end,sep)
Syntax format:
for variable in Iterative sequence: Code block
Example:
list1=[] #Cyclic operation for i in range(10):#Cycle 10 times list1.append(i)#Each time you loop, you add an element to the list print("Added completed!")#Print when operation is complete #Sequence traversal for i in list1:#Take one element from list1 at a time and name it i print(i,end=" ")#Printing is performed every time an element is taken out print("\n Output completed!")#Print when operation is complete
while loop statement
Both the while loop and the for loop perform repetitive operations, but the difference is that the for loop is generally used to determine the number of operations, while the while loop is used to determine the number of operations. It should be noted that the while loop must have an end flag, otherwise it will fall into a dead loop.
Syntax format:
while Conditions: Code block
Example:
a=1 while a<10:#Enter the cycle when a < 10 print(a,end=" ") a+=1#Change the value of a to prevent dead circulation
Jump statement
break statement
Break is used to terminate subsequent loops and jump out of the loop structure. It is often used in conjunction with while. After break, the code behind break in the loop body is not executed, and the subsequent code outside the loop structure is directly executed.
Example:
a=1 while True:#The while condition is written to True to indicate an endless loop print(a,end=" ") if a==10:#Judge the condition. If a=10, execute break and jump out of the while loop break a+=1 print('\n After the cycle a Value of:',a)#After the cycle, the value of a is still 10, indicating that a+=1 is not executed after the break
continue Statement
The continue statement is similar to the break statement. It is used to stop the loop, but the break will jump out of the loop structure after stopping the loop, while the continue statement only terminates the loop and will not jump out of the loop structure; After continue is executed, the subsequent code in the loop body will not be executed. Continue is often used in conjunction with the for loop.
Example:
for i in range(10): if i%2==0:#If i can be divided by 2, it is even continue#Terminate this even number of loops, do not execute the subsequent code of this loop body, and execute the next loop print(i,end=" ")
pass statement
The pass statement is used for blank placeholders, such as loop body, function body, class body, etc. when you don't know how to write it, you can use pass to occupy the position. It won't perform any operation, just to ensure the integrity of the syntax structure.
Example:
a=10 if a==10: pass for i in range(10): pass
Nesting structures
Select branch nesting
Sometimes a condition can not meet our requirements. We can nest the selection branch statements, that is, write the selection branch statements from the code block of the selection branch statements, and each selection branch statement can be nested with each other.
Example:
#Single branch statement nesting a=10 if a>0: if a%2==0: print('a Is an even number greater than 0') #Double branch statement nesting a=-10 if a>0: if a%2==0: print('a Is an even number greater than 0') else: print('a Is an odd number greater than 0') else: if a % 2 == 0: print('a Is an even number less than 0') else: print('a Is an odd number less than 0') #Multi branch statement nesting num=3 work_num=0 if num==1: if work_num: print('I have to work overtime on Monday') else: print('No overtime on Monday') elif num==2: if work_num: print('I have to work overtime on Tuesday') else: print('No overtime on Tuesday') elif num==3: if work_num: print('I have to work overtime on Wednesday') else: print('No overtime on Wednesday') elif num==4: if work_num: print('I have to work overtime on Thursday') else: print('No overtime on Thursday') elif num==5: if work_num: print('I have to work overtime on Friday') else: print('No overtime on Friday') elif num==6: if work_num: print('I have to work overtime on Saturday') else: print('No overtime on Saturday') elif num==7: if work_num: print('I have to work overtime on Sunday') else: print('No overtime on Sundays') else: print('Not 1~7 Number of ranges')
Loop branch nesting
Sometimes the one-level loop can not meet our task requirements, such as printing the 99 multiplication table. At this time, we should consider the nesting of loop branch statements and complete the task by controlling two variables through the two-level loop. It should be noted that each time the outer loop is executed, the inner loop will execute all times.
Example:
#for loop nesting for i in range(1,10): for j in range(1,i+1): print('{}*{}={}'.format(j,i,i*j),end='\t') print("") #while loop nesting i=1 while i<10: j=1 while j<i+1: print("{}*{}={}".format(j,i,i*j),end='\t') j+=1 print("") i+=1 #For while loop nesting for i in range(1,10): j=1 while j<i+1: print("{}*{}={}".format(j,i,i*j),end='\t') j+=1 print("")
Select loop branch nesting
Not only statements with the same structure can be nested, but statements with different structures can also be nested, such as
Alternative structures and circular structures can also be nested with each other.
Example:
#If for nesting a=10 if a>0: for i in range(1, a): print(i,end='\t') #If while nesting a=10 if a>0: i=1 while i<=10: print(i,end='\t') i+=1 #If else for nesting a=-10 if a>0: for i in range(1,a): print(i,end='\t') else: for i in range(a,0): print(i,end='\t') #If else while nesting a=-10 if a>0: i=1 while i<=10: print(i,end='\t') i+=1 else: i = 1 while i <= 10: print(i, end='\t') i += 2 #If elif else for nesting a=4 if a==3: for i in range(1,a): print(i,end='\t') elif a==4: for i in range(1,a): print(i,end='\t') else: for i in range(1,a): print(i,end='\t') #If elif else while nesting a=4 if a==3: i=1 while i<a: print(i,end='\t') i+=1 elif a==4: i = 1 while i < a: print(i, end='\t') i += 1 else: i = 1 while i < a: print(i, end='\t') i += 1