First, the Flow program control if the judgment
Syntax 1
If condition:
Code 1
Code 2
Code 3 "
...
cls= ' Human '
sex= ' Female '
Age=18
If CLS = = ' human ' and sex = = ' female ' and age > All and Age < 22:
Print (' Start confession ')
Print (' End .... ')
Syntax 2
If condition:
Code 1
Code 2
Code 3
...
Else
Code 1
Code 2
Code 3
...
cls= ' Human '
sex= ' Female '
age=38
If CLS = = ' human ' and sex = = ' female ' and age > All and Age < 22:
Print (' Start confession ')
Else
Print (' Auntie Good ')
Print (' End .... ')
Syntax 3
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
...
............
Else
Code 1
Code 2
Code 3
...
‘‘‘
If: Score >=90, then: excellent
If the score >=80 and <90, then: good
If the scores are >=70 and <80, then: normal
Other situation: very poor
‘‘‘
Score=input (' Your score: ') #score = ' 73 '
Score=int (score) #score =73
If score >= 90:
Print (' excellent ')
Elif Score >= 80:
Print (' good ')
Elif score >= 70:
Print (' normal ')
Else
Print (' very poor ')
user_from_db= ' Egon '
pwd_from_db= ' 123 '
User_from_inp=input (' Username>>>: ')
Pwd_from_inp=input (' Password>>>: ')
if User_from_inp = = user_from_db and Pwd_from_inp = = pwd_from_db:
Print (' Login successfull ')
Else
Print (' User or password error ')
Nesting of If
cls= ' Human '
sex= ' Female '
Age=18
Is_success=false
If CLS = = ' human ' and sex = = ' female ' and age > All and Age < 22:
Print (' Start confession ... ')
If is_success:
Print (' Together ')
Else
Print (' I'm teasing you ... ')
Else
Print (' Auntie Good ')
Print (' End .... ')
Second, the process control while cycle
While syntax, while loop is also called conditional loop
While condition:
Code1
Code2
Code3
....
user_db= ' Egon '
pwd_db= ' 123 '
While True:
Inp_user=input (' Username>>: ')
Inp_pwd=input (' Password>>: ')
if Inp_user = = user_db and Inp_pwd = = pwd_db:
Print (' Login successfull ')
Else
Print (' User or password error ')
2 While+break:break means to terminate the loop of the current layer. Execute other code
While True:
Print (' 1 ')
Print (' 2 ')
Break
Print (' 3 ')
user_db= ' Egon '
pwd_db= ' 123 '
While True:
Inp_user=input (' Username>>: ')
Inp_pwd=input (' Password>>: ')
if Inp_user = = user_db and Inp_pwd = = pwd_db:
Print (' Login successfull ')
Break
Else
Print (' User or password error ')
Print (' other code ')
3 while+continue:continue means to terminate the cycle. Go directly to the next loop
PS: Remember that continue must not be added to the last step of the loop body code executed
N=1
While n <= 10: #
if n = = 8:
n + = 1 #n =9
Continue
Print (n)
N+=1 #n =11
While True:
If condition 1:
Code1
Code2
Code3
Continue #无意义
Elif Condition 1:
Code1
Continue #有意义
Code2
Code3
Elif Condition 1:
Code1
Code2
Code3
Continue #无意义
....
Else
Code1
Code2
Code3
Continue #无意义
While loop nesting
user_db= ' Egon '
pwd_db= ' 123 '
While True:
Inp_user=input (' Username>>: ')
Inp_pwd=input (' Password>>: ')
if Inp_user = = user_db and Inp_pwd = = pwd_db:
Print (' Login successfull ')
While True:
Cmd=input (' Please enter the command you want to execute: ')
if cmd = = ' Q ':
Break
Print ('%s function execution ... '%cmd)
Break
Else
Print (' User or password error ')
Print (' End .... ')
#while +tag
user_db= ' Egon '
pwd_db= ' 123 '
Tag=true
While tag:
Inp_user=input (' Username>>: ')
Inp_pwd=input (' Password>>: ')
if Inp_user = = user_db and Inp_pwd = = pwd_db:
Print (' Login successfull ')
While tag:
Cmd=input (' Please enter the command you want to execute: ')
if cmd = = ' Q ':
Tag=false
Else
Print ('%s function execution ... '%cmd)
Else
Print (' User or password error ')
Print (' End .... ')
#while +else (* *)
N=1
While n < 5:
# if n = = 3:
# break
Print (n)
N+=1
Else
Print (' After the end of the loop, it will be judged: only the while loop executes the code in the else if it is not terminated by a break ')
Third, process control for the loop
names=[' Egon ', ' ASB ', ' WSB ', ' lsb ', ' CSB ']
N=0
While n < len (names):
Print (Names[n])
N+=1
names=[' Egon ', ' ASB ', ' WSB ', ' lsb ', ' CSB ']
info={' name ': ' Egon ', ' age ':, ' sex ': ' Male '}
For k in info: #x = "Age"
Print (K,info[k])
# for item in Names:
# Print (item)
For I in Range (1,10):
Print (i)
For I in range: #默认的起始位置是0
Print (i)
For I in Range (1,10,2): #1 3 5 7 9
Print (i)
names=[' Egon ', ' ASB ', ' WSB ', ' lsb ', ' CSB ']
For I in range (len (names)):
Print (I,names[i])
For I in range (5):
Print (' ========> first layer:%s<========= '%i)
For j in Range (3):
Print (' Second tier:%s '%j)
For+break
names=[' ASB ', ' WSB ', ' Egon ', ' lsb ', ' CSB ']
For N in Names:
if n = = ' Egon ':
Break
Print (n)
For+continue
names=[' ASB ', ' WSB ', ' Egon ', ' lsb ', ' CSB ']
For N in Names:
if n = = ' Egon ':
Continue
Print (n)
#for +else
names=[' ASB ', ' WSB ', ' Egon ', ' lsb ', ' CSB ']
For N in Names:
# if n = = ' Egon ':
# break
Print (n)
Else
Print (' =====> ')
Getting Started with Python II