Python中for后接else的語法使用
0、背景
今天看到了一個(gè)比較詭異的寫法,for后直接跟了else語句,起初還以為是沒有縮進(jìn)好,查詢后發(fā)現(xiàn)果然有這種語法,特此分享。之前寫過c++和Java,在for后接else還是第一次見。
1、試驗(yàn)
# eg1
import numpy as np
for i in np.arange(5):
print i
else:
print("hello?")
# 0
# 1
# 2
# 3
# 4
# hello?
可以發(fā)現(xiàn),在for正常結(jié)束后,break中的語句進(jìn)行了執(zhí)行。
# eg2
import numpy as np
for i in np.arange(5):
print i
if (i == 3):
break
else:
print("hello?")
# 0
# 1
# 2
# 3
在這個(gè)例子當(dāng)中,i==3的時(shí)候break出了循環(huán),然后else當(dāng)中的語句就沒有執(zhí)行。
2、總結(jié)
總結(jié)起來比較簡單,如果for循環(huán)正常結(jié)束,else中語句執(zhí)行。如果是break的,則不執(zhí)行。
工程性代碼寫的比較少,暫時(shí)沒有想到很好的場景,為了不對(duì)其他同學(xué)造成干擾,這種形式還是少些一點(diǎn)較好。
官方文檔也有解釋:
When the items are exhausted (which is immediately when the sequence is empty), the suite in the else clause, if present, is executed, and the loop terminates.
A break statement executed in the first suite terminates the loop without executing the else clause's suite. 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 was no next item.
https://docs.python.org/2/reference/compound_stmts.html#the-for-statement
補(bǔ)充:python里for和else的搭配
用找質(zhì)數(shù)作為代碼示例
for i in range(2,10):
for n in range(2,i):
if i % n == 0:
#print(i, '=', n, '*', i//n)
break
else:
print('found it %s' %i)
注意:這里的 else 并不屬于 if 代碼塊
根據(jù)官方文檔的解釋理解的意思:當(dāng)?shù)膶?duì)象迭代完并為空時(shí),位于else的語句將會(huì)執(zhí)行,而如果在for循環(huán)里有break時(shí),則會(huì)直接終止循環(huán),并不會(huì)執(zhí)行else里的代碼
寫一個(gè)簡單例子,用來輔助理解
for i in range(10):
if i == 7:
print('found it %s'%i)
break
else:
print('not found')
可以先運(yùn)行代碼,看一下運(yùn)行結(jié)果,然后將代碼塊里的break注釋掉再運(yùn)行一遍,與第一次運(yùn)行的結(jié)果進(jìn)行比較,就會(huì)發(fā)現(xiàn)不同
補(bǔ)充:python中for—else的用法,執(zhí)行完for執(zhí)行else
結(jié)束for循環(huán)后執(zhí)行else
for i in range(5):
print(i)
else:
print("打印else")

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python判斷素?cái)?shù)的3種方法及for-else語句的用法介紹
- Python語法糖for?else循環(huán)語句里的break使用詳解
- python入門學(xué)習(xí)關(guān)于for else的特殊特性講解
- python使用for...else跳出雙層嵌套循環(huán)的方法實(shí)例
- Python for循環(huán)搭配else常見問題解決
- python for和else語句趣談
- Python中在for循環(huán)中嵌套使用if和else語句的技巧
- Python的for和break循環(huán)結(jié)構(gòu)中使用else語句的技巧
- Python中for-else循環(huán)的使用時(shí)機(jī)與注意點(diǎn)
相關(guān)文章
Python實(shí)現(xiàn)http接口自動(dòng)化測(cè)試的示例代碼
這篇文章主要介紹了Python實(shí)現(xiàn)http接口自動(dòng)化測(cè)試的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
從列表到生成器詳解Python可迭代對(duì)象的完全指南
可迭代對(duì)象是?Python?中非常重要的概念,列表、元組、字符串、字典、集合等都是可迭代對(duì)象,本文詳細(xì)介紹了可迭代對(duì)象和迭代器的區(qū)別,以及如何創(chuàng)建它們,希望對(duì)大家有所幫助2026-04-04
解決Python2.7中IDLE啟動(dòng)沒有反應(yīng)的問題
今天小編就為大家分享一篇解決Python2.7中IDLE啟動(dòng)沒有反應(yīng)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11
解決python調(diào)用matlab時(shí)的一些常見問題
這篇文章主要介紹了解決python調(diào)用matlab時(shí)的一些常見問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03
django遷移數(shù)據(jù)庫錯(cuò)誤問題解決
這篇文章主要介紹了django遷移數(shù)據(jù)庫錯(cuò)誤問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07

