- #1
<font style="color:rgb(71, 101, 130);">for</font>循环">#1<font style="color:rgb(71, 101, 130);">for</font>循环 - #2
<font style="color:rgb(71, 101, 130);">while</font>循环">#2<font style="color:rgb(71, 101, 130);">while</font>循环 - #3
<font style="color:rgb(71, 101, 130);">break</font>和<font style="color:rgb(71, 101, 130);">continue</font>">#3<font style="color:rgb(71, 101, 130);">break</font>和<font style="color:rgb(71, 101, 130);">continue</font>- #3.1
<font style="color:rgb(71, 101, 130);">break</font>">#3.1<font style="color:rgb(71, 101, 130);">break</font> - #3.2
<font style="color:rgb(71, 101, 130);">continue</font>">#3.2<font style="color:rgb(71, 101, 130);">continue</font>- #3.2.1 将
<font style="color:rgb(71, 101, 130);">continue</font>用于<font style="color:rgb(71, 101, 130);">for</font>循环">#3.2.1 将<font style="color:rgb(71, 101, 130);">continue</font>用于<font style="color:rgb(71, 101, 130);">for</font>循环 - #3.2.2 将
<font style="color:rgb(71, 101, 130);">continue</font>用于<font style="color:rgb(71, 101, 130);">while</font>循环">#3.2.2 将<font style="color:rgb(71, 101, 130);">continue</font>用于<font style="color:rgb(71, 101, 130);">while</font>循环
- #3.2.1 将
- #3.1
- #4 总结">#4 总结
- #5 练习题">#5 练习题
- #5.1 使用
<font style="color:rgb(71, 101, 130);">for</font>配合<font style="color:rgb(71, 101, 130);">continue</font>求解 20 以内的奇数的和。">#5.1 使用<font style="color:rgb(71, 101, 130);">for</font>配合<font style="color:rgb(71, 101, 130);">continue</font>求解 20 以内的奇数的和。 - #5.2 使用
<font style="color:rgb(71, 101, 130);">while</font>配合<font style="color:rgb(71, 101, 130);">continue</font>求解 20 以内的奇数的和。">#5.2 使用<font style="color:rgb(71, 101, 130);">while</font>配合<font style="color:rgb(71, 101, 130);">continue</font>求解 20 以内的奇数的和。
- #5.1 使用

- Python 基础教程

#1 <font style="color:rgb(71, 101, 130);">for</font> 循环
<font style="color:rgb(71, 101, 130);">for</font> 循环用于迭代序列(即列表、元组、字典、集合或字符串等)。这与其他编程语言中的 <font style="color:rgb(71, 101, 130);">for</font> 关键字不太相似,而是更像其他面向对象编程语言中的迭代器方法。
<font style="color:rgb(71, 101, 130);">for</font> 循环,我们可以为列表、元组、集合中的每个元素执行一系列的操作。
#1.1 <font style="color:rgb(71, 101, 130);">for</font> 循环语法

#1.2 <font style="color:rgb(71, 101, 130);">for</font> 循环流程图

#1.3 实例
我们一起来看一个数字的实例:依次打印出 1~201
# 打印出1-20numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]for var in numbers:print(var)
2
3
4
5 我们也可以在这个基础上进行进一步的数字计算,比如对 1 到 20 的数字进行求和:
1
# 对 1 到 20 的数字求和numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]count = 0for var in numbers:count += varprint("count = ", count)
2
3
4
5
6
7
8 我们了解了
<font style="color:rgb(71, 101, 130);">for</font> 循环的使用方法之后,我们一起来帮助后羿判断射箭的动作什么时候可以停下来吧?
天空中有 10 个太阳,后羿的任务是要射掉其中的九只,留下一只在天上工作来保障地球的正常运转。也就是说,当天空中太阳数量为 2~10 时,后羿需要保持射箭动作。我们可以将天空中太阳的数量用一个列表sun_in_sky 表示,当太阳数量为2~10时,后羿需要继续射箭!语句如下,我们点击运行:
1
# 天空中的太阳数量在2-10时,保持射箭!sun_in_sky = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]for i in sun_in_sky:if i > 1:print("继续射箭 ")
2
3
4
5
6
#2 <font style="color:rgb(71, 101, 130);">while</font> 循环
我们也可以使用 <font style="color:rgb(71, 101, 130);">while</font> 循环来帮助后羿做判断,我们一起先来了解下 <font style="color:rgb(71, 101, 130);">while</font> 的语法规则:
<font style="color:rgb(71, 101, 130);">while</font> 循环:只要条件满足,就会一直执行一组语句。Python 中,<font style="color:rgb(71, 101, 130);">while</font> 表示的信息是当…时候,也就是说当 <font style="color:rgb(71, 101, 130);">while</font> 循环的条件满足时,会一直执行满足条件的语句。
#2.1 <font style="color:rgb(71, 101, 130);">while</font> 循环语法

