iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 15
0
AI & Data

AI的本_資料科學打基礎系列 第 15

[Day 15] 進階的for迴圈

進階???
其實就是多寫些

巢狀迴圈~ 一個迴圈中還有一個迴圈

先來做99乘法吧~
https://ithelp.ithome.com.tw/upload/images/20190916/20119806D0efN54ACJ.jpg

強制離開 break

for迴圈的時候,期待某些條件成立時候可以離開迴圈,可以用break來立即離開
https://ithelp.ithome.com.tw/upload/images/20190916/20119806Cx9bn8lKa9.jpg

https://ithelp.ithome.com.tw/upload/images/20190916/201198065Wsxi6rGBe.jpg

https://ithelp.ithome.com.tw/upload/images/20190916/20119806aYdM8nbRW3.jpg

迴圈暫時停止不往下執行 continue

在設計for迴圈時候,如果期待某些條件發生時可以不往下執行迴圈內容,此時可以使用continue
這個通常和if搭配使用
https://ithelp.ithome.com.tw/upload/images/20190916/201198069oD2zdlE7U.jpg

https://ithelp.ithome.com.tw/upload/images/20190916/201198066vvOVG2Qoy.jpg

for...else

在設計for迴圈時,如果期待所有的if敘述條件是False時,在最後一次迴圈後,可以執行特定程式區塊

for var in 可迭代物件:
   if 條件運算式:
       程式區塊 1
       break
else:
   程式區塊 2

這個語法很適合傳統數學中的驗證質數
https://ithelp.ithome.com.tw/upload/images/20190916/20119806iyE26up6BC.jpg

小小抱怨一下~
在公司沒寫程式,反倒是現在天天在家寫一點
接DELL案子的 BIOS中的Diag到底是做甚麼的呢?


上一篇
[Day 14] Python ~ range()
下一篇
[Day 16] while迴圈
系列文
AI的本_資料科學打基礎30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言