table of Contents
Process for the control of a loop
Loop: Repeat (according to some law) to do one thing
lt = [1, 2, 3, 4]
ind = 0
while True:
print(lt[ind])
ind += 1
while loop: everything can be recycled
for loop: provide a means, do not rely on the value of the index
dic = {'a': 1, 'b': 2, 'c': 3}
for i in dic:
print(i,dic[i])
'' '
For the variable name (will get a value of each element of the container class, it is not the end of the cycle) in the container-earth element:
Print (variable name)
' '
lt = [1, 2, 3, 4]
for i in lt:
print(i)
dic = {'a': 1, 'b': 2, 'c': 3}
count = 0
for i in dic: # 对于字典,for循环只能拿到key
print(i, dic[i])
print(count)
count += 1
print(list(range(10))) # 循环得到列表
for i in range(50,101): # 顾头不顾尾
print(i)
for i in range(50,101,3): # 顾头不顾尾,2表示步长
print(i)
for + break
for i in range(50,101,3): # 顾头不顾尾,2表示步长
if i == 53:
break # 中断循环
print(i)
for + continue
for i in range(50,101,3): # 顾头不顾尾,2表示步长
if i == 53:
continue # 跳出本次循环,不执行下面的代码
print(i)
for + else (as understood only): for loop is not terminated break on the implementation of the code in the else, or do not perform
for i in range(50,101,3):
if i == 1000:
break
print(i)
else:
print('如果没有被break终止我就打印')
Print loading state "Loading ...."
import time
print('loading',end=' ')
for i in range(6):
print('.',end='')
time.sleep(0.2) # 间隔0.2秒打印一个'.'