# NKUST ITC 108-1 Python[1]
###### [https://python-slide.macs1207.dev](https://python-slide.macs1207.dev)
###### 2019/10/15
###### tags: `NKUST ITC` `Python 社課`
[TOC]
---
## [Github](https://github.com/macs1207/python-tutorial)
---
## (應該是)今天的進度
<span><li>複習Dict與List</li><!-- .element: class="fragment" data-fragment-index="1" --></span>
<span><li>json</li><!-- .element: class="fragment" data-fragment-index="2" --></span>
<span><li>檔案讀寫</li><!-- .element: class="fragment" data-fragment-index="3" --></span>
<span><li>基本的import用法</li><!-- .element: class="fragment" data-fragment-index="4" --></span>
---
## 複習一下Dict(字典)和List(列表)
----
### List
```python=
a_list = ["這是一個List", "你說你知道?", "好吧"]
print(a_list[2]) # Output: 好吧
```
- 常用方法請參考[上次簡報](/@macs1207/rkP0GZKOS#/8/6)
----
### Dict
```python=
club_name = {
"資研社": "ITC",
"滔滔社": "TAO",
"學生會": "SA"
}
print(club_name["資研社"]) # Output: ITC
```
- 常用方法請參考[上次簡報](/@macs1207/rkP0GZKOS#/10/7)
----
### 內容可以是任何物件?
- 所以Dict裡面可以放List?
- 可以喔
- 那List裡面可以放D...
- 當然可以
----
### Dict裡面包含List
```python=
a_dict = {
"程式語言": ["C", "C++", "Python", "Go", "Rust"],
"水果": ["西瓜", "蘋果"]
}
```
### 怎麼取得內容呢?
<span>
```python=
print(a_dict["程式語言"][2]) # Output: Python
```
<!-- .element: class="fragment" data-fragment-index="1" -->
</span>
----
### 以此類推,List裡面包含Dict...
```python=
movie_list = [
{
"片名": "Joker",
"上映日期": "10/3"
},
{
"片名": "返校",
"上映日期": "9/20"
}
]
print(movie_list[0]["上映日期"]) # Output: 10/3
```
----
### 再更多層一點呢
```python=
movie_list = [
{
"片名": ["Joker", "小丑"],
"上映日期": "10/3"
},
{
"片名": "返校",
"上映日期": "9/20"
}
]
print(movie_list[0]["片名"][1]) # Output: 小丑
```
---
## json? jason?
----
### 結構上和Dict、List可以無痛轉換
- 直接看看維基百科上的[例子](https://zh.wikipedia.org/wiki/JSON#%E4%B8%BE%E4%BE%8B)
:::warning
無痛轉換的前提是,
內容的所有物件都是標準資料格式
(字串、數值或布林等等)
:::
----
### json能用在哪裡呢?
- 看看這個精美的[測速照相資料集](https://od.moi.gov.tw/api/v1/rest/datastore/A01010000C-000674-011)
----
### Serialize(序列化)
- 把資料轉成可取用的格式,這邊指將Dict、List等資料轉為json格式
- 方便資料的交換
{%gist /macs1207/fd84ab012fd94b3d0ce2ed1c84855668 %}
----
### Deserialize(反序列化)
- 將序列化後的資料(json)轉換回Dict、List
{%gist /macs1207/8b9d8440af7106ccd9896f1751d55dd3 %}
----
### 還不知道怎麼編寫json嗎?
- [json編輯器](https://jsoneditoronline.org)
- 右邊編輯,左邊同步顯示對應的json
----
### json in Python
- json.dumps(object)
- 將Python物件轉為json格式字串
- json.loads(string)
- 將json格式字串轉為Python物件
- json.dump(fp)
- 將Python物件轉為json格式字串,並存到fp指定的檔案
- json.load(fp)
- 從fp指定的檔案讀取json格式字串,並轉為Python物件
----
### 如果不太懂load跟loads的差別
- 請參考[Github範例](https://github.com/macs1207/python-tutorial/blob/master/ch2/json_example/json_example.py)
---
## File檔案讀寫
----
### 為何要讀寫檔?
儲存在變數的資料只是暫時放在記憶體中,
想要在程式結束後還保有資料就必須儲存為檔案
----
### 1. 基本操作
```python=
f = open(filename, mode)
f.close()
```
一個open()搭配一個close()使用,</br>有開有關養成好習慣
----
```python=
f = open(filename, mode)
```
- filename是檔名
- 那mode呢?
----
讀寫檔案的模式有以下幾種

----
### r、w、a的意義
- r: read
- w: write
- a: append
- 後綴b: 以二進制讀取檔案
- 如rb、wb+等等
----
### 有沒有更好的方法可以讀寫完檔案後</br>順便關檔呢
```python=
with open(filename, mode) as f:
print(f.read())
```
- 其中的f變數只在with這個區塊(block)中有效
----
### 2. 讀取檔案
{%gist daidaidarwa/c4a3bfaf2798f5edc490bb44e6fee090 %}
----
### 3. readline()一次讀一行
{%gist daidaidarwa/166983351e298581f9761d8d9d9ea370 %}
----
### 4. readlines()以Column(列)為單位儲存在List裡面
{%gist daidaidarwa/21e86cac097933d00a4b95cbbd9b5068 %}
- 科普一下,直行(Row)橫列(Column)
----
### 5. write : 寫檔
{%gist daidaidarwa/953e917a3ffac3cf63f2a9f6111d2281 %}
----
### 6. seek : 移動目前檔頭位置
{%gist daidaidarwa/778313dbfbd194d4255cdb559572e525 %}
----
### 7. tell : 獲取目前處理位置
{%gist daidaidarwa/13f8e5f0246ed5b96cee953ef2313b0d %}
---
## import something
----
### Why import
- 今天我要造一部車
- ~~我要從輪子開始造起~~
- 有人是專門生產輪子,我跟他進貨
```python=
from factory import wheel
# 沒有這種套件,不要這樣寫
```
----
- 另外一個原因是
為了方便維護程式碼,有時需要將功能分開寫。
在Python中,將常用的函式或方法包成Package(套件)或Module(模組)是相當常見的作法。
----
### import module_name
- 以json為範例
```python=
import json
data = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ]
json_string = json.dumps(data)
print(json_string)
```
----
- 也可以寫成以下樣子
```python=
import json as js
data = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ]
json_string = js.dumps(data)
print(json_string)
```
----
### import的用法相當自由(?
- 這邊簡單介紹一些
```python=
from package_name import module_name
```
```python=
from package_name.module_name import function_name
```
----
### Package? Module? Function?
- 原則上,Package一般指目錄
- Module則是指檔案
- Function下下次社課會教到,</br>基本上和數學的函式是同一個概念
- f(x) = y
----
### 示範一下
- 這是[範例程式碼](https://github.com/macs1207/python-tutorial/tree/master/ch2/import_example)中的一個目錄

----
{%gist /macs1207/f6a01a5ff52f746e2c95c030d3f340b9 %}
- Output: Hello, ITC
----
### 也可以import同目錄的module喔

- 像這樣
```python=
# import_example3.py
import just_a_module
just_a_module.intro()
```
- Output: I am a module.
----
### 小陷阱
```python=
from random import randint
num = random.randint(1,5)
# 此時不可使用random模組內的其他方法
seed = random.seed() # 所以這裡會報錯誤訊息
```
----
### 解決方法
```python=
from random import randint,seed
num = random.randint(1,5)
seed = random.seed()
# 此時兩個module都可以使用
```
----
### 懶人用法
```python=
from random import *
num = random.randint(1,5)
seed = random.seed()
status = random.getstate()
# * 為引入所有物件
```
---
## 專案主題
- 主題小組決定
- 不知道可以做什麼?參考[政府開放資料平台](https://data.gov.tw/)
- 各縣市還有自己的開放資料平台,</br>可以自行搜尋
----
- 還是不知道的話...
- 參考我剛學Python時做的[東西](https://macs1207.dev/speed-camera-and-traffic-accidents/opt.html)
- [原始碼底加](https://github.com/macs1207/speed-camera-and-traffic-accidents/blob/master/main.py)
- 也可以跟我們討論喔喔喔
----
### 舉些例子
- 各大聊天軟體的BOT
- TG BOT、Line BOT、Discord BOT
- 股價分析
- 之類的
- ~~搶票小幫手~~
- 需要一直重複的事情
----
## 上完ㄌㄌㄌ,以下開放討論題目及發問
---
## Discord
- [https://discord.gg/hmUeXeH](https://discord.gg/hmUeXeH)

----
## Telegram
- [https://t.me/kuasitc](https://t.me/kuasitc)

----
## 作者
- 社長 [Macs](https://github.com/macs1207)
- 副社長 [呆呆大蛙](https://github.com/daidaidarwa)
- 總務 [C.H](https://github.com/chrisliu430)
----
## 參考資料
- [Python-100-Days](https://github.com/jackfrued/Python-100-Days)
- [Wikipedia](https://zh.wikipedia.org)
- [成大X-Village教材](https://www.facebook.com/pages/category/Education/X-Village-423736361424301)
{"metaMigratedAt":"2023-06-15T00:44:20.048Z","metaMigratedFrom":"Content","title":"NKUST ITC 108-1 Python[1]","breaks":true,"contributors":"[{\"id\":\"0543727d-0e35-443a-a198-84223cf6d534\",\"add\":5706,\"del\":1959},{\"id\":\"478dc5a5-55d6-4469-91c3-56c4ab9c7543\",\"add\":2320,\"del\":429},{\"id\":\"8a9f2da6-22f2-4329-8d35-37824be8c99d\",\"add\":2747,\"del\":1845}]"}