Circulation is introduced
1. Do similar things over and over conditionally
2. Loops in Python are divided into while and for
The use of the while loop
-
Format: while condition: loop body
-
The three necessary elements of the while loop
- While the keyword
- The loop condition
- The loop body
-
Four things to think about when constructing a cycle
- The initial state
- The loop condition
- Something to do over and over again
- Cycle control
-
case
Sum1 = 0 while I <= 100: Accumulation and # # o sum1 = sum1 + I sum1 + = # I prepare for the next cycle, since the increase I + = 1 print (' accumulation and 1-100 is % d '% sum1) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- While I <= 10: # what does the loop do if I % 2! # = 0: print (I) to prepare for the next cycle On the I + = 1 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # demand: # 1-100 even accumulation and initial state: I = 1 sum1 = 0 # accumulator # loop condition while I <= 100: if i % 2 == 0: Sum1 + = # I prepare for the next cycle accumulation I + = 1 print (f '1-100 is an even number of accumulation and {sum1}') -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # practice: 1-20 While I <= 20: # do what if I % 2! Print ({mult1}'); print({mult1}');Copy the code