Process control: if the branch while loop for loop

Process Control

Python program execution, must perform according to a certain rule in

 a macro must be top-down (first logical code above a certain ratio of logic execution code below): Sequence structure
 2 . Analyzing conditions encountered need to select how the different execution routes: branch structure
 3. some things need to be repeated constantly to carry out (or when a certain condition is met certain conditions are not satisfied end repeat): loop structure

if branch

Syntax structure
 if Analyzing conditions: 
    same indent code blocks 
elif Analyzing conditions: 
    with indented block 
...
else : with indented block of code if the execution condition is determined by elif will not enter and else if the condition is determined if not by then determine whether by elif not judged by the execution else elif else only judge in all conditions not only through the implementation of elif multiple times (the code level to consider the terms of conditions of the order) appears or does not appear on demand can decide whether there else on demand structure
nested if 
can be indented added with a branched structure is determined if any of the structure and determination conditions depends outer conditional 
if Condition:
     if Condition:
         Pass 
    elif conditions:
         Pass 
    ... 
    the else :
         Pass 
elif conditions: 
     Pass 
...

while loop

Syntax:
 the while condition: 
    loop the code 

when the condition is judged to meet the body of the loop is executed, finished will meet again to determine whether the conditions 
also will continue to meet the body of the loop, so the loop executes the code 
if needed at the end of the cycle need to make some kind of condition does not satisfy 
if desired codes allow circulation loop exit condition may be defined as a global variable to True initial 
modified under certain conditions, the variable is False exit 
or exits the loop directly break 
whlie break is used to exit the loop layer is present 
while the continue to use out of this cycle is directly start the next cycle 

to add knowledge: 
infinite loop: You can not take the initiative to end the cycle, you want to end only through BREAK 
0, "" empty string , [], {}, None can be while keywords and conversion if the conditions that need to False, others are True
 while ... the else ...: the else branch will be executed when the while loop ends normally (not the end of the break)

for loop

for requirements to accomplish a specific cycle does not depend on the index values 
Syntax: 
for variables in the circulation container: 
    loop codes 
for variables in Range (range): 
    loop Code 

# 1.For circulation loop will be the value of the container individually assigned to the variable 
# Note: each time the loop variable assignment results not in use, the next cycle will be covered, can not be retrieved 
# 3. when the value of the container to be recycled is assigned all over again, automatically end the cycle 

# for BREAK + 
# BREAK used to actively end for 
NAME_LIST = [ ' Nick ' , ' Jason ' , ' Tank ' , ' Sean ' ]
 for name in NAME_LIST:
    IF name == ' Jason ' :
         BREAK 
    Print (name) 

# for Continue + 
# Continue out under the current cycle beginning the cycle again 
NAME_LIST = [ ' Nick ' , ' Jason ' , ' Tank ' , ' Sean ' ]
 for name in NAME_LIST :
     IF name == ' Jason ' :
         the Continue 
    Print (name) 

# for the else ... ... 
only for normal end is not the end of the break will execute the else 

#Range () 
# 1. Range (end not included) Rang (. 3) # [. 1, 2,. 3] 
# 2. Range (start is included, the end is not included) rang (1,5) # [1 , 2 ,. 3,. 4] 
# 3. Range (start is included, the end is not included, step) Range (10,. 5, -1) # [10,. 9,. 8,. 7,. 6] 
Range of the python2 to python3 difference 

python2 in 
# the Range is actually a list of 
# xrange is actually python3 in the Range 
# python3 in 
the time range, you need only give you value

 

Guess you like

Origin www.cnblogs.com/george-007/p/11121798.html
Recommended