python分句_Python循环中的分句,继续和其他子句

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子句之类的控制语句。

Image for post
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:
1
2
3
4
5
'''

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 for post
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:
1
2
Outside 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 for post
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:
1
2
Outside 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")'''
1
2
4
5
for loop is done
Outside the for loop
'''
Image for post
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")'''
1
2
3
4
5
for loop is done
Outside 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")'''
1
2
Outside 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")'''
1
2
4
5
for loop is done
Outside 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:
0
1
2
3
4
Element is not less than 5
'''
Image for post
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:
break
else
:
print ("Element is not less than 5")'''
Output:
0
1
2
'''
Image for post
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:
continue
else
:
print ("Element is not less than 5")'''
Output:
0
1
2
3
4
Element is not less than 5
'''
Image for post
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.mzph.cn/news/388127.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

eclipse plugin 菜单

简介&#xff1a; 菜单是各种软件及开发平台会提供的必备功能&#xff0c;Eclipse 也不例外&#xff0c;提供了丰富的菜单&#xff0c;包括主菜单&#xff08;Main Menu&#xff09;&#xff0c;视图 / 编辑器菜单&#xff08;ViewPart/Editor Menu&#xff09;和上下文菜单&am…

[翻译 EF Core in Action 2.0] 查询数据库

Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Core 书籍。原版地址. 是除了官方文档外另一个学习EF Core的不错途径, 书中由浅入深的讲解的EF Core的相关知识。因为没有中文版,所以本人对其进行翻译。 预计每两天…

hdu5692 Snacks dfs序+线段树

题目传送门 题目大意&#xff1a;给出一颗树&#xff0c;根节点是0&#xff0c;有两种操作&#xff0c;一是修改某个节点的value&#xff0c;二是查询&#xff0c;从根节点出发&#xff0c;经过 x 节点的路径的最大值。 思路&#xff1a;用树状数组写发现还是有些麻烦&#xff…

python数据建模数据集_Python中的数据集

python数据建模数据集There are useful Python packages that allow loading publicly available datasets with just a few lines of code. In this post, we will look at 5 packages that give instant access to a range of datasets. For each package, we will look at h…

打开editor的接口讨论

【打开editor的接口讨论】 先来看一下workbench吧&#xff0c;workbench从静态划分应该大致如下&#xff1a; 从结构图我们大致就可以猜测出来&#xff0c;workbench page作为一个IWorkbenchPart&#xff08;无论是eidtor part还是view part&#…

【三角函数】已知直角三角形的斜边长度和一个锐角角度,求另外两条直角边的长度...

如图,已知直角三角形ABC中,∠C90, ∠Aa ,ABc ,求直角边AC、BC的长度. ∵ ∠C90,∠Aa ,ABc ,Cos∠AAC/AB ,Sin∠ABC/AB ,∴ ACAB*Cos∠Ac*Cosa ,BCAB*Sin∠Ac*Sina . 复制代码

网络攻防技术实验五

2018-10-23 实验五 学 号201521450005 中国人民公安大学 Chinese people’ public security university 网络对抗技术 实验报告 实验五 综合渗透 学生姓名 陈军 年级 2015 区队 五 指导教师 高见 信息技术与网络安全学院 2018年10月23日 实验任务总纲 2018—2019 …

usgs地震记录如何下载_用大叶草绘制USGS地震数据

usgs地震记录如何下载One of the many services provided by the US Geological Survey (USGS) is the monitoring and tracking of seismological events worldwide. I recently stumbled upon their earthquake datasets provided at the website below.美国地质调查局(USGS)…

Springboot 项目中 xml文件读取yml 配置文件

2019独角兽企业重金招聘Python工程师标准>>> 在xml文件中读取yml文件即可&#xff0c;代码如下&#xff1a; 现在spring-boot提倡零配置&#xff0c;但是的如果要集成老的spring的项目&#xff0c;涉及到的bean的配置。 <bean id"yamlProperties" clas…

eclipse 插件打包发布

如果想把调试好的插件打包发布&#xff0c;并且在ECLIPSE中可以使用. 1.File-->Export 2.选择 PLug-in Development下 的 Deployable plug-ins and fragments 3.进入 Deployable plug-ins and fragments 页面 4.把底下的 Destubatuib 的选项中选择 Archive file 在这里添入要…

无法获取 vmci 驱动程序版本: 句柄无效

https://jingyan.baidu.com/article/a3a3f811ea5d2a8da2eb8aa1.html 将 vmci0.present "TURE" 改为 “FALSE”; 转载于:https://www.cnblogs.com/limanjihe/p/9868462.html

数据可视化 信息可视化_更好的数据可视化的8个技巧

数据可视化 信息可视化Ggplot is R’s premier data visualization package. Its popularity can likely be attributed to its ease of use — with just a few lines of code you are able to produce great visualizations. This is especially great for beginners who are…

分布式定时任务框架Elastic-Job的使用

为什么80%的码农都做不了架构师&#xff1f;>>> 一、前言 Elastic-Job是一个优秀的分布式作业调度框架。 Elastic-Job是一个分布式调度解决方案&#xff0c;由两个相互独立的子项目Elastic-Job-Lite和Elastic-Job-Cloud组成。 Elastic-Job-Lite定位为轻量级无中心化…

Memcached和Redis

Memcached和Redis作为两种Inmemory的key-value数据库&#xff0c;在设计和思想方面有着很多共通的地方&#xff0c;功能和应用方面在很多场合下(作为分布式缓存服务器使用等) 也很相似&#xff0c;在这里把两者放在一起做一下对比的介绍 基本架构和思想 首先简单介绍一下两者的…

第4章 springboot热部署 4-1 SpringBoot 使用devtools进行热部署

/imooc-springboot-starter/src/main/resources/application.properties #关闭缓存, 即时刷新 #spring.freemarker.cachefalse spring.thymeleaf.cachetrue#热部署生效 spring.devtools.restart.enabledtrue #设置重启的目录,添加那个目录的文件需要restart spring.devtools.r…

border-radius 涨知识的写法

<div idapp></div>复制代码#app{width:100%;height:80px;background:pink;border-radius:75%/20% 20% 0 0;}复制代码仅供自己总结记忆转载于:https://juejin.im/post/5c80afd66fb9a049f81a1217

ibm python db_使用IBM HR Analytics数据集中的示例的Python独立性卡方检验

ibm python dbSuppose you are exploring a dataset and you want to examine if two categorical variables are dependent on each other.假设您正在探索一个数据集&#xff0c;并且想要检查两个分类变量是否相互依赖。 The motivation could be a better understanding of …

Oracle优化检查表

分类检查项目相关文件或结果状态备注日志及文件Oracle Alert 日志bdump/udump下是否存在明显的报警listener相关日志SQL* Net日志参数/参数文件listener.ora/tnsnames.ora操作系统操作系统版本检查操作系统补丁节点名操作系统vmstat状态操作系统I/O状态操作系统进程情况操作系统…

spring分布式事务学习笔记(2)

此文已由作者夏昀授权网易云社区发布。欢迎访问网易云社区&#xff0c;了解更多网易技术产品运营经验。Model类如下&#xff1a;package com.xy.model1 package com.xy.model;2 3 /**4 * Created by helloworld on 2015/1/30.5 */6 public class NameQa {7 private long …

sql 左联接 全联接_通过了解自我联接将您SQL技能提升到一个新的水平

sql 左联接 全联接The last couple of blogs that I have written have been great for beginners ( Data Concepts Without Learning To Code or Developing A Data Scientist’s Mindset). But, I would really like to push myself to create content for other members of …