# DAY 06 - 遞迴練習 & 字典
###### tags: `python教學` `函式` `副程式`
### 遞迴練習
#### 階乘
```python=
def Fac(n):
if n > 1:
return n * Fac(n-1)
else:
return 1
print(Fac(5))
```
### 字典
#### 設定一個新的字典時,要用大括號 { }
```python=
car = {"color":"black" , "brand":"honda"}
color = car["color"]
print(color)
```
新增新的屬性
```python
car["mileage"] = 100
```
修改一個原本就有的屬性
```python
car["color"] = "white"
```
刪除一個原本就有的屬性
```python
del car["mileage"]
```
檢視字典裡有什麼屬性(key)
```python
print(car.keys())
```
---
使用.items()讓for迴圈可以遍訪字典
.keys()可以只遍訪屬性(key)
.values()可以只遍訪數值(value)
```python=
for key,value in car.items():
print(key,end=" : ")
print(value)
```
---
整理字典
sorted()
```python=
fav_fruits = {
"alan":"apple",
"jack":"grape",
"rose":"lemon",
"bonny":"banana",
"stan":"apple"
}
for key in sorted(fav_fruits.keys()):
print(key)
```
去掉重複的
set()
```python=
for value in sorted( set( fav_fruits.values() ) ):
print(value)
```
### 字典裡塞字典
```python=
students ={
"alan":{
"ID":"D0001",
"age":21,
"city":"taipei",
},
"jack":{
"ID":"D0002",
"age":21,
"city":"taichung",
},
"rose":{
"ID":"D0003",
"age":20,
"city":"taichung",
}
}
for name , infomation in st.items():
print(name,":", infomation["ID"] , "," , infomation["city"] )
#人名: ID , city
```
### HW: 查「河內塔」