python分句
Python中的循環 (Loops in Python)
for
loopfor
循環while
loopwhile
循環
Let’s learn how to use control statements like break
, continue
, and else
clauses in the for
loop and the while
loop.
讓我們學習如何在for
循環和while
循環中使用諸如break
, continue
和else
子句之類的控制語句。

聲明 (‘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
循環可能具有控制語句,例如break
和continue
或else
子句。
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
循環的控制語句break
并continue
處理這些情況。
“中斷”聲明 (‘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
循環主體之后的語句。

示例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 thefor
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 thefor
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
子句的套件。

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 thefor
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
'''

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
'''

“中斷”聲明 (‘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
'''

“繼續”聲明 (‘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
'''

結論 (Conclusion)
Python version used is 3.8.1.
使用的Python版本是 3.8.1。
The
break
statement will terminate the loop (bothfor
andwhile
). Theelse
clause is not executed.break
語句將終止循環(for
和while
)。else
子句不執行。The
continue
statement will skip the rest of the suite and continue with the next item or with theelse
clause, if there is no next item.如果沒有下一個項目,則
continue
語句將跳過套件的其余部分,并繼續下一個項目或else
子句。The
else
clause is executed when thefor
loop terminates after the exhaustion of the iterable.當
for
循環在迭代器用盡后終止時,將執行else
子句。
資源(Python文檔) (Resources (Python Documentation))
break
and continue
Statements, and else
Clauses on Loops
break
并 continue
語句, 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,一經查實,立即刪除!