Home  >  Article  >  Backend Development  >  How to implement loop in python

How to implement loop in python

silencement
silencementOriginal
2019-06-12 14:39:4310283browse

How to implement loop in python

The loops in Python mainly include for loops and while loops.

while loop

If the condition is true (true), repeat the same operation, if the condition is not met, jump out of the loop

while   循环条件:
     循环操作

While loop example: Enter Wang Xiaoming’s test scores for 5 courses and calculate the average score

1 i=1                                            # 初始化循环计数器i
2 sum=0                                          # 初始化总成绩变量
3 while i<=5:                                    # 从i为1开始到5,重复执行一共5次接受考试成绩、求和的操作
4         print (&#39;请输入第%d门课程的考试成绩&#39;%i)    # 提示用户输入成绩,其中用到了格式化输出,%d的取值随i的值显示,第1门课程,第2门课程……
5         sum=sum+input()                        # 把用户输入的成绩赋值给sum,最后保存着5次成绩的和
6         i=i+1                                  # 每次循环 i 都自增1,直到大于5跳出循环
7 avg=sum/(i-1)                                  # 当第五次执行完i=i+1时,i为6,跳出循环,计算出sum/(i-1)的值就是平均值,并赋值给avg
8 print (&#39;王晓明5门课程的平均成绩是%d&#39;%avg)          # 格式化输出avg的值,由于用了%d所以计算出的%avg的数值有小数也会省去,接收整数部分

Nested while loop example

After the outer loop meets the conditions, the execution of the code begins Execute the inner loop and wait until all the inner loops are executed. If the outer loop conditions are still met, the outer loop will be executed again, and so on, until the outer loop is jumped out.

Example: Enter the 5 scores of two students respectively, and calculate the average score respectively

1 j=1                                         # 定义外部循环计数器初始值
 2 prompt=&#39;请输入学生姓名&#39;                       # 定义字符串变量,在用户输入时调用此变量可以减少敲汉字的麻烦
 3 while j<=2:                                 # 定义外部循环为执行两次
 4     sum=0                                   # 定义成绩初始值,之所以定义在这里,是为了当第二个学生输入成绩时可以让sum初始化为0,重新接收新学生的成绩和
 5     i=1                                     # 定义内部循环计数器初始值
 6     name = raw_input(prompt)               # 接收用户输入的学生姓名,赋值给name变量
 7     while i<=5:                             # 定义内部函数循环5次,就是接收5门课程的成绩
 8         print (&#39;请输入第%d门的考试成绩: &#39;%i)   #提示用户输入成绩,其中用到了格式化输出,%d的取值随i的值显示,第1门课程,第2门课程……
 9         sum= sum + input()                  # 接收用户输入的成绩,赋值给sum
10         i+=1                                # i变量自增1,i变为2,继续执行循环,直到i等于6时,跳出循环
11     avg=sum/(i-1)                           # 计算第一个学生的平均成绩 sum/(6-1),赋值给avg
12     print name,&#39;的平均成绩是%d\n&#39;%avg         # 输出学生成绩平均值
13     j=j+1                                   # 内部循环执行完毕后,外部循环计数器j自增1,变为2,再进行外部循环
14 print &#39;学生成绩输入完成!&#39;                     # 外部循环结束,提示输入完成!

for loop

Use the for statement to traverse all Elements, such as outputting characters in a string one by one, outputting elements in a list one by one, elements in tuples, elements in sets (pay attention to the order of each element when assigning values), keys in the dictionary...

for letter in &#39;Python&#39;:
  print letter

结果:
P
y
t
h
o
n
fruits=[&#39;西瓜&#39;,&#39;水蜜桃&#39;,&#39;葡萄&#39;]
for fruit in fruits:
    print fruit

结果:
西瓜
水蜜桃
葡萄

Loop control

Loop control statements can change the normal execution order of the loop

Loop control statements

break statement: jump out of this loop (nested Only one level of loop is jumped out of the loop)

continue statement: skip the remaining statements of the current loop body, retest the loop state, and enter the next loop. For example, the number of loops is 5 times in total, and the fourth If continue is encountered for the first time, then the execution will not continue and the fifth loop judgment will be performed directly

The above is the detailed content of How to implement loop in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn