l=['a','b','c']
print(len(l))
i=0
while i<len(l):
print(l[i])
i+=1
l=['a','b','c']
for item in l: #item='a'
print(item)
dic={'x':111,'y':222,'z':333}
for k in dic: #k='x'
print(k,dic[k])
while循环 VS for循环
1.
while循环:称之为条件循环,循环的次数取决于条件何时为False
for循环:称之为...循环,循环的次数取决于数据的包含的元素的个数
2.
for循环专门用来取值,在循环取值方面比while循环要强大,以后但凡
遇到循环取值的场景,就应该用for循环
0 1 2
l=['a','b','c']
for i in range(3):
print(i,l[i])
for+break
names=['egon','kevin','alex','hulaoshi']
for name in names:
if name == 'alex':break
print(name)
for+continue
names=['egon','kevin','alex','hulaoshi']
for name in names:
if name == 'alex':continue
print(name)
for+else
names=['egon','kevin','alex','hulaoshi']
for name in names:
# if name == 'alex':break
print(name)
else:
print('=====>')
for循环嵌套
for i in range(3): #i=2
for j in range(2): #j=1
print(i,j) #2,1
'''
外层循环第一次:i=0
内层循环
0,0
0,1
外层循环第二次:i=1
内层循环
1,0
1,1
外层循环第三次: i=2
内层循环
2,0
2,1
'''
print(' ',end='')
print('bbbbb',end='')