前言
-
while 循环 在给定的判断条件为 true 时执行循环体,否则退出循环体。
-
for 循环 重复执行语句
-
嵌套循环 你可以在while循环体中嵌套for循环
-
break 语句 在语句块执行过程中终止循环,并且跳出整个循环
-
continue 语句 在语句块执行过程中终止当前循环,跳出该次循环,执行下一次循环。
-
pass 语句 pass是空语句,是为了保持程序结构的完整性。
一、While
注意使用语句的
while 判断条件(condition):
执行语句(statements)……
list = ['time', 'second','year','month','week']
index = 0
while index < len(list):
print(list[index])
index = index + 1
while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则是用于退出循环,此外"判断条件"还可以是个常值,表示循环必定成立
二、for 循环
for iterating_var in sequence:
statements(s)
下为for循环的2种方式
list = ['time', 'second','year','month','week']
# 第一种最简单的情况
# for a in list:
# print(a)
# 第二种用索引的方法
# for index in range(len(list)):
# print('this is', list[index])