0
点赞
收藏
分享

微信扫一扫

Python【循环】


Python【循环】

while

# while循环
i = 0
while i < 7:
print(i,end=" ")
i += 1

输出:0 1 2 3 4 5 6

while可以配合一个else语句,就是在循环条件不满足时,执行一次:

# while循环
i = 0
while i < 7:
print(i,end=" ")
i += 1
else:
print('i is no long less than 7')

输出:

0 1 2 3 4 5 6 i is no long less than 7

但是如果是break语句终止了循环的话,那就不会执行else语句中的内容了:

# while循环
i = 0
while i < 7:
print(i,end=" ")
i += 1
break
else:
print('i is no long less than 7')

输出:0

for

# for循环
xx = ['A','B','C']
for i in xx:
print(i,end=' ')

输出:A B C

# for循环
for i in range(10,30):
print(i,end=' ')

输出:10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29


举报

相关推荐

0 条评论