基本语法之for循环
for循环
循环:重复干一件事,循环容器数据类型
lis = ['gebilaowang', 'dapao', 'yongjiushuaige','dsb']
print(len(lis))
count = 0
while True:
if count == len(lis):
break
print(lis[count])
count += 1
'''
while:
1. 会进入死循环(不可控),尽量少使用while循环
2. 世间万物都可以作为循环的对象
for:
1. 不会进入死循环(可控),以后尽量使用for循环
2. 只对容器类数据类型+字符串循环(可迭代对象)
'''
lis = ['gebilaowang', 'dapao', 'yongjiushuaige','dsb']
for i in lis: # i='gebilaowang' # i='dapao'
print(i) # i='gebilaowang' # i='dapao'
print(1)
for i in 'nick':
print(i)
print(list(range(3))) # 0,1,2
print(list(range(10))) # 0,1,2..9
print(list(range(2,5))) # 2,3,4 # 顾头不顾尾
print(list(range(3,6))) # 3,4,5 # 顾头不顾尾
print(list(range(3,7,3))) # 3,6 # 顾头不顾尾
print(list(range(1,10,2))) # 1,3,5,7,9 # 顾头不顾尾
for + break的语法
for i in range(1,11): # 1,2,3,4,5
print(i)
if i == 5:
break #跳出循环
for + continue的语法
for i in range(1,11): # 1,2,3,4,5,6,7,8,9,10
if i == 5:
continue # 跳出本次循环
print(i)
for + else的语法
for i in range(1,11): # 1,2,3,4,6,7,8,9,10
if i == 5:
break #跳出本次循环
print(i)
else: # 没有被break会触发
print('hello,你居然运行到了else')
eg:
import time
print('Loading',end='')
for i in range(6):
print('.',end='') # ......
time.sleep(0.5)