<font style="color:rgb(71, 101, 130);">condition</font> 是否为 <font style="color:rgb(71, 101, 130);">True</font>。如果为 <font style="color:rgb(71, 101, 130);">True</font>,则执行循环体语句。
一次循环结束之后,会继续判断 <font style="color:rgb(71, 101, 130);">condition</font> 是否为 <font style="color:rgb(71, 101, 130);">True</font>,如果为 <font style="color:rgb(71, 101, 130);">True</font>,则继续执行循环体语句;上述过程会一直持续下去,直到 <font style="color:rgb(71, 101, 130);">condition</font> 为 <font style="color:rgb(71, 101, 130);">False</font>。
#2.2 <font style="color:rgb(71, 101, 130);">while</font> 循环流程图

#2.3 实例
和<font style="color:rgb(71, 101, 130);">for</font> 循环一样,我们一起来看下使用 <font style="color:rgb(71, 101, 130);">while</font> 循环打印出:1~20。
1
# 打印1-20i = 0while i < 20:i += 1print(i)
2
3
4
5 我们继续来看一个复杂一些的例子,对 20 以内的正整数求和:
1
# 对 20 以内的正整数求和count = 0n = 20i = 1while i <= 20:count += ii += 1print("count = ", count)
2
3
4
5
6
7
8
9 了解了
<font style="color:rgb(71, 101, 130);">while</font> 循环的语法规则之后,我们一起来看下如何用 <font style="color:rgb(71, 101, 130);">while</font> 循环来帮助后羿判断是否需要继续射箭呢?我们来回顾下后羿射箭的条件:当天空中太阳数量为 2~10 时,后羿需要保持射箭动作!
1
# 当天空中太阳数量为2-10时,大羿需要保持射箭动作!sun_in_sky = 10while sun_in_sky > 1:sun_in_sky -= 1print("继续射箭")
2
3
4
5
#3 <font style="color:rgb(71, 101, 130);">break</font> 和 <font style="color:rgb(71, 101, 130);">continue</font>
一般情况下,循环语句会重复执行循环体语句,直到循环条件不满足。但是,在有些情况下,我们需要提前结束本轮循环或者直接结束整个循环。这时候便要用到 <font style="color:rgb(71, 101, 130);">break</font> 和 <font style="color:rgb(71, 101, 130);">continue</font> 语句。
<font style="color:rgb(71, 101, 130);">break</font> 和 <font style="color:rgb(71, 101, 130);">continue</font> 语句的使用,通常是与 <font style="color:rgb(71, 101, 130);">if</font> 条件语句配合的,当满足 <font style="color:rgb(71, 101, 130);">if</font> 条件语句的时候,结束整个循环或者结束本轮循环。
<font style="color:rgb(71, 101, 130);">break</font> 语句:
#3.1 <font style="color:rgb(71, 101, 130);">break</font>
如果想提前结束整个循环,可以使用 <font style="color:rgb(71, 101, 130);">break</font> 语句。
我们一起来看下方例子:对 1 到 20 的数字求和(和为 210),当和大于 100 时,停止求和,退出整个循环。
我们运行下方语句:
#3.1.1 将 <font style="color:rgb(71, 101, 130);">break</font> 用于 <font style="color:rgb(71, 101, 130);">for</font> 循环
1
# 对 1 到 20 的数字求和,当和大于 100 时,便停止求和numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]count = 0for var in numbers:count += varif count >= 100:breakprint("count = ", count)
2
3
4
5
6
7
8
9
10 我们看到打印出的结果是 105,我们提取出其中的
<font style="color:rgb(71, 101, 130);">for</font> 语句:

