python循环中的else_python 循环中else的简单示例
導讀熱詞對python這個高級語言感興趣的小伙伴,下面一起跟隨編程之家 jb51.cc的小編兩巴掌來看看吧!
眾多語言中都有if else這對條件選擇組合,但是在python中還有更多else使用的地方,比如說循環for,或者while都可以和else組合。
下面簡單介紹一下for-else while-else組合
循環組合中的else執行的情況下是循環正常結束(即不是使用break退出)。如下列代碼:
# @param 你所不知道的python 循環中的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
執行結果如下:
# @param 你所不知道的python 循環中的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
相關文章
總結
如果覺得編程之家網站內容還不錯,歡迎將編程之家網站推薦給程序員好友。
本圖文內容來源于網友網絡收集整理提供,作為學習參考使用,版權屬于原作者。
如您喜歡交流學習經驗,點擊鏈接加入交流1群:1065694478(已滿)交流2群:163560250
總結
以上是生活随笔為你收集整理的python循环中的else_python 循环中else的简单示例的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 华为Mate 50 Pro钢化膜曝光:非
- 下一篇: fsmb32.exe是什么进程 作用是什
