iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 16
0
AI & Data

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

[Day 16] while迴圈

這~while迴圈就如同其他語言一樣

    while 條件運算:
        程式區塊

開始寫程式吧!
https://ithelp.ithome.com.tw/upload/images/20190917/201198068gqVR9bIGn.jpg
https://ithelp.ithome.com.tw/upload/images/20190917/20119806N1IuwVgV1L.jpg

https://ithelp.ithome.com.tw/upload/images/20190917/20119806hjUzlvZkd2.jpg
https://ithelp.ithome.com.tw/upload/images/20190917/201198065SEUueK44v.jpg

https://ithelp.ithome.com.tw/upload/images/20190917/20119806tI3S6CftmM.jpg

巢狀迴圈 一樣來個九九乘法
https://ithelp.ithome.com.tw/upload/images/20190917/20119806MHaYUeUZQu.jpg

強制離開 while迴圈 break

    while 條件運算式A:
        程式區塊1
        if 條件運算B:
            程式區塊2
            break
        程式區塊3

https://ithelp.ithome.com.tw/upload/images/20190917/20119806778zBTxLAC.jpg

while 迴圈條件運算式與可迭代物件

    while var in 可迭代物件:
        程式區塊
    while 可迭代物件:
        程式區塊

https://ithelp.ithome.com.tw/upload/images/20190917/20119806Yaw18HXGAF.jpg

while 無限迴圈與pass

    while True:
        pass

pass指令是什麼事也不做
此指令有時候會用在設計一個迴圈或函數尚未完成時,先放pass,未來再用完整程式碼取代

enumerate物件使用for迴圈

此物件是由索引值與元素值配對出現
使用for迴圈迭代一般物件時,無法得知每個物件元素的索引
但是可以利用enumerate()建立enumerate物件,建立原物件的索引資訊
https://ithelp.ithome.com.tw/upload/images/20190917/20119806dAelwCKJ8U.jpg

下面有兩種寫法,一種是傳統的方式
另一種是利用enumerate的特性來寫
https://ithelp.ithome.com.tw/upload/images/20190917/20119806t6BWVEwSzh.jpg

會用enumerate()的才是真正懂得Python精神的高手(書上說的)


上一篇
[Day 15] 進階的for迴圈
下一篇
[Day 17] Tuple 元組???
系列文
AI的本_資料科學打基礎30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言