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.pswp.cn/news/388127.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388127.shtml
英文地址,請注明出處:http://en.pswp.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 …