版权所有,未经许可,禁止转载
<div class="article-child "><h3>章节</h3><ul><li class="page_item page-item-2744"><a href="https://www.qikegu.com/docs/2744">Python 介绍</a></li> <li class="page_item page-item-2750"><a href="https://www.qikegu.com/docs/2750">Python 开发环境搭建</a></li> <li class="page_item page-item-2752"><a href="https://www.qikegu.com/docs/2752">Python 语法</a></li> <li class="page_item page-item-2754"><a href="https://www.qikegu.com/docs/2754">Python 变量</a></li> <li class="page_item page-item-2757"><a href="https://www.qikegu.com/docs/2757">Python 数值类型</a></li> <li class="page_item page-item-2759"><a href="https://www.qikegu.com/docs/2759">Python 类型转换</a></li> <li class="page_item page-item-2761"><a href="https://www.qikegu.com/docs/2761">Python 字符串(String)</a></li> <li class="page_item page-item-2763"><a href="https://www.qikegu.com/docs/2763">Python 运算符</a></li> <li class="page_item page-item-2766"><a href="https://www.qikegu.com/docs/2766">Python 列表(list)</a></li> <li class="page_item page-item-2769"><a href="https://www.qikegu.com/docs/2769">Python 元组(Tuple)</a></li> <li class="page_item page-item-2774"><a href="https://www.qikegu.com/docs/2774">Python 集合(Set)</a></li> <li class="page_item page-item-2776"><a href="https://www.qikegu.com/docs/2776">Python 字典(Dictionary)</a></li> <li class="page_item page-item-2779"><a href="https://www.qikegu.com/docs/2779">Python If … Else</a></li> <li class="page_item page-item-2781"><a href="https://www.qikegu.com/docs/2781">Python While 循环</a></li> <li class="page_item page-item-2783"><a href="https://www.qikegu.com/docs/2783">Python For 循环</a></li> <li class="page_item page-item-2785"><a href="https://www.qikegu.com/docs/2785">Python 函数</a></li> <li class="page_item page-item-2787"><a href="https://www.qikegu.com/docs/2787">Python Lambda</a></li> <li class="page_item page-item-2790"><a href="https://www.qikegu.com/docs/2790">Python 类与对象</a></li> <li class="page_item page-item-2793"><a href="https://www.qikegu.com/docs/2793">Python 继承</a></li> <li class="page_item page-item-2801"><a href="https://www.qikegu.com/docs/2801">Python 迭代器(Iterator)</a></li> <li class="page_item page-item-2803"><a href="https://www.qikegu.com/docs/2803">Python 模块</a></li> <li class="page_item page-item-2806"><a href="https://www.qikegu.com/docs/2806">Python 日期(Datetime)</a></li> <li class="page_item page-item-2808"><a href="https://www.qikegu.com/docs/2808">Python JSON</a></li> <li class="page_item page-item-2810"><a href="https://www.qikegu.com/docs/2810">Python 正则表达式(RegEx)</a></li> <li class="page_item page-item-2813"><a href="https://www.qikegu.com/docs/2813">Python PIP包管理器</a></li> <li class="page_item page-item-2815"><a href="https://www.qikegu.com/docs/2815">Python 异常处理(Try…Except)</a></li> <li class="page_item page-item-2817"><a href="https://www.qikegu.com/docs/2817">Python 打开文件(File Open)</a></li> <li class="page_item page-item-2819"><a href="https://www.qikegu.com/docs/2819">Python 读文件</a></li> <li class="page_item page-item-2821"><a href="https://www.qikegu.com/docs/2821">Python 写文件</a></li> <li class="page_item page-item-2823"><a href="https://www.qikegu.com/docs/2823">Python 删除文件与文件夹</a></li> </ul></div>
Python For 循环
for
循环用于遍历序列(即列表、元组、字典、集合或字符串)。
使用for
循环,我们可以为列表、元组、集合等序列中的每项执行操作。
示例
打印交通工具列表中的交通工具:
vehicle = ["自行车", "汽车", "高铁"]
for x in vehicle:
print(x)
遍历字符串
示例
遍历字符串“自行车”中的字符:
for x in "自行车":
print(x)
break 语句
break
语句可以跳出循环:
示例
当x为“汽车”时退出循环:
vehicle = ["自行车", "汽车", "高铁"]
for x in vehicle:
print(x)
if x == "汽车":
break
示例
当x为“汽车”时退出循环,但这一次中断出现在打印之前:
vehicle = ["自行车", "汽车", "高铁"]
for x in vehicle:
if x == "汽车":
break
print(x)
continue 语句
continue
语句的作用是停止执行当前循环(不执行continue之后的语句),然后继续执行下一个循环:
示例
不要打印汽车:
vehicle = ["自行车", "汽车", "高铁"]
for x in vehicle:
if x == "汽车":
break
print(x)
range() 函数
要指定循环次数,可以使用range()
函数。
range()
函数返回一个数字序列,默认从0开始,递增1(默认),并以指定的数字结束。
示例
使用range()
函数:
for x in range(6):
print(x)
注意
range(6)
不是0到6的值,而是0到5的值。
range()
函数默认0作为起始值,可以指定起始值:range(2, 6)
,表示取值范围从2到6(但不包括6):
示例
使用起始值参数:
for x in range(2, 6):
print(x)
range()
函数返回的序列默认递增1,可以指定递增值: range(2, 30, 3)
:
示例
序列递增3(默认为1):
for x in range(2, 30, 3):
print(x)
For循环中else语句
for
循环中的else
关键字指定循环完成时要执行的代码块:
示例
打印从0到5的所有数字,并在循环结束时打印一条消息:
for x in range(6):
print(x)
else:
print("完成!")
嵌套循环
嵌套循环是循环中的循环。
示例
打印每个交通工具与速度描述:
vehicle = ["自行车", "汽车", "高铁"]
speed = ["slow", "high", "higher"]
for x in speed:
for y in vehicle:
print(x, y)