# ch05_串列_w8 高承紘 課堂程式練習
5.5 串列排序
5.6 串列常用的方法列表
5.7 元組(Tuple)
## 【inclass practice】
```python
scores=[30,69,85,100,35]
scores.sort(reverse=True)
print(scores)
```
[100, 85, 69, 35, 30]
```python
scores2=[30,69,85,100,35]
scores_sorted=sorted(scores2,reverse=True)
print(scores2)
print(scores_sorted)
```
[30, 69, 85, 100, 35]
[100, 85, 69, 35, 30]
```python
scores2=[]
a=0
while a==0:
scores2.append(int(input("來個數: ")))
a=int(input("按0繼續"))
scores_sorted=sorted(scores2,reverse=True)
print(scores2)
print(scores_sorted)
```
來個數: 88
按0繼續0
來個數: 6
按0繼續0
來個數: 4
按0繼續0
來個數: 5
按0繼續0
來個數: 2
按0繼續0
來個數: 3
按0繼續0
來個數: 1
按0繼續0
來個數: 5
按0繼續1
[88, 6, 4, 5, 2, 3, 1, 5]
[88, 6, 5, 5, 4, 3, 2, 1]
```python
list1=[30,69,85,100,35]
list2=list1
list1.reverse()
print("原始: ",list2,"反轉後: ",list1)
```
原始: [35, 100, 85, 69, 30] 反轉後: [35, 100, 85, 69, 30]
```python
list2=[30,69,85,100,35]
print(max(list2))
```
100
```python
list1=[30,69,85,100,35]
n=list1[0]
m=n
for i in range(1,5):
if(n<list1[i]):
n=list1[i]
for i in range(1,5):
if(m>list1[i]):
m=list1[i]
print("最大值: ",n,", 最小值: ",m)
```
最大值: 100 , 最小值: 30
```python
血型_個性={"A":"很棒","B":"超棒","AB":"超級棒","O":"超級無敵棒"}
a=input("甚麼血型?")
print(血型_個性[a])
```
甚麼血型?A
很棒
```python
血型_個性={"A":"很棒","B":"超棒","AB":"超級棒","O":"超級無敵棒"}
print(血型_個性.get("o","沒"))
```
沒
#### 實作6
西元2021年是牛年。請你開發一個程式:當使用者輸入他的出生西元年之後,畫面會顯示這個西元年的生肖。
```python
a=int(input("請輸入你幾年出生:"))
b=["鼠","牛","虎","兔","龍","蛇","馬","羊","猴","雞","狗","豬"]
if((2020-a)>0):
c=-((2020-a)%12)
print("你屬",b[c],"!")
else:
c=(a-2020)%12
print("你屬",b[c],"!")
```
請輸入你幾年出生:2024
你屬 龍 !
#### 實作2
輸入喜歡的水果,直到Enter鍵結束,找尋fruit = ["香蕉","蘋果","橘子","鳳梨","西瓜"]水果串列是否包含此水果,並顯示該水果是串列中的第幾項
```python
a=input("請輸入喜歡的水果:")
fruit = ["香蕉","蘋果","橘子","鳳梨","西瓜"]
for i in range(0,5):
if(a==fruit[i]):
print(a,"有出現在串列裡,在第",i+1,"項")
else:
if(i==4 and a!=fruit[i]):
print("沒有在串列裡!!!!")
```
請輸入喜歡的水果:西瓜
西瓜 有出現在串列裡,在第 5 項
```python
fruits = ["香蕉","蘋果","橘子","鳳梨","西瓜"]
i=0
fruit=input("請輸入喜歡的水果: ")
if(fruits.count(fruit)>0):
i=fruits.index(fruit)
print("存在於第",i+1,"項,索引值為",i)
else:
print("404 not found")
```
請輸入喜歡的水果: 8
404 not found
```python
fruits = ["香蕉","蘋果","橘子","鳳梨","西瓜"]
a=0
while(a==0):
fruit=input("要刪掉哪個水果?")
if(fruits.count(fruit)>0):
i=fruits.remove(fruit)
print("已刪除",fruit)
else:
print("404 not found")
a=int(input("要繼續刪嗎?(按其他數字為不要,按0為要)"))
print(fruits)
```
要刪掉哪個水果?蘋果
已刪除 蘋果
要繼續刪嗎?(按其他數字為不要,按0為要)0
要刪掉哪個水果?鳳梨
已刪除 鳳梨
要繼續刪嗎?(按其他數字為不要,按0為要)0
要刪掉哪個水果?7
404 not found
要繼續刪嗎?(按其他數字為不要,按0為要)7
['香蕉', '橘子', '西瓜']
## 【afterclass practice】
綜合演練 選擇題1-10 (需抄題在markdown cell ; 有程式碼的題目要有code cell )
教學影音 : 新手入門#8Dictionary
( B ) 1. 關於字典,下列何者敘述是錯誤的?
(A)以「鍵-值」對方式儲存 (B)資料依序排列
(C)可由「鍵」取得「值」 (D)資料隨機排列
( D ) 2. d={"香蕉":20, "蘋果":50},print(d[0]) 的結果為何?
(A)香蕉 (B)20 (C)50 (D)產生錯誤
```python
d={"香蕉":20, "蘋果":50}
print(d[0])
```
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[1], line 2
1 d={"香蕉":20, "蘋果":50}
----> 2 print(d[0])
KeyError: 0
( B ) 3. d={"香蕉":20, "蘋果":50},print(d["香蕉"]) 的結果為何?
(A)香蕉 (B)20 (C)50 (D)產生錯誤
```python
d={"香蕉":20, "蘋果":50}
print(d["香蕉"])
```
20
( C ) 4. d={"香蕉":20, "蘋果":50},print(d.get("巴樂", 60)) 的結果為何?
(A)20 (B)50 (C)60 (D)None
```python
d={"香蕉":20, "蘋果":50}
print(d.get("巴樂", 60))
```
60
( A ) 5. d={"香蕉":20, "蘋果":50},程式「d["巴樂"]=60」的作用為:
(A)新增資料 (B)修改資料 (C)取得資料 (D)刪除資料
```python
d={"香蕉":20, "蘋果":50}
d["巴樂"]=60
print(d)
```
{'香蕉': 20, '蘋果': 50, '巴樂': 60}
( A ) 6. d={"香蕉":20, "蘋果":50},程式「d.clear()」的作用為:
(A)刪除所有元素 (B)刪除一個元素 (C)刪除字典 (D)以上皆非
```python
d={"香蕉":20, "蘋果":50}
d.clear()
print(d)
```
{}
( C ) 7. d={"香蕉":20, "蘋果":50},print("香蕉" in d) 的結果為何?
(A)20 (B)50 (C)True (D)False
```python
d={"香蕉":20, "蘋果":50}
print("香蕉" in d)
```
True
( C ) 8. 下列哪一個功能可取得字典中所有「值」?
(A)in (B)keys (C)values (D)items
( D ) 9. 下列哪一個功能可取得字典中所有「鍵」及所有「值」?
(A)in (B)keys (C)values (D)items
( B ) 10. d={"香蕉":20},print(d.setdefault("巴樂")) 的結果為何?
(A)20 (B)None (C)巴樂 (D)產生錯誤
```python
d={"香蕉":20}
print(d.setdefault("巴樂"))
```
None