for循环
for循环
game_list=['吃','吃饭','吃饭饭','吃饭饭饭','吃烦烦烦放','23','33','77',]
# 0 1 2 3 4 5 6 7
index=0
while index<8:
print(game_list[index])
index+=1
吃
吃饭
吃饭饭
吃饭饭饭
吃烦烦烦放
23
33
77
for i in 列表or字典:
i即是列表的每个元素
for i in game_list:
print(i)
吃
吃饭
吃饭饭
吃饭饭饭
吃烦烦烦放
23
33
77
for+ break
game_list=['吃','吃饭','吃饭饭','吃饭饭饭','吃烦烦烦放','23','33','77',]
# 0 1 2 3 4 5 6 7
for i in game_list:
if i=='吃饭饭':
break
print(i)
吃
吃饭
for+continue
game_list=['吃','吃饭','吃饭饭','吃饭饭饭','吃烦烦烦放','23','33','77',]
# 0 1 2 3 4 5 6 7
for i in game_list:
if i=='吃饭饭':
continue
print(i)
吃
吃饭
吃饭饭饭
吃烦烦烦放
23
33
77
for+else
- for循环没有被break的时候,执行else里的代码
game_list = ['xiaoxiaokan','kaixinxiaoxiaole','tiaoyitiao','chaojimali','hundouluo','zhizhuzhipai','saolei','renzheshengui']
for i in game_list:
if i == 'chaojimali':
continue
print(i)
else:
print('我没有被break掉')
xiaoxiaokan
kaixinxiaoxiaole
tiaoyitiao
hundouluo
zhizhuzhipai
saolei
renzheshengui
我没有被break掉
game_list = ['xiaoxiaokan','kaixinxiaoxiaole','tiaoyitiao','chaojimali','hundouluo','zhizhuzhipai','saolei','renzheshengui']
for i in game_list:
if i == 'chaojimali':
break
print(i)
else:
print('我没有被break掉')
xiaoxiaokan
kaixinxiaoxiaole
tiaoyitiao
list(range(3))
[0, 1, 2]
for循环的嵌套
for i in range(3):
for j in range(2):
print(i,j)
0 0
0 1
1 0
1 1
2 0
2 1
for循环实现loading
import time
print('Loading', end='')
for i in range(6):
print(".", end='')
time.sleep(0.2)
Loading......
print(1)
print(2)
print(3)
1
2
3
print(1,end='')
print(2,end='')
print(3,end='')
123