python笔记之while循环
生活随笔
收集整理的這篇文章主要介紹了
python笔记之while循环
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
While 循環
特點:提高代碼的復用性
語法:
while 判斷條件: 條件滿足時執行代碼塊實例:
count = 0 while count<10:print(count,end="")count+=1輸出結果:
0123456789While 循環使用 else 語句
當while 后的判斷條件為False 時執行else后的代碼
語法:
while 判斷條件: 代碼塊 else: 代碼塊實例:
count = 0 while count<10:print(count,end="")count+=1 else:print("當不滿足 count<10 時輸出我!")運行結果:
0123456789當不滿足 count<10 時輸出我!While死循環
語法:
while True:print("一直輸出我!")運行結果:
一直輸出我! 一直輸出我! 一直輸出我! 一直輸出我! 一直輸出我!...continue 和 break
continue表示跳出本次循環進入下次循環
break 表示終止整個循環
continue實例
打印1-10之間的奇數。
當1-10之間有偶數時退出本次循環,進入下次循環
運行結果:
1 3 5 7 9break實例
打印1,2,3后終止整個循環
i = 0 while i<10:i+=1if i>3:breakprint(i)運行結果:
1 2 3總結
以上是生活随笔為你收集整理的python笔记之while循环的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python笔记之if练习
- 下一篇: python笔记之for循环