In the program we've seen so far, there are always a series of statements that Python faithfully executes in their order. What if you want to change the order in which statements flow is executed? For example, you want the program to make some decisions and do different things depending on the situation, such as printing "Good Morning" or "nice evening" based on time.
Like the Java language, Python has three control structures-three control-flow statements--if, for, and while.
1.IFIF is mainly used for condition determination. 1.1 If structure
i = 1x = 1if i > 0: #注意后面的冒号必不可少! x = x+1 #必须输入四个空格的缩进表示隶属关系! Print X
1.2 If-elif-else Structure
<pre name= "code" class= "python" >a = Input ("A:") #这条语句的作用是在屏幕显示 "A:" and waits for the user to assign a value to a. b = Input ("b:") if (a > B): Print (A, "greater than", B) print ("Show 1") Elif (a==b): #elif类似java中的else If, but be aware of the colon at the end! print (A, "equals", b) print ("Show 2") Else: #注意末尾的冒号! Print ( A, "less than", b) print ("Show 3") Print ("Display 4")
Here are three blocks, each belonging to if, Elif, else led. The python detection condition, if the condition of the if is found to be false, then skip the immediately following block, detect the next elif condition, if it is false, then execute the else block. The above structure divides the program into three branches. The program executes only one of the three branches according to the conditions. 1.3 Nested use of IF
A = input ("A:") b = Input ("B:") c = input ("c:") if (a > B): print (A, "greater than", B) if (a>c): #注意这里的的if需要缩进, Because the if is subordinate to the IF (a>b) print (A, "greater than", C) else: print (A, "less than", c) #注意这里的if应该与if (a>c) indent the same
The 2.while gives a while loop termination condition. The following example loops through 1 to 100
A=0while a<100: a+=1 #Python没有i + + print (a) <pre name= "code" class= "Python" >
Else
Print ("Loop End")
The 3.forfor loop requires a pre-set number of cycles (n) and then executes the statement that is subordinate to the for (n) times.
student2=["Wang Nima", "Male", 22,["head big", "neck short", "Butt Round"]]for A in Student2: #循环次数为列表student2中元素的个数. Note at the end of the quote print (a) <span style= "White-space:pre" ></span> #循环结构同样需要缩进
Because iterations (that is, loops) of a range of numbers are very common, there is a built-in scope function that provides the use of--range ()
List (range) #建立一个从0到9的列表 (range) generates <span style= "font-size:10px;" ><span style= "Font-family:verdana, Arial, Helvetica, Sans-serif; line-height:24px; " The >iterator object. The function of list () is converted to list </span>) </span>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
We can use the range () function like this
For a in range (1,101): #对while中的事例进行改造, print a number from 1 to 100 printed (a)
List (range 1,100,3) #生成从1到100间的数, Steps 3[1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52, 55, 58, 61 , 64, 67, 70, 73, 76, 79, 82, 85, 88, 91, 94, 97]
List (range 99,1,-3) #1到100间的数字, reverse arrangement [99, 96, 93, 90, 87, 84, 81, 78, 75, 72, 69, 66, 63, 60, 57, 54, 51, 48, 45, 42, 3 9, 36, 33, 30, 27, 24, 21, 18, 15, 12, 9, 6, 3]
Lightweight loops
[X*x for X in range (Ten)] #生成一个简单的 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
4. Interrupt Loop break--jump out of the loop continue--jump out of the line of this command, continue the next cycle
namelist=["Wang two small", "Wang Xiaoming", "Xiao Hong", "Xiao Gang", "Cold Plum", "Wang Nima", "Li Lei"]for name in NameList: if name== "Wang Nima": print ("The murderer found it!") ") Breakprint (name)
Output is Wang Nima
namelist=["Wang two small", "Wang Xiaoming", "Xiao Hong", "Xiao Gang", "Cold Plum", "Wang Nima", "Li Lei"]for name in NameList: if name== "Wang Nima": print ("The murderer found it!") ") Continueprint (name)
Output as Li Lei
5. Note 1. The indentation in Python
Whitespace is important in python. In fact the gap at the beginning of the line is important. It is called indentation. Whitespace (spaces and tabs) at the beginning of a logical line is used to determine the indentation level of logical lines, which is used to determine the grouping of statements. This means that statements at the same level must have the same indentation. Each set of such statements is called a block. We will see examples of the usefulness of blocks in later chapters. One thing you need to remember is that the wrong indentation throws an error.
2.if format if < condition 1>:
Statement
Elif < conditions 2>:
Statement
Elif < conditions 3>:
Statement
Else
Statement3.while format while< conditions;:StatementElseStatement4.for format for variable in < object collection;:
5.Python while can write else!
Python Beginner Learning manual----Control Flow