python分句

Python中的循环 (Loops in Python)

  • for loop

    for循环

  • while loop

    while循环

Let’s learn how to use control statements like break, continue, and else clauses in the for loop and the while loop.

让我们学习如何在for循环和while循环中使用诸如breakcontinueelse子句之类的控制语句。

Topics covered in this story (Image source: Author)
这个故事涉及的主题(图片来源:作者)

声明 (‘for’ Statement)

The for statement is used to iterate over the elements of a sequence (such as a string, tuple, or list) or any other iterable object.

for语句用于遍历序列的元素(例如字符串,元组或列表)或任何其他可迭代对象。

for item in iterable:    suite
  • The iterable is evaluated only once. An iterator is created for the result of that iterable.可迭代仅被评估一次。 为该可迭代的结果创建一个迭代器。
  • The suite is then executed once for each item provided by the iterator, in the order returned by the iterator.然后按迭代器返回的顺序对迭代器提供的每个项目执行一次套件。
  • When the items are exhausted, the loop terminates.物品用完后,循环终止。

例子1.'for'循环 (Example 1. ‘for’ loop)

for i in range(1,6):    print (i)'''Output:12345'''

The for loop may have control statements like break andcontinue, or the else clause.

for循环可能具有控制语句,例如breakcontinueelse子句。

There may be a situation in which you may need to exit a loop completely for a particular condition or you want to skip a part of the loop and start the next execution. The for loop and while loop have control statements break and continue to handle these situations.

在某些情况下,您可能需要针对特定​​条件完全退出循环,或者想要跳过循环的一部分并开始下一次执行。 for循环和while循环的控制语句breakcontinue处理这些情况。

“中断”声明 (‘break’ Statement)

The break statement breaks out of the innermost enclosing for loop.

break语句脱离了最里面的for循环。

A break statement executed in the first suite terminates the loop without executing the else clause’s suite.

在第一个套件中执行的break语句将终止循环,而不执行else子句的套件。

The break statement is used to terminate the execution of the for loop or while loop, and the control goes to the statement after the body of the for loop.

break语句用于终止for循环或while的执行 循环,然后控制转到for循环主体之后的语句。

Image source: Author
图片来源:作者

示例2.在“ for”循环中使用“ break”语句 (Example 2. Using the ‘break’ statement in a ‘for’ loop)

  • The for loop will iterate through the iterable.

    for循环将迭代可迭代对象。

  • If the item in the iterable is 3, it will break the loop and the control will go to the statement after the for loop, i.e., print (“Outside the loop”).

    如果iterable中的项目为3 ,它将中断循环,并且控件将在for循环后转到语句,即print (“Outside the loop”)

  • If the item is not equal to 3, it will print the value, and the for loop will continue until all the items are exhausted.

    如果该项不等于3 ,它将打印该值,并且for循环将继续进行,直到用尽所有项。

for i in [1,2,3,4,5]:    if i==3:        break    print (i)print ("Outside the loop")'''Output:12Outside the loop'''

例子3.在带有“ else”子句的“ for”循环中使用“ break”语句 (Example 3. Using the ‘break’ statement in a ‘for’ loop having an ‘else’ clause)

A break statement executed in the first suite terminates the loop without executing the else clause’s suite.

在第一个套件中执行的break语句将终止循环,而不执行else子句的套件。

Image source: Author
图片来源:作者

Example:

例:

In the for loop, when the condition i==3 is satisfied, it will break the for loop, and the control will go to the statement after the body of the for loop, i.e., print (“Outside the for loop”).

for循环中,当满足条件i==3 ,它将中断for循环,并且控制将进入for循环主体之后的语句,即print (“Outside the for loop”)

The else clause is also skipped.

else子句也被跳过。

for i in [1,2,3,4,5]:    if i==3:        break    print (i)else:    print ("for loop is done")print ("Outside the for loop")'''Output:12Outside the for loop'''

“继续”声明 (‘continue’ Statement)

The continue statement continues with the next iteration of the loop.

continue语句继续执行循环的下一个迭代。

A continue statement executed in the first suite skips the rest of the suite and continues with the next item or with the else clause, if there is no next item.

在第一个套件中执行的continue语句将跳过套件的其余部分,并继续执行下一项或else子句(如果没有下一项)。

