您现在的位置是:网站首页> 编程资料编程资料

python循环语句的使用方法_python_

2023-05-26 347人已围观

简介 python循环语句的使用方法_python_

文章介绍内容以Python 3.x版本为主

一、​for循环语句

程序一般情况下都是按顺序执行代码,在代码执行过程中,会有复杂的语句,这个时候循环语句就发挥作用了

遍历指定对象,可以是数组、字符串、Json等

for value in [5,1,'C','T','O']: print('当前遍历值:%s' % (value)) 语句块...可多行 for value in ['51CTO']: print('当前遍历值:%s' % (value)) 语句块...可多行

代码如下​:

import json # for循环语句 - 数组 print('=====数组遍历=====') for value in [5,1,'C','T','O']: print('当前遍历值:%s\r\n' % (value)) # for循环语句 - 字符串 print('=====字符串遍历=====') for value in '51CTO': print('当前遍历值:%s\r\n' % (value)) # for循环语句 - Json对象 jsonString='[{"day":"7","prize":"奖品2选1,超大鼠标垫/定制冰箱贴"},{"day":"14","prize":"奖品3选2,超大鼠标垫/定制冰箱贴/虎年笔记本"},{"day":"21","prize":"奖品5选3,超大鼠标垫/定制冰箱贴/虎年笔记本/双肩背包/WuKong熊手办"}]'; jsonObject = json.loads(jsonString) print('=====Json遍历=====') for item in jsonObject: print('当前遍历值:连续更文第%s天,可获得奖励:%s\r\n' % (item['day'],item['prize']))

效果如下:

#yyds干货盘点#for循环 - python基础学习系列(14)_json

二、​循环嵌套

多个循环类型嵌套使用,完成更多的逻辑编码

while、for循环嵌套,同时也可以结合if等语句,组成一组多逻辑编码

for 循环值 in 循环对象: print('当前遍历值:%s' % (value)) 语句块...可多行 while 成立条件: print('当前遍历值:%s' % (value)) 语句块...可多行

代码如下​:

day=0; jsonString='[{"day":"7","prize":"奖品2选1,超大鼠标垫/定制冰箱贴"},{"day":"14","prize":"奖品3选2,超大鼠标垫/定制冰箱贴/虎年笔记本"},{"day":"21","prize":"奖品5选3,超大鼠标垫/定制冰箱贴/虎年笔记本/双肩背包/WuKong熊手办"}]'; jsonObject = json.loads(jsonString) # 循环嵌套语句 while day<=21: day+=1 for item in jsonObject: if(day==int(item['day'])): print('连续更文第%s天,可获得奖励:%s\r\n' % (item['day'],item['prize']))

效果如下:

#yyds干货盘点#循环嵌套 - python基础学习系列(15)_嵌套

 

三、​break结束循环

场景:当在一个循环里,想在某个条件完成后结束循环,这个时候就需要用到break

当在while、for循环嵌套,break终止循环,只会跳出当前循环

flag=0 while 成立条件: print('当前遍历值:%s' % (value)) 语句块...可多行 for 循环值 in 循环对象: flag=1 print('当前遍历值:%s' % (value)) 语句块...可多行 break if flag: break

代码如下​:

import json day=0; jsonString='[{"day":"7","prize":"奖品2选1,超大鼠标垫/定制冰箱贴"},{"day":"14","prize":"奖品3选2,超大鼠标垫/定制冰箱贴/虎年笔记本"},{"day":"21","prize":"奖品5选3,超大鼠标垫/定制冰箱贴/虎年笔记本/双肩背包/WuKong熊手办"}]'; jsonObject = json.loads(jsonString) # 循环嵌套语句 flag=0 while day<=21: day+=1 for item in jsonObject: if(day==int(item['day'])): flag=1 print('连续更文第%s天,可获得奖励:%s\r\n' % (item['day'],item['prize'])) break if flag: break

效果如下:

#yyds干货盘点#break结束循环 - python基础学习系列(16)_for循环

四、​continue跳过本次循环

场景:当在一个循环里,想在某个条件完成后结束循环,这个时候就需要用到break

和上面讲到的break不同在于,break是跳出整个循环,continue是跳过本次循环,进入下一个序号循环

while 成立条件: print('当前遍历值:%s' % (value)) 语句块...可多行 for 循环值 in 循环对象: print('当前遍历值:%s' % (value)) 语句块...可多行 if i==0: continue

代码如下​:

import json day=0; jsonString='[{"day":"7","prize":"奖品2选1,超大鼠标垫/定制冰箱贴"},{"day":"14","prize":"奖品3选2,超大鼠标垫/定制冰箱贴/虎年笔记本"},{"day":"21","prize":"奖品5选3,超大鼠标垫/定制冰箱贴/虎年笔记本/双肩背包/WuKong熊手办"}]'; jsonObject = json.loads(jsonString) # 循环嵌套语句 while day<=21: day+=1 for item in jsonObject: if(day==int(item['day'])): if day!=21: continue print('连续更文第%s天,可获得奖励:%s\r\n' % (item['day'],item['prize'])) break

效果如下:

#yyds干货盘点#continue跳过本次循环 - python基础学习系列(17)_python

到此这篇关于python循环语句的使用方法的文章就介绍到这了,更多相关python循环内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

-六神源码网