General programming languages have loop statements, which allow us to execute a statement or statement group multiple times.
The general form of loop statements is as follows:
Python provides for loops and while loops, and of course there are some control loops Statement:
Loop control statement | Description |
break | Executed in statement block Terminate the loop during the process, and jump out of the entire loop |
continue | Terminate the current loop during the execution of the statement block, jump out of the loop, and execute the next loop |
pass | pass is an empty statement to maintain the integrity of the program structure |
1. While loop statement
count = 1 sum = 0 while (count <= 100): sum = sum + count count = count + 1 print(sum)
Output result:
5050
Of course, there are two other important commands in the while statement: continue and break to skip Loop, continue is used to skip the loop, break is used to exit the loop
For example, the above example is to calculate the sum of all integers from 1 to 100. When we need to judge that the sum is greater than 1000, no When adding, you can use break to exit the entire loop
count = 1 sum = 0 while (count <= 100): sum = sum + count if ( sum > 1000): #当 sum 大于 1000 的时候退出循环 break count = count + 1 print(sum)
Output result:
1035
Sometimes, we only want to count the sum of odd numbers between 1 and 100, so that is When count is an even number, that is, an even number, we need to jump out of the current loop and do not want to add. At this time, we can use the statement output by break
count = 1 sum = 0 while (count <= 100): if ( count % 2 == 0): # 双数时跳过输出 count = count + 1 continue sum = sum + count count = count + 1 print(sum)
:
2500
in Python In the while loop, you can also use the else statement. while ... else executes the else statement block when the loop condition is false.
For example:
count = 0 while count < 5: print (count) count = count + 1 else: print (count)
Output result:
0 1 2 3 4 5
2. for loop statement
The for loop can traverse any sequence of items, such as a list or a string
The flow chart is basically as follows:
Basic syntax format:
for iterating_var in sequence: statements(s)
Example:
for letter in 'Hello 两点水': print(letter)
The output results are as follows:
H e l l o 两 点 水
There is a while ... else statement, of course There are also for...else statements. The statements in for are no different from ordinary ones. The statements in else will be executed when the loop is executed normally (that is, for is not interrupted by break), and the same is true for while...else.
for num in range(10,20): # 迭代 10 到 20 之间的数字 for i in range(2,num): # 根据因子迭代 if num%i == 0: # 确定第一个因子 j=num/i # 计算第二个因子 print ('%d 是一个合数' % num) break # 跳出当前循环 else: # 循环的 else 部分 print ('%d 是一个质数' % num)
Output results:
10 是一个合数 11 是一个质数 12 是一个合数 13 是一个质数 14 是一个合数 15 是一个合数 16 是一个合数 17 是一个质数 18 是一个合数 19 是一个质数
3. Nested loops
The Python language allows embedding another loop inside a loop body. The above example also uses nested loops, so no example is given here.
The specific syntax is as follows:
for loop nested syntax
for iterating_var in sequence: for iterating_var in sequence: statements(s) statements(s)
while loop nested syntax
while expression: while expression: statement(s) statement(s)
In addition, you can also embed other loop bodies in the loop body. For example, you can embed a for loop in a while loop. Conversely, you can embed a while loop in a for loop.
Next Section