In solving a specific problem, a computer program consists of three scenarios, namely, executing all statements sequentially, selecting the execution part of the statement, and looping through the execution of some statements, which corresponds to the three program execution structure processes in the programming: sequential structure, selection structure and cyclic structure. It turns out that any problem that can be solved with a computer can be solved by applying the three basic structures to write the program. The Python language, of course, also has these three basic constructs.One, select structure (if statement)1. The structure of the IF statement:If < condition 1>: < statement 1> Elif < condition 2>: < statement 2> else: < statement 3> at this time, condition 10% Immediately executes statement 1, Condition 20% executes statement 2 immediately, and condition 1 and condition 2 are not valid when statement 3 is executed. Instance:
x = Int (input ("Please enter your total score:"))ifX >= 90: Print('Excellent')elifX>=80: Print('Liang')elifX >= 70: Print('in')elifX >= 60: Print('qualified')Else: Print('not qualified')It is important to note that when you use the branching structure of multiple elif statements, you should grasp the relationships between multiple conditional statements. As long as one condition is true, the subsequent statement is executed, and the entire if statement is exited after execution. Instance:
x = Int (input ("Please enter your total score:"))ifX >= 90: Print('Excellent')elifX>=80: Print('Liang')elifX >= 60: Print('qualified')elifX >= 70: Print('in')Else: Print('not qualified')At this point the print (' in ') statement in the program is never executed, because when the number of input [70,80] of this interval is first judged to be x>=60, the subsequent statement is executed and the output is qualified, and then the IF statement is exited.2, ternary operatorThe IF statement can be used in a single statement to implement the ternary operator, the basic form:< expression 1> if < condition > Else < expression 2>3. Nesting of If statementsif< conditions: if< conditions;: < statement 1> elif< conditions;: < statement 2>: Els E: < statement 3> when you write conditional statements, you should avoid using nested statements whenever possible. Nested statements are not easy to read, and some possibilities may be overlooked.second, for Loop statement1. The structure of the FOR statement: The For statement in the Python language differs greatly from other advanced programming languages, and other high-level language for statements use loop control variables to control loops. The For statement in Python constructs a loop by looping through a sequence of objects (strings, lists, tuples, and so on), and the condition that the loop ends is that the object is traversed. The For statement is in the following form: for < loop variable > in < Loop object;: < statement 1> else: < statement 2> Else statement 2 Execution occurs only when the loop exits normally (iterating through all the values in the traversed object). Instance:
for num in range (10,20 for i in range (2< Span style= "COLOR: #000000" >,num): if num% i == 0:j = Num/i print ( " %d equals%d*%d "% (num,i,j)) break else : print (" %d is a prime number "% num)
2. Range () functionThe Loop object for a For statement can be a list, a tuple, and a string, and you can generate an iterative value from the range () function to complete the count loop. Range ([Start,] stop [, step]) instance:
for in range (5): print(i) 0 for in range (0,10,3): Print(i) 0369
The For statement uses the range () function to build a loop program based on the number of known cycles, or to use the number generated by range () as an index to access values in lists, tuples, and strings. It is important to note that the range () function returns an object that is a list, but in fact it is not that the range () function does not generate the entire sequence at the time of the call, but rather traverses it once to produce a value to reduce the memory footprint, which is essentially an iterator. Such as:
>>> Range (ten)>>> type (range)<class' Range'>>>> list (range1, 2, 3, 4, 5, 6, 7, 8, 9]
third, while Loop statementThe For statement constructs loops in a way that iterates over the objects, sometimes requiring a program structure like an infinite loop or a loop of an indeterminate number of runs, which requires the use of the while statement1. While statement structure:The basic form of the while statement is as follows: while< condition;: < statement 1> else: < statement 2> Unlike for Loop, the while statement is only in the test strip Will stop when the piece is false. Instance:
Count = 0 while count < 5: print" was less than 5" /c8>) = count + 1else: print" is not less than 5
")
Four, break statement, continue statement, pass statementThe break statement is used in the while and for loops, and the break statement terminates the Loop statement, that is, the loop condition does not have a false condition, or the sequence has not been fully recursive, and it stops executing the loop statement. The continue statement is used in the while and for loops, and the continue statement tells Python to skip the remaining statements of the current loop and then proceed to the next round of loops. The continue statement jumps out of the loop, and break jumps out of the loop. The Python Pass is an empty statement in order to maintain the integrity of the program structure. Pass does not do anything, generally used as a placeholder statement. Instance:
for in range: if i%2 = = 0 :continue print(i If i>=7: breakElse: Print (" end of Loop "1357
Python Process Control