亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

loop statement

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:

0917f20fea2f3027c12bd036eb7ad4a.png

Python provides for loops and while loops, and of course there are some control loops Statement:

Loop control statementDescription
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
passpass 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 的時候退出循環(huán)
        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):  # 雙數(shù)時跳過輸出
        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:

b553560177ec037fa1db4fbef038d7f.png

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 之間的數(shù)字
   for i in range(2,num): # 根據(jù)因子迭代
      if num%i == 0:      # 確定第一個因子
         j=num/i          # 計算第二個因子
         print ('%d 是一個合數(shù)' % num)
         break            # 跳出當前循環(huán)
   else:                  # 循環(huán)的 else 部分
      print ('%d 是一個質(zhì)數(shù)' % num)

Output results:

10 是一個合數(shù)
11 是一個質(zhì)數(shù)
12 是一個合數(shù)
13 是一個質(zhì)數(shù)
14 是一個合數(shù)
15 是一個合數(shù)
16 是一個合數(shù)
17 是一個質(zhì)數(shù)
18 是一個合數(shù)
19 是一個質(zhì)數(shù)

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.

Continuing Learning
||
submitReset Code