### wk09_1102_字典
##### Ch06_字典
- 6.1 字典基本操作
建立字典
字典取值
字典維護
- 6.2 字典進階操作
字典進階功能整理
in 功能
keys 及 values方法
items 方法
setdefault 方法
#### 【inclass practice】
###### {範例}
1. 血型個性查詢 \<dictget>
2. 輸入及查詢學生成績 \<in>
3. keys 及 values 顯示世大運獎牌數 \<keyvalue>
4. intems 顯示世大運獎牌數 \<item>
```python
#1
dict_血型個性 ={"A":"沉穩",
"B":"活潑",
"O":"自信",
"AB":"聰明"}
blood = input("key in the blood type ")
if dict_血型個性.get(blood) != None :
print("%5 的個性 : %5"%(blood,dict_血型個性[blood]))
else :
print("不存在此血型", blood)
```
key in the blood type a
不存在此血型 a
```python
print(dict_血型個性.items())
print(type(dict_血型個性.items()))
print(dict_血型個性.keys())
print(type(dict_血型個性.keys()))
print(dict_血型個性.values())
print(type(dict_血型個性.values()))
```
dict_items([('A', '沉穩'), ('B', '活潑'), ('O', '自信'), ('AB', '聰明')])
<class 'dict_items'>
dict_keys(['A', 'B', 'O', 'AB'])
<class 'dict_keys'>
dict_values(['沉穩', '活潑', '自信', '聰明'])
<class 'dict_values'>
```python
#2
dict_學生成績 = {"Ray": 100,
"Alice":85,
"David":79,
"Alan":90}
student = input("key in the student name ")
if dict_學生成績.get(student) != None :
print("%s 的成績 : %d"%(student, dict_學生成績[student]))
else :
print("查無此學生", student)
```
key in the student name Ray
Ray 的成績 : 100
```python
#2.additional
dict_學生成績 = {"Ray": 100,
"Alice":85,
"David":79,
"Alan":90}
student = input("key in the student name ")
if dict_學生成績.get(student) != None :
print("%s 的成績 : %d"%(student, dict_學生成績[student]))
else :
score = int(input("key in score ??"))
dict_學生成績.setdefault(student, score)
print(dict_學生成績)
```
key in the student name Martin
key in score ??99
{'Ray': 100, 'Alice': 85, 'David': 79, 'Alan': 90, 'Martin': 99}
```python
#3
dict_世大運獎牌數 = {"金牌": 51,
"銀牌": 26,
"銅牌": 37}
print("2023世大運")
for name, num in dict_世大運獎牌數.items() :
print("%s 數 : %d 面"%(name, num))
```
2023世大運
金牌 數 : 51 面
銀牌 數 : 26 面
銅牌 數 : 37 面
```python
#4
dict_世大運獎牌數 = {"金牌": 51,
"銀牌": 26,
"銅牌": 37}
print(dict_世大運獎牌數)
```
{'金牌': 51, '銀牌': 26, '銅牌': 37}
```python
dict_世大運獎牌數 = {"金牌": 51,
"銀牌": 26,
"銅牌": 37}
print("2023世大運")
for i in range(len(dict_世大運獎牌數)) :
print("%s 數 : %d 面"%(list(dict_世大運獎牌數.keys())[i], list(dict_世大運獎牌數.values())[i]))
print("-----------------")
```
2023世大運
金牌 數 : 51 面
銀牌 數 : 26 面
銅牌 數 : 37 面
-----------------
HackMD
LYRC
password:Aa28364119
#### 【afterclass practice】
1. 教學影音 : hackMd 教學 16:56標籤 18:26將筆記轉換成書本模式
2. 複習 list、tuple、dictionary