例子4.在“ for”循环中使用“ continue”语句 (Example 4. Using the ‘continue’ statement in a ‘for’ loop)

  • The for loop will iterate through the iterable.

    for循环将迭代可迭代对象。

  • If the item in the iterable is 3, it will continue the for loop and won’t execute the rest of the suite, i.e., print (i).

    如果iterable中的项目为3 ,它将继续for循环,并且将不执行套件的其余部分,即print (i)

  • So element 3 will be skipped.

    因此元素3将被跳过。

  • The for loop will continue execution from the next element.

    for循环将从下一个元素继续执行。

  • The else clause is also executed.

    else子句也将执行。

for i in [1,2,3,4,5]:    if i==3:        continue    print (i)else:    print ("for loop is done")print ("Outside the for loop")'''1245for loop is doneOutside the for loop'''
Image source: Author
图片来源:作者

for循环中的else子句 (‘else’ Clause in ‘for’ Loop)

Loop statements may have an else clause. It is executed when the for loop terminates through exhaustion of the iterable — but not when the loop is terminated by a break statement.

循环语句可能包含else子句。 当for循环通过迭代器的穷尽而终止时,将执行该语句,但当该循环由break语句终止时,则不会执行该语句。

例子5.在“ for”循环中使用“ else”子句 (Example 5. Using the ‘else’ clause in a ‘for’ loop)

The else clause is executed when the for loop terminates after the exhaustion of the iterable.

for循环在迭代器用尽后终止时,将执行else子句。

for i in [1,2,3,4,5]:    print (i)else:    print ("for loop is done")print ("Outside the for loop")'''12345for loop is doneOutside the for loop'''

例6.在“ for”循环中使用“ break”语句使用“ else”子句 (Example 6. Using the ‘else’ clause in a ‘for’ loop with the ‘break’ statement)

The else clause is not executed when the for loop is terminated by a break statement.

for循环由break语句终止时, else子句不会执行。

for i in [1,2,3,4,5]:    if i==3:        break    print (i)else:    print ("for loop is done")print ("Outside the for loop")'''12Outside the for loop'''

示例7.在“ for”循环中使用“ continue”语句使用“ else”子句 (Example 7. Using the ‘else’ clause in a ‘for’ loop with the ‘continue’ statement)

The else clause is also executed.

else子句也将执行。

for i in [1,2,3,4,5]:    if i==3:        continue    print (i)else:    print ("for loop is done")print ("Outside the for loop")'''1245for loop is doneOutside the for loop'''

例子8.在“ for”循环中使用“ break”语句和“ else”子句 (Example 8. Using the ‘break’ statement and ‘else’ clause in a ‘for’ loop)

Search the particular element in the list. If it exists, break the loop and return the index of the element; else return “Not found.”

搜索列表中的特定元素。 如果存在,则中断循环并返回该元素的索引;否则,返回0。 否则返回“未找到”。

l1=[1,3,5,7,9]def findindex(x,l1):    for index,item in enumerate(l1):        if item==x:            return index            break    else:        return "Not found"print (findindex(5,l1))#Output:2print (findindex(10,l1))#Output:Not found

“ while”循环 (‘while’ Loop)

The while statement is used for repeated execution as long as an expression is true.

只要表达式为真, while语句将用于重复执行。

while expression:    suiteelse:    suite

This repeatedly tests the expression and, if it is true, executes the first suite. If the expression is false (which it may be the first time it is tested) the suite of the else clause, if present, is executed and the loop terminates.

这将反复测试表达式,如果为true,则执行第一个套件。 如果表达式为假(可能是第一次测试),则执行else子句套件(如果存在)的套件,并终止循环。

例子9.在“ while”循环中使用“ else”子句 (Example 9. Using the ‘else’ clause in a ‘while’ loop)

The while loop is executed until the condition i<5 is False.

执行while循环,直到条件i<5为False。

The else clause is executed after the condition is False.

条件为False后执行else子句。

i=0while i<5:    print (i)    i+=1else:    print ("Element is not less than 5")'''Output:01234Element is not less than 5'''
Image Source:Author
图片来源:作者

“中断”声明 (‘break’ Statement)

A break statement executed in the first suite terminates the loop without executing the else clause’s suite.

在第一个套件中执行的break语句将终止循环,而不执行else子句的套件。

例子10.在“ while”循环中使用“ break”语句和“ else”子句 (Example 10. Using the ‘break’ statement and ‘else’ clause in a ‘while’ loop)

The break statement terminates the loop and the else clause is not executed.

