blog/docs/code/python/loop.md

915 B
Raw Blame History

title date tags categories author
循环 2020-09-6 17:25:10
Python
Python
Anges黎梦

while

以代码来看一下 while 语句的语法 示例代码

a = 0
b = []
while a<10:
    # 当右边的表达式为真时循环执行while语句内的代码块
    a += 1
    b.append(a)
    print(a)
else:
    # 当表达式为假时,执行的代码块
    print("---------------------")

for

以代码来看一下 while 语句的语法 示例代码

# 使用 in 循环
for i in b: # 循环输出列表中的每一个元素i为列表中的元素适用元组、集合、字典->循环的内容是字典中的key b[i]
    print(i)

# 使用 range 循环
for j in range(0, 20, 2):  # 遍历数字序列 start end step len()
    print(j)

continue 关键字

跳出当前循环,继续下一个循环

break 关键字

跳出所有循环