python循环语句的使用方法
作者:小5聊基础 时间:2023-06-21 10:20:38
文章介绍内容以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']))
效果如下:
二、循环嵌套
多个循环类型嵌套使用,完成更多的逻辑编码
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']))
效果如下:
三、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
效果如下:
四、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
效果如下:
来源:https://blog.51cto.com/u_15288318/5046865
标签:python,循环,语句,方法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Finished with error:Navicat运行SQL文件报错的解决
2024-01-28 01:40:03
![](https://img.aspxhome.com/file/2023/2/124822_0s.png)
使用Python画出小人发射爱心的代码
2022-08-07 01:52:16
![](https://img.aspxhome.com/file/2023/3/103513_0s.jpg)
慎用UL列表
2009-03-25 20:21:00
![](https://img.aspxhome.com/file/UploadPic/20093/25/01-71s.gif)
Python中的tkinter库简单案例详解
2021-01-20 14:25:16
python使用ProjectQ生成量子算法指令集
2023-03-17 20:26:51
ROW_NUMBER SQL Server 2005的LIMIT功能实现(ROW_NUMBER()排序函数)
2012-06-06 20:11:21
mysql中使用replace替换某字段的部分内容
2024-01-20 14:43:39
![](https://img.aspxhome.com/file/2023/8/70478_0s.png)
RHEL下架设MYSQL集群
2008-12-24 16:05:00
在python中利用GDAL对tif文件进行读写的方法
2022-03-25 08:08:07
Tensorflow之MNIST CNN实现并保存、加载模型
2023-10-16 06:21:33
![](https://img.aspxhome.com/file/2023/7/65877_0s.jpg)
OpenCV半小时掌握基本操作之圆圈检测
2022-09-17 08:07:50
![](https://img.aspxhome.com/file/2023/6/101026_0s.gif)
ASP常见错误详解及解决方案小结 推荐第1/2页
2011-02-24 11:19:00
Go语言线程安全之互斥锁与读写锁
2024-05-09 09:56:18
thinkphp5修改view到根目录实例方法
2024-05-03 15:51:58
![](https://img.aspxhome.com/file/2023/7/129407_0s.png)
Python元组的定义及使用
2021-06-30 01:24:40
使用symfony命令创建项目的方法
2023-11-15 10:07:45
Python利用flask sqlalchemy实现分页效果
2023-10-05 16:36:21
![](https://img.aspxhome.com/file/2023/5/86185_0s.jpg)
在Linux系统上通过uWSGI配置Nginx+Python环境的教程
2023-01-05 22:25:57
mysql中的general_log(查询日志)开启和关闭
2024-01-19 01:49:10
![](https://img.aspxhome.com/file/2023/5/91445_0s.png)
golang 实现tcp server端和client端,并计算RTT时间操作
2023-08-30 14:12:49