Python学习笔记之Break和Continue用法分析


Posted in Python onAugust 14, 2019

本文实例讲述了Python学习笔记之Break和Continue用法。分享给大家供大家参考,具体如下:

Python 中的Break 和 Continue

  • break:控制何时循环应该结束
  • continue: 跳过循环的一次迭代

Break 和 Continue[示例练习]

用 break 语句写一个循环,用于创建刚好长 140 个字符的字符串 news_ticker。你应该通过添加 headlines 列表中的新闻标题创建新闻提醒,在每个新闻标题之间插入空格。如果有必要的话,从中间截断最后一个新闻标题,使 news_ticker 刚好长 140 个字符

headlines = ["Local Bear Eaten by Man",
       "Legislature Announces New Laws",
       "Peasant Discovers Violence Inherent in System",
       "Cat Rescues Fireman Stuck in Tree",
       "Brave Knight Runs Away",
       "Papperbok Review: Totally Triffic"]
news_ticker = ""
for item in headlines:
  news_ticker += item + " "
  if len(news_ticker) >= 140:
    news_ticker = news_ticker[:140]
    break
print(news_ticker) # Local Bear Eaten by Man Legislature Announces New Laws Peasant Discovers Violence Inherent in System Cat Rescues Fireman Stuck in Tree Brave
print(len(news_ticker)) # 140

运行结果:

Local Bear Eaten by Man Legislature Announces New Laws Peasant Discovers Violence Inherent in System Cat Rescues Fireman Stuck in Tree Brave
140

break与continue的区别:

1、break:终止,跳出,结束循环(可以作用在任何地方)。常与switch分支结构合用。

2、continue:结束本次的循环,进入下一次的循环(只能运用到循环结构中)。

关于Python相关内容感兴趣的读者可查看本站专题:《Python函数使用技巧总结》、《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
跟老齐学Python之Python文档
Oct 10 Python
Python匹配中文的正则表达式
May 11 Python
Python图形绘制操作之正弦曲线实现方法分析
Dec 25 Python
Anaconda2 5.2.0安装使用图文教程
Sep 19 Python
python绘制热力图heatmap
Mar 23 Python
django框架auth模块用法实例详解
Dec 10 Python
pytorch 自定义参数不更新方式
Jan 06 Python
使用Python发现隐藏的wifi
Mar 04 Python
python实现程序重启和系统重启方式
Apr 16 Python
django restframework serializer 增加自定义字段操作
Jul 15 Python
Python tkinter界面实现历史天气查询的示例代码
Aug 23 Python
解决Python3.8运行tornado项目报NotImplementedError错误
Sep 02 Python
Python学习笔记之While循环用法分析
Aug 14 #Python
Python 使用 PyMysql、DBUtils 创建连接池提升性能
Aug 14 #Python
Python学习笔记之For循环用法详解
Aug 14 #Python
Python学习笔记之Zip和Enumerate用法实例分析
Aug 14 #Python
Python使用mongodb保存爬取豆瓣电影的数据过程解析
Aug 14 #Python
使用python写的opencv实时监测和解析二维码和条形码
Aug 14 #Python
用python3 urllib破解有道翻译反爬虫机制详解
Aug 14 #Python
You might like
用PHP将数据导入到Foxmail
2006/10/09 PHP
PHP中static关键字原理的学习研究分析
2011/07/18 PHP
header跳转和include包含问题详解
2012/09/08 PHP
php二维数组排序方法(array_multisort usort)
2013/12/25 PHP
php内核解析:PHP中的哈希表
2014/01/30 PHP
php模拟登陆的实现方法分析
2015/01/09 PHP
PHP连接Nginx服务器并解析Nginx日志的方法
2015/08/16 PHP
Thinkphp页面跳转设置跳转等待时间的操作
2019/10/16 PHP
解决jquery submit()提交表单提示:f[s] is not a function
2013/01/23 Javascript
Javascript获取HTML静态页面参数传递值示例
2013/08/18 Javascript
容易造成JavaScript内存泄露几个方面
2014/09/04 Javascript
原生javascript实现图片弹窗交互效果
2015/01/12 Javascript
javascript 中iframe高度自适应(同域)实例详解
2017/05/16 Javascript
js 计算图片内点个数的示例代码
2019/04/04 Javascript
快速搭建Node.js(Express)用户注册、登录以及授权的方法
2019/05/09 Javascript
微信小程序拼接图片链接无底洞深入探究
2019/09/03 Javascript
JS实现动态无缝轮播
2020/01/11 Javascript
使用Python的Django和layim实现即时通讯的方法
2018/05/25 Python
Python 查找list中的某个元素的所有的下标方法
2018/06/27 Python
python读取word文档,插入mysql数据库的示例代码
2018/11/07 Python
基于Python的PIL库学习详解
2019/05/10 Python
详解Django配置JWT认证方式
2020/05/09 Python
python中lower函数实现方法及用法讲解
2020/12/23 Python
python推导式的使用方法实例
2021/02/28 Python
10个很棒的 CSS3 开发工具 推荐
2011/05/16 HTML / CSS
canvas 绘图时位置偏离的问题解决
2020/09/16 HTML / CSS
日本运动品牌美津浓官方购物网站:MIZUNO SHOP
2016/08/21 全球购物
澳大利亚免息网上购物:Shop Zero
2016/09/17 全球购物
打架检讨书500字
2014/01/29 职场文书
电工工作职责范本
2014/02/22 职场文书
我的大学生活演讲稿
2014/04/25 职场文书
房屋维修协议书范本
2014/09/25 职场文书
医院合作意向书范本
2015/05/08 职场文书
2015年档案管理员工作总结
2015/05/13 职场文书
MySQL如何修改字段类型和字段长度
2022/06/10 MySQL
windows11选中自动复制怎么开启? Win11自动复制所选内容的方法
2022/07/23 数码科技