# 字典 Dictionary
### 2026資訊之芽Python班
實習講師 吳天弘
---
# 好的變數命名方式
----
舉例:[796 . 外送迷陣](https://tioj.sprout.tw/contests/40/problems/796)
----
不好的變數命名
```python!
a = int(input())
b = int(input())
c = int(input())
d = int(input())
```
...所以abcd分別代表什麼?
----
好的變數命名
```python!
price = int(input())
distance = int(input())
rain = int(input())
VIP = int(input())
```
一目瞭然
----
再來一個例子
| 座號 | 0 | 1 | 2 | 3 | 4|
| :----: |:----:|:----:|:----: | :----: |:----: |
| chinese | 80 | 78 | 65 | 57 |56 |
| english | 89 | 64 | 42 | 95 |87 |
| math | 11 | 45 | 14 | 50 |35|
| science | 77 | 72 | 90 | 38 |66 |
| social | 49 | 52 | 67 | 61 |98 |
----
用二維串列!
```python!
score = [
[80, 78, 65, 57, 56],
[89, 64, 42, 95, 87],
[11, 45, 14, 50, 35],
[77, 72, 90, 38, 66],
[49, 52, 67, 61, 98],
]
```
不過好像不太好
----
### 要是能自訂index就好了...
---
# 字典介紹
----
字典是什麼?
| list | dictionary |
| :----: |:----:|
| index : value | key : value |
| index是從0開始<br>的連續整數 | key可以是數字<br>、字串等 |
| value型態無限制 | value型態無限制 |
----
宣告方法#1
```python!
weight = {} #空字典
weight = {'Alan': 65, 'Ben': 57, 'Charles': 70}
```

----
宣告方法#2
```python!
weight = dict() #空字典
weight = dict([('Alan', 65), ('Ben', 57), ('Charles', 70)])
```
----
宣告方法#3
```python!
keys = ['Alan', 'Ben', 'Charles']
values = [65, 57, 70]
weight = dict(zip(keys, values))
```
---
# 取值
----
```python!
weight = {'Alan': 65, 'Ben': 57, 'Charles': 70}
print(weight['Alan'])
```
好像跟串列一樣耶
----
但是不一定有對應的Key...
```python!
weight = {'Alan': 65, 'Ben': 57, 'Charles': 70}
print(weight['Dave'])
```

----
用if判斷key有沒有在字典裡
```python!
weight = {'Alan': 65, 'Ben': 57, 'Charles': 70}
if 'Dave' in weight:
print(weight['Dave'])
else:
print('Not found')
```
----
用get()函式
```python!
get(key, default value)
```
```python!
weight = {'Alan': 65, 'Ben': 57, 'Charles': 70}
print(weight.get('Alan'))
print(weight.get('Dave','Not found'))
```
----
### 練習一下
[1070 . 查成績](https://tioj.sprout.tw/contests/45/problems/1070)
---
# 新增/修改
----
直接賦值就好ㄌ
```python!
weight = {'Alan': 65, 'Ben': 57, 'Charles': 70}
weight['Dave'] = 60
print(weight['Dave'])
```
----
也可以直接修改
```python!
weight = {'Alan': 65, 'Ben': 57, 'Charles': 70}
weight['Alan'] = 60
print(weight['Alan'])
```
----
還是要注意key是否存在
```python!
weight = {'Alan': 65, 'Ben': 57, 'Charles': 70}
weight['Dave'] += 1
print(weight['Dave'])
```

---
# 刪除
----
清空整個字典
```python!
weight = {'Alan': 65, 'Ben': 57, 'Charles': 70}
weight.clear()
print(weight)
```

----
刪掉其中一項
```python!
weight = {'Alan': 65, 'Ben': 57, 'Charles': 70}
weight.pop('Alan')
print(weight)
```
----
跟串列的pop()比一比
|| list | dictionary |
| :----:| :----: |:----:|
| 括號內放的東西 | index | key |
| 如果括號內<br>沒東西 | 自動刪除<br>最後一項 | 錯誤:( |
----
### 練習一下
[715 . 凌晨4點的修羅道](https://tioj.sprout.tw/contests/45/problems/715)
---
# 遍歷
----
印出所有的key
```python!
weight = {'Alan': 65, 'Ben': 57, 'Charles': 70}
for name in weight:
print(name)
```
----
印出所有的key#2
```python!
weight = {'Alan': 65, 'Ben': 57, 'Charles': 70}
for name in weight.keys():
print(name)
```
----
印出所有的value
```python!
weight = {'Alan': 65, 'Ben': 57, 'Charles': 70}
for weigh in weight.values():
print(weigh)
```
----
印出所有東西
```python!
weight = {'Alan': 65, 'Ben': 57, 'Charles': 70}
for key, value in weight.items():
print(key, value)
```
----
印出所有東西#2
```python!
weight = {'Alan': 65, 'Ben': 57, 'Charles': 70}
for i in weight.items():
print(i) #i是tuple哦
```
###### (不過其實也可以直接print(weight)啦)
----
### 練習一下
[674 . 海芭樂愛的大冒險I改](https://tioj.sprout.tw/contests/45/problems/674)
---
# 好用的函式們
----
len(dict)
取得字典長度
```python!
weight = {'Alan': 65, 'Ben': 57, 'Charles': 70}
print(len(weight))
```
----
dict.popitem()
把最後加入字典的key-value組合刪除
```python!
weight = {'Alan': 65, 'Ben': 57, 'Charles': 70}
weight['Dave'] = 60
weight.popitem()
print(weight)
```
----
dict.update(another_dict)
把在括號裡的字典插入前面那個字典
```python!
weightA = {'Alan': 65, 'Ben': 57}
weightB = {'Charles': 70, 'Dave': 60}
weightA.update(weightB)
print(weightA)
```
---
### 作業
(上課練習)
[1070 . 查成績](https://tioj.sprout.tw/contests/45/problems/1070)
[715 . 凌晨4點的修羅道](https://tioj.sprout.tw/contests/45/problems/715)
[674 . 海芭樂愛的大冒險I改](https://tioj.sprout.tw/contests/45/problems/674)
(回家作業)
[695 . bearrrrrrrro的綽號們](https://tioj.sprout.tw/contests/45/problems/695)
[712 . 海芭樂愛的大冒險III](https://tioj.sprout.tw/contests/45/problems/712)
[696 . Dictionary操作練習](https://tioj.sprout.tw/contests/45/problems/696)
[734 . 認證考 dict](https://tioj.sprout.tw/contests/45/problems/734)
[714 . 海芭樂愛的大冒險V](https://tioj.sprout.tw/contests/45/problems/714)
----
# 下課!
謝謝大家~
{"title":"字典","description":"作者:XXX","contributors":"[{\"id\":\"a37e6bd1-9651-4f48-be88-f507693593af\",\"add\":6074,\"del\":1194,\"latestUpdatedAt\":1773544188443}]"}