for循环
1.RANGE循环
range(start,end):默认取start值,不取end值,如果没有start值,则从0开始
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/usr/bin/python 2 #简单循环 3 for i in range(5): 4 print(i)View Code
2.字符串循环
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #字符串 2 test ='abcdefg' 3 for i in test: 4 print('字符串循环输出',i)View Code
3.列表循环(集合)
列表循环3种方式:
1.输出列表里面的值
2.输出列表索引
3.输出列表索引和值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/usr/bin/python 2 test=[1,2,3,4,5] 3 #输出列表值 4 for i in test: 5 print('列表循环输出',i) 6 #输出列表索引 7 for i,j in enumerate(test): 8 print('列表索引',i) 9 #输出列表序号和值 10 for i in enumerate(test): 11 print('列表序列及值',i)View Code
4.字典循环
字典循环3种方式:
1.输出字典key
2.输出字典value
3.输出字典key-value
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/usr/bin/python 2 test={'name':'zhangyang','age':22} 3 #循环输出key 4 for i in test: 5 print(i) 6 #循环输出value 7 for i in test.values(): 8 print(i) 9 #循环输出key-value 10 方法一: 11 for i in test: 12 print(i,test[i]) 13 方法二: 14 for k,v in test.items(): #会先把字典转为列表,数据量大是不要用 15 print(k,v)View Code
5.嵌套循环及if,continue,break
打印出九九乘法表,遇到值一致的时候就跳过
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/usr/bin/python 2 #if 用作判断 3 #break 终止循环 4 #continue 跳出本次循环不执行后面语句 5 #打印九九乘法表,一致的就不输出 6 for i in range(1,10): 7 for j in range(1,i+1): 8 if i ==j : #判断是否值相等 9 continue #跳出本次循环,不执行i=j的值,进行下一次循环 10 else: 11 print('%s*%s='%(i,j),(i*j),end=' ') #默认结束为换行,此处定义为以空格进行换行 12 print('\n')View Code
标签:输出,python,列表,print,循环,test From: https://www.cnblogs.com/Little-Girl/p/17980132