<font style="color:rgb(71, 101, 130);">break</font> 用于 <font style="color:rgb(71, 101, 130);">while</font> 循环
我们一起再来看下 <font style="color:rgb(71, 101, 130);">while</font> 循环中<font style="color:rgb(71, 101, 130);">break</font> 的使用:
1
# 对 20 以内的正整数求和,当和大于 100 时,停止求和count = 0n = 20i = 1while i <= 20:count += ii += 1if count >= 100:breakprint("count = ", count)
2
3
4
5
6
7
8
9
10
11 我们看到打印的结果还是 105,当 count 值为 105 时,满足条件判断语句
<font style="color:rgb(71, 101, 130);">if count >= 100:</font> 执行break,退出整个循环。
在这里还是提醒大家,注意代码规范哟,注意各个语句的缩进,<font style="color:rgb(71, 101, 130);">if</font> 语句块是包含在 <font style="color:rgb(71, 101, 130);">while</font> 语句块中的,因此<font style="color:rgb(71, 101, 130);">if</font> 语句前面有 4 个空白字符,<font style="color:rgb(71, 101, 130);">break</font> 语句是在 <font style="color:rgb(71, 101, 130);">if</font> 语句下的,因此 <font style="color:rgb(71, 101, 130);">break</font> 语句相较于 <font style="color:rgb(71, 101, 130);">if</font> 语句前方又有四个空白字符,还有冒号及所有代码都是英文状态下的,这些细节千万不要忽略哈。
#3.2 <font style="color:rgb(71, 101, 130);">continue</font>
<font style="color:rgb(71, 101, 130);">break</font> 语句是结束整个循环,如果想提前结束本轮循环,可以使用 <font style="color:rgb(71, 101, 130);">continue</font> 语句。
#3.2.1 将 <font style="color:rgb(71, 101, 130);">continue</font> 用于 <font style="color:rgb(71, 101, 130);">for</font> 循环
我们先一起来看下<font style="color:rgb(71, 101, 130);">continue</font> 在 <font style="color:rgb(71, 101, 130);">for</font> 循环的应用,大家点击运行。
1
# 对 20 以内的偶数求和numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]count = 0for var in numbers:# 奇数,除以2的余数不为0,也就是不能被2整除if var % 2 != 0:# 当数字为奇数时,退出本次循环continuecount += varprint("count = ", count)
2
3
4
5
6
7
8
9
10
11
12 结果实现了对 20 以内偶数的求和。我们来看这部分的
<font style="color:rgb(71, 101, 130);">for</font> 循环语句:

<font style="color:rgb(71, 101, 130);">continue</font> 是退出本次循环,当数字为奇数时,则退出本次循环,不执行循环体内的命令;当数字为偶数时,执行循环体内的命令,完成求和。如果在上述语句中,<font style="color:rgb(71, 101, 130);">continue</font> 的位置是 <font style="color:rgb(71, 101, 130);">break</font>,打印的结果就是0,因为第一次循环 1 就是奇数,不能对 2 整除,直接跳出了整个循环,没有执行 count 变量的求和运算,结果还是count 的原始赋值 0。通过这个例子,希望大家可以分辨清 <font style="color:rgb(71, 101, 130);">continue</font> 和 <font style="color:rgb(71, 101, 130);">break</font> 循环的语法规则 ,能够灵活运用哦!
#3.2.2 将 <font style="color:rgb(71, 101, 130);">continue</font> 用于 <font style="color:rgb(71, 101, 130);">while</font> 循环
我们继续来看下<font style="color:rgb(71, 101, 130);">continue</font> 在于 <font style="color:rgb(71, 101, 130);">while</font> 循环中的应用:
1
# 对 20 以内的偶数求和count = 0n = 20i = 0while i < 20:i += 1if i % 2 != 0:continuecount += iprint("count = ", count)
2
3
4
5
6
7
8
9
10
11 语法规则是一样的哦,当
<font style="color:rgb(71, 101, 130);">i</font>是奇数时,退出本次循环,不执行 <font style="color:rgb(71, 101, 130);">count += i</font>求和语句,当<font style="color:rgb(71, 101, 130);">i</font>为偶数时,则执行求和语句,实现对 20 以内的偶数求和!
#4 总结
我们一起来看下本关总结:
#5 练习题
#5.1 使用 <font style="color:rgb(71, 101, 130);">for</font> 配合 <font style="color:rgb(71, 101, 130);">continue</font> 求解 20 以内的奇数的和。
#5.2 使用 <font style="color:rgb(71, 101, 130);">while</font> 配合 <font style="color:rgb(71, 101, 130);">continue</font> 求解 20 以内的奇数的和。
更新于: 12/30/2021, 2:46:39 AM
