## wk07_1019_ch05_串列與元組
## 【inclass practice】
5.1 串列的使用
5.2 使用 for迴圈讀取串列
5.3 串列搜尋與計次
5.4 串列元素新增與刪除
5.5 串列排序
5.6 串列常用的方法列表
5.7 元組(Tuple)
```python
list1=[]
list2=["櫻桃","葡萄","楊桃"]
list3=[["捷克","印度","芬蘭"],["游泳","籃球","保齡球"]]
print(list1)
print(list2)
print(list3)
```
[]
['櫻桃', '葡萄', '楊桃']
[['捷克', '印度', '芬蘭'], ['游泳', '籃球', '保齡球']]
```python
print("我最想去旅遊的地方是",list3[0][0])
```
我最想去旅遊的地方是 捷克
```python
print(list2[0:2:1])
```
['櫻桃', '葡萄']
```python
list2.append("香蕉")
print(list2)
```
['櫻桃', '葡萄', '楊桃', '香蕉']
```python
list2.pop()
print(list2)
```
['櫻桃', '葡萄', '楊桃']
```python
list2.pop(0)
print(list2)
```
['葡萄', '楊桃']
```python
list2[1]="西瓜"
print(list2)
```
['葡萄', '西瓜']
```python
list2.insert(0,"櫻桃")
print(list2)
```
['櫻桃', '葡萄', '西瓜']
{範例}
1. 串列出值設定 \<list1>
2. 使用 for 迴圈讀取串列元素 \<list2>
3. 利用迴圈配合索引讀取串列元素 \<list3>
4. 以串列計算班級成績 \<append1>
5. 刪除指定串列元素 \<remove>
6. 成績由大到小排序 \<sorted>
```python
#1.
list4=[100,89,79]
print(max(list4))
print(min(list4))
print(len(list4))
print(list4.count(79)) #出現多少次
print(list4.index(79)) #位置
```
100
79
3
1
2
```python
#2.
print(list4)
for n in list4 :
print(n*3,end=" ")
```
[100, 89, 79]
300 267 237
```python
for i in range(len(list4)) :
print(i,list4[i],sep=",")
```
0,100
1,89
2,79
```python
scores=[100,90,50,70,100,100,90,90,60,30]
ttl=0
avg=0
n=0
for score in scores:
ttl+=score
n+=1
avg=ttl/n
print("總成績 : %3.0f , 平均成績 : %5.2f"%(ttl,avg))
```
總成績 : 780 , 平均成績 : 78.00
```python
scores=[]
inscore=0
ttl=0
avg=0
n=0
while inscore != -1 :
inscore = int(input("enter score,to end just enter -1 and enter"))
if inscore != -1 :
scores.append(inscore)
for score in scores:
ttl+=score
n+=1
avg=ttl/n
print("總成績 : %3.0f , 平均成績 : %5.2f"%(ttl,avg))
```
enter score,to end just enter -1 and enter100
enter score,to end just enter -1 and enter90
enter score,to end just enter -1 and enter80
enter score,to end just enter -1 and enter-1
總成績 : 270 , 平均成績 : 90.00
"""
下次講
fruits=["香蕉","蘋果","橘子","鳳梨","西瓜","香蕉","蘋果","橘子","鳳梨","西瓜"]
while True :
print("串列元素有:",fruits)
fruit=input("請輸入要刪除的水果:(Enter 結束)")
"""
{綜合演練}
1. 實作2
輸入喜歡的水果,直到Enter鍵結束,找尋fruit = ["香蕉","蘋果","橘子","鳳梨","西瓜"]水果串列是否包含此水果,並顯示該水果是串列中的第幾項
## 【self practice】
```python
a=input("輸入喜歡的水果:")
fruit = ["香蕉","蘋果","橘子","鳳梨","西瓜"]
b= a in fruit
if b == True :
print("包含","在串列中的第",fruit.index(a),"項")
else :
print("不包含")
```
輸入喜歡的水果:香蕉
包含 在串列中的第 0 項
2. 實作6
西元2021年是牛年。請你開發一個程式:
當使用者輸入他的出生西元年之後,畫面會顯示這個西元年的生肖。
```python
list1=["鼠","牛","虎","兔","龍","蛇","馬","羊","猴","雞","狗","豬",]
a=int(input("輸入西元出生年:"))
print(list1[a%12-4])
```
輸入西元出生年:2021
牛
## 【afterclass practice】
```python
#( B ) 1. 執行下列程式,下列結果何者正確?
list1 = [1, 2, 3, 4, 5]
print(list1[0])
#(A) 0 (B) 1 (C) 2 (D) [1, 2, 3, 4,5]
```
1
```python
#( A ) 2. 執行下列程式,下列結果何者正確?
list4 = ["香蕉", "蘋果", "橘子"]
print(list4[-3])
#(A) 香蕉 (B) 蘋果 (C) 橘子 (D) 錯誤,索引值超過範圍
```
香蕉
```python
#( D ) 3. 執行下列程式,n 的值為多少?
scores = [85, 79, 93]
n=len(scores)
#(A) 0 (B) 1 (C) 2 (D)3
print(n)
```
3
```python
#( B ) 4. 執行下列程式,n 的值為多少?
list1 = ["香蕉","蘋果","橘子"]
n = list1.index("蘋果")
#(A) 0 (B) 1 (C) 2 (D)3
print(n)
```
1
```python
#( A ) 5. 執行下列程式,下列結果何者正確?
list1 = ["香蕉","蘋果","橘子"]
n = list1.count("西瓜")
#(A) n=0 (B) n=1 (C) n=2 (D) 出現錯誤
print(n)
```
0
```python
#( D ) 6. 執行下列程式,下列結果何者正確?
list1 = [1,2,3,4,5,6]
m = list1.pop()
n = list1.pop(2)
#(A) m=1, n=6 (B) m=2, n=3 (C) m=6, n=2 (D) m=6, n=3
print(m,n)
```
6 3
```python
#( C ) 7. 執行下列程式,下列顯示結果何者正確?
list1 = [1,2,3,4,5,6]
list1.insert(-1, "愛")
list1.insert(12, "台灣")
print(list1[5])
#(A) -1 (B) 12 (C) 愛 (D)台灣
```
愛
```python
#( C ) 8. 執行下列程式,下列顯示結果何者正確?
list1 = [1,2,3,4,5,6]
del list1[1]
print(list1)
#(A) [1,2,3,4,5,6] (B) [2,3,4,5,6] (C) [1,3,4,5,6] (D) [1,2,3,4,5]
```
[1, 3, 4, 5, 6]
```python
#( D ) 9. 執行下列程式,下列顯示結果何者正確?
list1=[3,2,1,5]
list1.reverse()
print(list1)
#(A) [3,2,1,5] (B) [1,2,3,5] (C) [5,3,2,1] (D) [5,1,2,3]
```
[5, 1, 2, 3]
```python
#( C ) 10. 執行下列程式,下列顯示結果何者正確?
list1=[3,2,1,5]
list2=sorted(list1,reverse=True)
print(list2)
#(A) [3,2,1,5] (B) [1,2,3,5] (C) [5,3,2,1] (D) [5,1,2,3]
```
[5, 3, 2, 1]