用while 循环输出列表里面的元素 list = ['alex','egon','taibai','wahaha'] tag = 0 while tag < len(list): print(list[tag]) # print(tag,list[tag]) 索引,元素 tag += 1
那么while能输出字典里面的元素吗?
可以输出来的,你牛逼的话你就敲吧,最后你会发现,太麻烦了
几行代码解决的事,何必那么大费周折呢?
for i in list:
print(i)
dict = {'alex':'男','egon':'男','娃哈哈':'女'}
for i in dict:
print(i) #循环遍历出的是keys
print(i,dict[i])
ll = [[111,222],[333,444],[555,6666]]
for x,y in ll: # x,y=[111,222]
print(x,y)
1.for + break
li = [1,2,3,4,5]
for i in li:
if i == 3:
break
print(i)
2.for + continue
for i in li:
if i == 3:
continue
print(i)
3.for + else
for i in li:
if i ==3:
continue
print(i)
else:
print('xxxx-----xxxxx')
4.for + range
for i in range(len(li)):
print(i)
5.for+enumerate
for i,v in enumerate(li):
print(i,v)
6.for循环嵌套
li1 = [111111,222222,333333]
li = [1,2,3,4,5]
for i in li:
print('外层循环',i)
for x in li1:
print('内层循环',x)