python3 while...else和for...else

'' ' 
The while conditions: 
    loop 
else: loop is executed where after normal bounce 
' '' 
I =. 1
 the while I <. 6 :
     Print (I) 
    I = I +. 1
 the else :
     Print ( " Hello " )
 '' ' 
Print results: 
. 1 
2 
. 3 
. 4 
. 5 
Hello 
'' ' 

' '' satisfied while condition, does not satisfy else not executed else '' ' 
I =. 1
 while I <. 11 :
     Print (I)
     IF I ==. 3 :
        break
    i I +. 1 =
 else :
     Print ( " Hello " )
 '' ' 
Print Results: 
. 1 
2 
. 3 
' '' 


'' ' 
Note: If the loop is exited by a break, while it follows else will not be executed. Only while conditional is false when it will execute the else. 
'' ' 

For I in Range (. 1,. 6 ):
     Print (I) 
    I = I +. 1
 the else :
     Print ( " Hello " )
 ' '' 
Print Results: 
. 1 
2 
. 3 
. 4 
. 5 
Hello 
'' ' 

' 'Summary: while ... else and for ... else is the same usage. '' ' 

LST = [. 1, 2,. 3,. 4,. 5 ]
 for I in LST:
     IF I ==. 6 :
         Print (I, " IF " )
         BREAK 
the else :
     Print (I, " the else " )   # . 5 the else

 

Guess you like

Origin www.cnblogs.com/lilyxiaoyy/p/11859222.html