break语句终止循环,而else子句未执行。

i=0while i<5:    print (i)    i+=1    if i==3:        breakelse:    print ("Element is not less than 5")'''Output:012'''
Image Source:Author
图片来源:作者

“继续”声明 (‘continue’ Statement)

A continue statement executed in the first suite skips the rest of the suite and goes back to testing the expression.

在第一个套件中执行的continue语句将跳过套件的其余部分,然后返回测试表达式。

例子11.在“ while”循环中使用“ continue”语句和“ else”子句 (Example 11. Using the ‘continue’ statement and ‘else’ clause in a ‘while’ loop)

The continue statement skips the part of the suite when the condition i==3 is True. The control goes back to the while loop again.

当条件i==3为True时, continue语句将跳过套件的一部分。 控件再次返回while循环。

The else clause is also executed.

else子句也将执行。

i=0while i<5:    print (i)    i+=1    if i==3:        continueelse:    print ("Element is not less than 5")'''Output:01234Element is not less than 5'''
Image Source:Author
图片来源:作者

结论 (Conclusion)

  • Python version used is 3.8.1.

    使用的Python版本是 3.8.1。

  • The break statement will terminate the loop (both for and while). The else clause is not executed.

    break语句将终止循环( forwhile )。 else子句不执行。

  • The continue statement will skip the rest of the suite and continue with the next item or with the else clause, if there is no next item.

    如果没有下一个项目,则continue语句将跳过套件的其余部分,并继续下一个项目或else子句。

  • Theelse clause is executed when the for loop terminates after the exhaustion of the iterable.

    for循环在迭代器用尽后终止时,将执行else子句。

资源(Python文档) (Resources (Python Documentation))

break and continue Statements, and else Clauses on Loops

breakcontinue 语句, else 循环中的子句

break statement in Python

Python中的 break 语句

continue statement in python

在python中的 continue 语句

for statement in python

for python中的语句

翻译自: https://medium.com/better-programming/break-continue-and-else-clauses-on-loops-in-python-b4cdb57d12aa

python分句


http://www.taodudu.cc/news/show-994847.html

相关文章:

  • python数据建模数据集_Python中的数据集
  • usgs地震记录如何下载_用大叶草绘制USGS地震数据
  • 数据可视化 信息可视化_更好的数据可视化的8个技巧
  • sql 左联接 全联接_通过了解自我联接将您SQL技能提升到一个新的水平
  • 科学价值 社交关系 大数据_服务的价值:数据科学和用户体验研究美好生活
  • vs azure web_在Azure中迁移和自动化Chrome Web爬网程序的指南。
  • selenium 解析网页_用Selenium进行网页搜刮
  • hive 导入hdfs数据_将数据加载或导入运行在基于HDFS的数据湖之上的Hive表中的另一种方法。
  • 大数据业务学习笔记_学习业务成为一名出色的数据科学家
  • python 开发api_使用FastAPI和Python快速开发高性能API
  • Power BI:M与DAX以及度量与计算列
  • 梯度下降法优化目标函数_如何通过3个简单的步骤区分梯度下降目标函数
  • seaborn 子图_Seaborn FacetGrid:进一步完善子图
  • 异常检测时间序列_时间序列的无监督异常检测
  • 存款惊人_如何使您的图快速美丽惊人
  • 网络传播动力学_通过简单的规则传播动力
  • 开源软件 安全风险_3开源安全风险及其解决方法
  • 自助分析_为什么自助服务分析真的不是一回事
  • 错误录入 算法_如何使用验证错误率确定算法输出之间的关系
  • pytorch回归_PyTorch:用岭回归检查泰坦尼克号下沉
  • iris数据集 测试集_IRIS数据集的探索性数据分析
  • flink 检查点_Flink检查点和恢复
  • python初学者_初学者使用Python的完整介绍
  • snowflake 数据库_Snowflake数据分析教程
  • 高级Python:定义类时要应用的9种最佳做法
  • 医疗大数据处理流程_我们需要数据来大规模改善医疗流程
  • python对象引用计数器_在Python中借助计数器对象对项目进行计数
  • 数字图像处理 python_5使用Python处理数字的高级操作
  • 软件测试框架课程考试_那考试准备课程值得吗?
  • 为什么在Python代码中需要装饰器

