python循环中的else_python 循环中else的简单示例
導(dǎo)讀熱詞對(duì)python這個(gè)高級(jí)語(yǔ)言感興趣的小伙伴,下面一起跟隨編程之家 jb51.cc的小編兩巴掌來(lái)看看吧!
眾多語(yǔ)言中都有if else這對(duì)條件選擇組合,但是在python中還有更多else使用的地方,比如說(shuō)循環(huán)for,或者while都可以和else組合。
下面簡(jiǎn)單介紹一下for-else while-else組合
循環(huán)組合中的else執(zhí)行的情況下是循環(huán)正常結(jié)束(即不是使用break退出)。如下列代碼:
# @param 你所不知道的python 循環(huán)中的else
# @author 編程之家 jb51.cc|www.www.jb51.cc
numbers = [1,2,3,4,5]
for n in numbers:
if (n > 5):
print('the value is %d '%(n))
break
else:
print('the for loop does not end with break')
i = 0
while(numbers[i] < 5):
print('the index %d value is %d'%(i,numbers[i]))
if (numbers[i] < 0) :
break
i = i + 1
else:
print('the loop does not end with break')
numbers = [1,5]
for n in numbers:
if (n > 5):
print('the value is %d '%(n))
break
else:
print('the for loop does not end with break')
i = 0
while(numbers[i] < 5):
print('the index %d value is %d'%(i,numbers[i]))
if (numbers[i] < 0) :
break
i = i + 1
else:
print('the loop does not end with break')
# End www.jb51.cc
執(zhí)行結(jié)果如下:
# @param 你所不知道的python 循環(huán)中的else
# @author 編程之家 jb51.cc|www.www.jb51.cc
C:\Python27>python.exe for_else.py
the for loop does not end with break
the index 0 value is 1
the index 1 value is 2
the index 2 value is 3
the index 3 value is 4
the loop does not end with break
# End www.jb51.cc
相關(guān)文章
總結(jié)
如果覺(jué)得編程之家網(wǎng)站內(nèi)容還不錯(cuò),歡迎將編程之家網(wǎng)站推薦給程序員好友。
本圖文內(nèi)容來(lái)源于網(wǎng)友網(wǎng)絡(luò)收集整理提供,作為學(xué)習(xí)參考使用,版權(quán)屬于原作者。
如您喜歡交流學(xué)習(xí)經(jīng)驗(yàn),點(diǎn)擊鏈接加入交流1群:1065694478(已滿(mǎn))交流2群:163560250
總結(jié)
以上是生活随笔為你收集整理的python循环中的else_python 循环中else的简单示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 华为Mate 50 Pro钢化膜曝光:非
- 下一篇: python数字类型怎么学,python