Content Today
First, if flow control
The definition of an if statement: judge right and wrong things true and false. Premise computer is a computer work like people to have the ability to judge right and wrong things true and false.
### if
Python `` ` IF Condition: Code 1 Code 2 Code 3 ... # code blocks (in the same indent level code, such as code 1, code 2 and code 3 is the same as the indented code, which combine three code It is a block of code, the same code indentation running from top to bottom) `
### if ... else indicate if the establishment of the code will be set up to do, else set up will not do.
`Python IF Condition: Code 1 Code 2 Code 3 ... the else : Code 1 Code 2 Code 3 ... ` ``
### if ... elif ... else represent if conditions do 1 set up, elif condition 2 set up to do, elif condition 3 set up to do, elif ... else to do.
Python `` ` IF Condition 1: Code 1 Code 2 Code 3 ... elif Condition 2: Code 1 Code 2 Code 3 ... elif Condition 3: Code 1 Code 2 Code 3 ... ... the else : Code 1 Code 2 Code 3 ... `` `
## if nesting
Python `` ` # IF nested CLS = ' Human ' Gender = ' FEMALE ' Age = 18 is is_success = False IF CLS == ' Human ' and Gender == ' FEMALE ' and Age> 16 and Age <22 is : Print ( ' began confession ' ) IF is_success: Print ( ' that we go together ... ' ) the else : Print ( ' I tease you play with ' ) the else : Print ( ' good aunt ' ) `` `
Second, the flow control while
Real life to do something like repetitive, repetitive labor workers on the assembly line, knowing that certain conditions be stopped.
while conditions: Code 1 Code 2 Code 3 Code 4 Code 5 ...
## while+break
`` Python ` # break syntax will the while True: Print ( ' 1 ' ) Print ( ' 2 ' ) break Print ( ' 3 ' ) # The above is only demo break usage, practically impossible to write like ours cycle should end depending on the conditions `` ` ` `` Python USER_DB = ' Jason ' pwd_db = ' 123 ' the while True: inp_user = INPUT ( ' username: ' ) inp_pwd= input('password: ') if inp_user == user_db and pwd_db == inp_pwd: print('login successful') break else: print('username or password error') print('退出了while循环') ```
break: an immediate end to this cycle layer (valid only for a while that it belongs)
## while+continue
continue: out of this cycle, the next cycle started directly
Python ` n- = 1 the while n-<. 4 : Print (n-) n- + = 1 # This line will be an infinite loop without ` then becomes a print cycle at the demand, 2,3,4,5,7 , 8,9 , numeral 6 is not printed `` `Python n- =. 1 the while n-<10 : IF n-== 6 : n- +. 1 = # If a comment this line, it will enter an infinite loop Continue Print (n-) n- + = 1 `` ` PS: the Continue can not add the final step in the execution of the code, because the code to add the phrase is meaningless ` `Python` the while True: IF condition 1: code1 code2 code3 ... the Continue # meaningless elif condition 1: code1 code2 code3 ... the Continue # meaningless the else : code1 code2 code3 ... the Continue # meaningless `` `
### while loop nest
while + else
only when the normal end of the while loop in accordance with the conditions will go else the code
- if the initiative is the end of the break, it will not go else
Python `` ` # while else + n-=. 1 while n-<. 3 : IF n-== 2: break # not go else Print (n-) n- + =. 1 else : Print ( ' if else will not break while in else executes the code in ' ) `
Third, the process control for
It does not depend on the value of the index
`` `Python for name in NAME_LIST: Print (name) # contrast while easier # look for loop dictionary what would be info = { ' name ' : ' Jason ' , ' Age ' :. 19 } for Item in info: Print (Item) # get all dictionary Key Print (info [Item]) # for without depending on the fetch index, is a common mode fetch cycle # of cycles for the cyclic object is contained by the value the number of decisions, while the number of cycles is determined by the condition `
### for+break
This layer out of cycle
```python # for+break name_list = ['nick', 'jason', 'tank', 'sean'] for name in name_list: if name == 'jason': break print(name) ```
### for+continue
Out of this cycle, the next cycle into the
```python # for+continue name_list = ['nick', 'jason', 'tank', 'sean'] for name in name_list: if name == 'jason': continue print(name) ```