<!-- 自訂CSS設定 --> <style> .hint { } .comment { font-size:70%; color: rgba( 200, 200, 200, 0.9 ); } .tag { padding: .4em; padding-bottom: .3em; margin: 0; font-size: 95%; font-family: Consolas; background-color: rgba( 200, 200, 200, .3 ); border-top-left-radius: 10px; border-bottom-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; } .block { width:auto; padding: .05em; font-family: Consolas; border-top-left-radius: 10px; border-bottom-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; background: rgba(255, 255, 255, .1); display:block; } </style> <!-- 內建CSS設定 --> <style> /* 單行codeblock */ .markdown-body code, .markdown-body tt{ padding: 0px; padding-top: .2em; padding-bottom: .2em; margin: 0; font-size: 85%; background-color: rgba( 0, 0, 0, .04 ); border-radius: 3px; } </style> # <span class="block"> Lesson 2 </span> --- ## 迴圈 ---- ### for 除了上次提到的while迴圈 for 迴圈在Python更常見 用它來**遍歷**一個**容器** ---- 甚麼是容器? 顧名思義 容器用來裝各種型態的資料 遍歷指將所有容器中的元素走過一遍 接下來式幾種Python中內建的容器 --- ### list 串列 ---- 用中括號`[]`括起來 ```python ['Python', 'Java', 'C++'] ``` 可以放不同型態的資料哦 ```python [8129,'FRC'] # OK ``` ---- 使用 [ ] 來取得**索引值**對應的資料 每個資料都有相應的索引值 跟點名表一樣 **在程式語言中索引值從0開始算起** ``` python l = ['Python', 'Java', 'C++'] print(l[0]) # 'Python' ``` ---- ### 示意圖: ![](https://i.imgur.com/Zcphyqm.png) ---- 如果索引值沒有對應的資料 也就是索引超過了串列的長度 ``` py l = ['Python', 'Java', 'C++'] print(l[3]) # IndexError: list index out of range ``` 會報錯 `IndexError` ---- 也可以在list裡放入另一個list(2維) ``` python l = [['Python', 'Ruby'], ['Java', 'C++']] print(l[0][0]) # 'Python' ``` ---- ### 示意圖: ![](https://i.imgur.com/QK1c8t9.png) --- ### slice 切片 用於取得一段的資料 ---- 先看一個範例 ``` python l = ['Python', 'Java', 'C++'] print(l[0:2]) # ['Python', 'Java'] ``` 中括號裡的0:2稱為**slice** ## <font color="red"> **slice不包含結束位置** </font> ---- 所以`0:2`只會取得 `l[0]`和`l[1]` ---- slice有三個值可以調整 {`start`|起始位置}:{`stop`|結束位置}:{`step`|間隔} ## <font color="red"> **slice不包含結束位置** </font> ---- ```python l = [0,1,2,3,4,5,6] print(l[0:5:2]) # 0 2 4 ``` --- ## str 字串 也就是字元形成的串列 ``` python s = 'Python' print(s[2]) # 't' ``` --- ## tuple 元組 不可變的list ``` python t = ('Python', 'Java', 'C++') print(t[0]) # 'Python' ``` ---- 當你忘記... ``` python t = ('Python', 'Java', 'C++') t[0] = 'Ruby' # TypeError: 'tuple' object does not support item assignment ``` ---- 示意圖: ![](https://i.imgur.com/b00WJeD.png) --- ### dict 字典 字典是由鍵(key)與值(value)構成 `{key: value}` 鍵是個不可變物件(ex.字串 元組) ``` python d = {'FRC' : 8129, 'Python': 'Object'} print(d['FRC']) # 8129 ``` ---- 示意圖: ![](https://i.imgur.com/raGeJZb.png) ---- dict 其實就是 mapping 的意思(對應) dict的鍵值也可以被遍歷 之後會再說明 在C++中, <map.h>有和dict一樣的功能 --- ### set 集合 和list很類似, 但集合內不會包含重複的資料 ---- 可以這樣用: ``` python l = [1, 1, 3, 4, 7, 7] l = set(l) print(l) # {1, 3, 4, 7} ``` ---- 集合是沒有順序的 ```python s = {8,1,2,9} print(s[0]) # TypeError: 'set' object is not subscriptable ``` --- 了解甚麼是容器後 實作看看for迴圈 ```python= # 首先創建一個陣列 languages = ['Python', 'Java', 'C++'] ``` ```python=3 # 用for迴圈遍歷它 for lang in languages: print(lang) # 'Python' # 'Java' # 'C++' ``` --- ### range ---- range(start, stop[, step]) ---- 先來看個例子 ```python= for i in range(5): print(i) ``` 以上的程式會依序輸出0~4 (以0為起始) ---- 再來個例子 ```python start = 1 end = 3 for i in range(start, end): print(i) # 1 # 2 ``` 以1為起始 2結束 **不包括最後一個數字** ---- 也可以指定他的間隔 ```python start = 1 end = 6 separation = 2 for i in range(start, end, separation): print(i) # 1 # 3 # 5 ``` 以1為起始, 每次加2, 當大於等於6時就跳出迴圈 ---- 舊版本的range會生成一個對應大小的串列 現在經過優化之後則會生成一個range物件 ```python= print(range(5)) # range(0, 5) ``` 如果想把它變成一個串列要使用list() ```python=4 print(list(range(5))) # [0,1,2,3,4] ``` ---- <style> @keyframes rainbow { from { color: red; letter-spacing: 0; } to{ color: blue; letter-spacing: 0.5em; } } </style> <b style="display: block; text-align: center; animation: 3s ease alternate rainbow infinite;"><h1>謝謝聆聽</h1></b>
{"metaMigratedAt":"2023-06-16T11:10:35.537Z","metaMigratedFrom":"YAML","title":"Lesson 2","breaks":true,"slideOptions":"{\"transition\":\"fade\",\"parallaxBackgroundImage\":\"https://cdn.discordapp.com/attachments/887196342135451652/889752537513750588/image0.png\",\"parallaxBackgroundSize\":\"2100px 1000px\",\"defaultTiming\":120}","contributors":"[{\"id\":\"35809032-c270-45a6-adab-a7e7a63da25b\",\"add\":5715,\"del\":2454},{\"id\":\"a41c05df-55bf-44dc-9b5f-6e53858b0a77\",\"add\":1446,\"del\":98}]"}
    248 views
   owned this note