# wk07_1019_串列與元組
<pre>
5.1 串列的使用
5.2 使用for迴圈讀取串列
5.3 串列搜尋與計次
5.4 串列元素新增與刪除
5.5 串列排序
5.6 串列常用的方法列表
5.7 元組(Tuple)
<pre/>
### {範例}
1. 串列出值設定 \<list1>
2. 使用 for 迴圈讀取串列元素 \<list2>
3. 利用迴圈配合索引讀取串列元素 \<list3>
4. 以串列計算班級成績 \<append1>
5. 刪除指定串列元素 \<remove>
6. 成績由大到小排序 \<sorted>
## 【inclass practice】
```python
#4 以串列計算班級成績 <append>
scores = [100, 90, 50, 70, 100, 100, 90, 90, 60, 30]
```
```python
scores = []
inscore = 0
while inscore != -1 :
inscore = int(input("enter score, to end just enter -1 and enter "))
if inscore != -1 :
scores.append(inscore)
print(scores)
```
enter score, to end just enter -1 and enter 89
enter score, to end just enter -1 and enter 18
enter score, to end just enter -1 and enter 45
enter score, to end just enter -1 and enter 68
enter score, to end just enter -1 and enter -1
[89, 18, 45, 68]
```python
scores = []
inscore = 0
while inscore != -1 :
inscore = int(input("enter score, to end just enter -1 and enter : "))
if inscore != -1 :
scores.append(inscore)
ttl = 0
for score in scores:
ttl = ttl + score
n = len(scores)
avg = ttl / n
print("共有%d人, 總成績 :%3.0f , 平均成績 :%5.2f" % (n, ttl, avg))
```
enter score, to end just enter -1 and enter : 89
enter score, to end just enter -1 and enter : 99
enter score, to end just enter -1 and enter : 56
enter score, to end just enter -1 and enter : -1
共有3人, 總成績 :244 , 平均成績 :81.33
```python
#5 刪除指定串列元素 <remove>
fruits = ["櫻桃", "葡萄", "楊桃", "香蕉", "酪梨", "西瓜", "龍眼"]
to_del = "西瓜" #del用來刪除list中的片段或清空整個list
print(fruits)
```
['櫻桃', '葡萄', '楊桃', '香蕉', '酪梨', '西瓜', '龍眼']
```python
```
```python
list4 = [100, 89, 79]
print(max(list4))
print(min(list4))
print(len(list4))
print(list4.count(79)) #count回傳項目在list中出現的次數
print(list4.index(79))
```
100
79
3
1
2
```python
#2 使用 for 迴圈讀取串列元素 <list4>
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 = ",") #sep = ”," 輸出時,以逗號隔開
```
0,100
1,89
2,79
```python
list1 = []
list2 = ["蘋果", "火龍果", "百香果"]
list3 = ["日本", "韓國", "新加坡"], ["保齡球", "網球", "籃球"]
print(list1)
print(list2)
print(list3)
```
[]
['蘋果', '火龍果', '百香果']
(['日本', '韓國', '新加坡'], ['保齡球', '網球', '籃球'])
```python
print("我最想去旅遊的地方是 %s" % list3[0][0])
```
我最想去旅遊的地方是 日本
```python
print(list2[0:2:1])
```
['蘋果', '火龍果']
```python
list2.append("奇異果") #append在list尾端添加一個新的項目
print(list2)
```
['蘋果', '火龍果', '百香果', '奇異果']
```python
list2.pop() #pop移除list中給定位置的項目,並回傳它。空白即最後一項
```
'奇異果'
```python
list2.pop(0)
```
'蘋果'
```python
list2[1] = "芒果"
print(list2)
```
['蘋果', '芒果', '百香果']
```python
list2.insert(0, "楊桃")
print(list2)
```
['楊桃', '蘋果', '芒果', '百香果']
```python
len(list1) #len用來計算字符串、列表、元组等…的長度
list1.count("楊桃")
```
0
### {綜合演練}
實作2
<pre>
輸入喜歡的水果,直到Enter鍵結束,找尋fruit = ["香蕉","蘋果","橘子","鳳梨","西瓜"]水果串列是否包含此水果,並顯示該水果是串列中的第幾項
<pre/>
實作6
<pre>
西元2021年是牛年。請你開發一個程式:
當使用者輸入他的出生西元年之後,畫面會顯示這個西元年的生肖。
<pre/>
## 【afterclass practice】
1. 綜合演練 選擇題1-10 (需抄題在markdown cell ; 有程式碼的題目要有code cell )
2. 教學影音 lesson 9
( B ) 1. 執行下列程式,下列結果何者正確?
list1 = [1, 2, 3, 4, 5]
print(list1[0])
(A) 0 (B) 1 (C) 2 (D) [1, 2, 3, 4,5]
```python
list1 = [1, 2, 3, 4, 5]
print(list1[0])
```
1
( A ) 2. 執行下列程式,下列結果何者正確?
list4 = ["香蕉", "蘋果", "橘子"]
print(list4[-3])
(A) 香蕉 (B) 蘋果 (C) 橘子 (D) 錯誤,索引值超過範圍
```python
list4 = ["香蕉", "蘋果", "橘子"]
print(list4[-3])
```
香蕉
( D ) 3. 執行下列程式,n 的值為多少?
scores = [85, 79, 93]
n=len(scores)
(A) 0 (B) 1 (C) 2 (D)3
```python
scores = [85, 79, 93]
n=len(scores)
print(n)
```
3
( B ) 4. 執行下列程式,n 的值為多少?
list1 = ["香蕉","蘋果","橘子"]
n = list1.index("蘋果")
(A) 0 (B) 1 (C) 2 (D)3
```python
list1 = ["香蕉","蘋果","橘子"]
n = list1.index("蘋果")
print(n)
```
1
( A ) 5. 執行下列程式,下列結果何者正確?
list1 = ["香蕉","蘋果","橘子"]
n = list1.count("西瓜")
(A) n=0 (B) n=1 (C) n=2 (D) 出現錯誤
```python
list1 = ["香蕉","蘋果","橘子"]
n = list1.count("西瓜")
print(n)
```
0
( 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
```python
list1 = [1,2,3,4,5,6]
m = list1.pop()
n = list1.pop(2)
print(m, n)
```
6 3
( 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
list1 = [1,2,3,4,5,6]
list1.insert(-1, "愛")
list1.insert(12, "台灣")
print(list1[5])
```
愛
( 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]
```python
list1 = [1,2,3,4,5,6]
del list1[1]
print(list1)
```
[1, 3, 4, 5, 6]
( 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]
```python
list1=[3,2,1,5]
list1.reverse()
print(list1)
```
[5, 1, 2, 3]
( 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]
```python
list1=[3,2,1,5]
list2=sorted(list1,reverse=True)
print(list2)
```
[5, 3, 2, 1]