The Python loop (while and for loops)

In order for a computer to calculate the repetitive operations thousands of times, we need to loop.

Python while statement in general form:

. 1  the while judgment conditions:
 2      loop

Gif demo

If the condition is true, then the body of the loop is executed, the loop again after the implementation, re-determine the conditions, if the condition is false, the loop is not executed, the loop terminates.

Note: pay attention when using a colon and indentation, in addition, there is no do..while loop in Python.

Example: Add 1 + 2 + 3 + 4 + ... + 100

1  # ! / Usr / bin / the env to python3 
2  # - * - Coding: UTF-. 8 - * - 
. 3  # from 1 to the 100 
. 4 A = 1
 . 5 B = 2
 . 6  the while B <101 :
 . 7      A = B + A
 . 8      = B +. 1 B
 . 9  Print (A)

The result: 5050

  • Endless loop

Infinite loop in real time to client requests very useful on the server.

We can never achieve infinite loop to false by the conditional expression, examples are as follows:

Quit using: Ctrl + C

1  the while True:
 2      the above mentioned id = the INPUT ( " Enter the user name " )
 3      Print ( " user input your name: " , the above mentioned id)
  • while recycling else statement

else while the latter refers to the effect, when the while loop normally been executed, are suspended with no break, then it will perform the back else

1  # and other languages else generally with different and if, while in Python there is a ... else statement, else behind while refers to the role, while the normal cycle when the execution finished, intermediate not break suspended, it would else statement execution back 
2 COUNT = 0
 . 3  the while COUNT <=. 5 :
 . 4      COUNT + =. 1
 . 5      Print ( " loop " , COUNT)
 . 6  
. 7  else :
 . 8      Print ( " loop done for normal execution " )
 . 9  Print ( " - OUT of the while Loop ------ ---- " )
 10  outputs
 . 11 Loop. 1
 12 is Loop 2
13 is Loop. 3
 14 Loop. 4
 15 Loop. 5
 16 Loop. 6
 . 17  cycles executed normally done for
 18 is ----- OUT of the while Loop ------
 . 19  
20 is  # if it performed during the break, will not be executed else it statement 
21 is COUNT = 0
 22 is  the while COUNT <=. 5 :
 23 is      COUNT. 1 = +
 24      IF COUNT ==. 3: BREAK 
25      Print ( " loop " , COUNT)
 26 is  
27  the else :
 28      Print ( " normal cycle performed Wanla")
29 print("-----out of while loop ------")
30 输出
31 
32 Loop 1
  • Logogram

The syntax is similar to the if statement, the while loop if you only one statement, you can write the statement and while on the same line, as follows:

1 Flag = 1
 2   
3  the while (Flag): Print ( ' Welcome ' ) 
4 Print ( " Good BYE! " )

 

for loop

Python for loop can iterate any sequence of items, such as a list or a string.

The for loop format, there may be no else part

1 for <variable> in <sequence>:
2     <statements>
3 else:
4     <statements>

Example: calculated from the added 0 and 100

. 1 SUM = 0
 2  for X in range (101): # range range (101) is provided in [0,100] 
. 3      SUM = SUM + X
 . 4  Print (SUM)

 

breaker and continue

 

The following example of the output 10, and then outputs END

. 1 n-=. 1
 2  the while n-<= 100 :
 . 3      IF n-> 10: # when n = 11, the condition is satisfied, execution break statement 
. 4          break  # break statement ends the current cycle 
. 5      Print (n-)
 . 6      n-= n-+. 1
 . 7  Print ( ' the END ' )

The following example prints 1,3,5,7,9

. 1 n = 0
 2  the while n <10 :
 . 3      n = n +. 1
 . 4      IF n% 2 == 0: # If n is even, execute continue statement 
. 5          continue  # continue statement will proceed directly to the next cycle, the subsequent print () statement does not execute 
6      Print (the n-)

 

Guess you like

Origin www.cnblogs.com/zhangyanlong/p/11307186.html