python分句_Python循环中的分句,继续和其他子句相关推荐

  1. python在for循环中不能删除正在循环的列表(问题已解决)

    如,本意是删除列表中的33元素,但是由于两个元素位置相近,在找到第一个33的时候,删除后,元素就自动向前补齐,此时i已经后移,所以在使用remove删除相邻元素的时候会:删除一个元素,漏掉一个元素: ...

  2. python 计算制冷系统循环中的干度

    制冷循环中蒸发器进口干度 # 首先需要安装CoolProp # pip install CoolProp # 制冷循环中蒸发器进口干度 from CoolProp.CoolProp import Pr ...

  3. python中loop函数运用_使用涉及函数的Python在for循环中填充DataFrame

    我想从Mapzen中检索德国地址的地理数据(long/lat).Mapzen提供了一个请求密钥的API.每个请求都返回一个JSON. 以下代码返回一个地址的long/lat和地址名: import p ...

  4. python如何在循环中保存文件_Python中如何将爬取到的数据循环存入到csv文件中?...

    求大神指导 再此感激不尽!!! 我想要把输出的结果存入到csv文件中 我的代码如下:(Python 需要3.5版本的) # coding:utf-8 import requests import js ...

  5. python中文分句_python实现中文文本分句的例子

    对于英文文本分句比较简单,只要根据终结符"."划分就好,中文文本分句看似很简单,但是实现时会遇到很多麻烦,尤其是处理社交媒体数据时,会遇到文本格式不规范等问题. 下面代码针对一段一 ...

  6. python如何在循环中保存文件_python-如何在for循环中更改为另一行文件

    我有一个ifs和elses(不写)长函数,而whatnot包含一个for循环,用于在文件的每一行中查找: def check(low,high): with open('users.txt', 'r+ ...

  7. python 异常回溯_关于python:在循环中捕获异常回溯,然后在脚本末尾引发错误...

    我正在尝试捕获所有异常错误,然后在脚本结尾处使其引发/显示所有回溯... 我有一个主脚本,例如调用我的下标: errors = open('MISC/ERROR(S).txt', 'a') try: ...

  8. python输入程序_Python 程序设计中的输入与输出介绍

    关于Python 编程语言中的输入输出,其实我们在前两几节中已经接触过了.这节我们将具体的介绍一下Python中的输入与输出.什么是输入输出呢? 用户告诉计算机程序所需的信息,就是输入:程序运行结束告 ...

  9. python反余弦函数_Python代码中acos()函数有什么功能呢?

    摘要: 下文讲述Python代码中acos()函数的简介说明,如下所示: acos()函数功能 用于计算出x的反余弦弧度值 acos()函数语法 math.acos(x) ---------参数说明- ...

最新文章

  1. 这7个实用又强大的软件,真的惊艳到我了!
  2. 别找了 这就是适合入门的第一本算法书
  3. Python数据库添加时间
  4. 2. 两数相加(中等)
  5. 黑龙江大学计算机调剂信息,黑龙江大学各学院2019考研调剂信息汇总(4月1日)
  6. v-bind单向绑定与v-model双向绑定
  7. python import 问题
  8. 拿着5家offer的Java,对面试官做了什么?
  9. 解决:关于Git无法提交 index.lock File exists的问题
  10. 数据结构——堆栈的C++实现
  11. oracle10g rac升级到10.2.0.5
  12. wow 私服trinitycore
  13. 博客园文章markdown实现
  14. Windows 下 docker 部署 gitlab ci
  15. ORACLE动态SQL语句
  16. Android之Binder和AIDL原理
  17. 谭浩翔c语言,严谨细致的科技尖兵丨广州市公安局黄埔区分局民警谭浩翔
  18. xy轴坐标图数字表示_求坐标x轴、y轴公式-x轴y轴-数学-潘遮驴同学
  19. 聚美优品广告词和经典分析
  20. layui关闭当前tab页

热门文章

  1. UVA - 12096:The SetStack Computer
  2. Linux下shell脚本指定程序运行时长
  3. 【C language】动态数组的创建和使用
  4. Arcgis 使用ArcToolbox实现数据统计
  5. NLPPython笔记——WordNet
  6. [博客..配置?]博客园美化
  7. 实践作业2:黑盒测试实践(小组作业)每日任务记录1
  8. [阅读笔记]Zhang Y. 3D Information Extraction Based on GPU.2010.
  9. C#调用SQL中的存储过程中有output参数,存储过程执行过程中返回信息
  10. SQL Azure十月份更新