# wk7_1019 wk7_1019_ch05_串列與元組 5.1 串列的使用 5.2 使用 for迴圈讀取串列 5.3 串列搜尋與計次 5.4 串列元素新增與刪除 5.5 串列排序 5.6 串列常用的方法列表 5.7 元組(Tuple) 【inclass practice】 {綜合演練} 實作2 輸入喜歡的水果,直到Enter鍵結束,找尋fruit = ["香蕉","蘋果","橘子","鳳梨","西瓜"]水果串列是否包含此水果,並顯示該水果是串列中的第幾項 In [ ]: fruit= ["香蕉","蘋果","橘子","鳳梨","西瓜"] del_fruit="蘋果" In [17]: list1=[] fruit=["tomato","grape","blueberry"] list3=[["Canada","Germany","the UK"],["volleyball","tabletennis","sleep"]] print(list1) print(fruit) print(list3) [] ['tomato', 'grape', 'blueberry'] [['Canada', 'Germany', 'the UK'], ['volleyball', 'tabletennis', 'sleep']] In [18]: fruit=["tomato","grape","blueberry"] fruit[2] Out[18]: 'blueberry' In [19]: fruit.append("cherry") fruit.append("watermelon") print(fruit) ['tomato', 'grape', 'blueberry', 'cherry', 'watermelon'] In [22]: fruit.insert(3,"apple") print(fruit) ['tomato', 'grape', 'blueberry', 'apple', 'apple', 'cherry', 'watermelon', 'apple'] In [23]: fruit.pop(1) print(fruit) ['tomato', 'blueberry', 'apple', 'apple', 'cherry', 'watermelon', 'apple'] In [25]: fruit.remove("tomato") print(fruit) ['blueberry', 'apple', 'apple', 'cherry', 'watermelon', 'apple'] In [27]: for i in fruit: print(i) for i in range(5): print(i,fruit[i]) blueberry apple apple cherry watermelon apple 0 blueberry 1 apple 2 apple 3 cherry 4 watermelon In [28]: len(fruit) Out[28]: 6 In [29]: fruit.index("watermelon") Out[29]: 4 In [30]: fruit.count("cherry") Out[30]: 1 In [ ]: print(fruit) fruit.index("banana") fruit.count("banana") 實作6 西元2021年是牛年。請你開發一個程式: 當使用者輸入他的出生西元年之後,畫面會顯示這個西元年的生肖。 {範例} 1. 串列出值設定 \ 2. 使用 for 迴圈讀取串列元素 \ 3. 利用迴圈配合索引讀取串列元素 \ 4. 以串列計算班級成績 \ 5. 刪除指定串列元素 \ 6. 成績由大到小排序 \ In [31]: fruit=["tomato","grape","blueberry","watermelon","pineapple"] print(fruit) del_fruit="tomato" fruit.remove(del_fruit) print("我已經完成刪除") print("查無此水果") print(fruit) ['tomato', 'grape', 'blueberry', 'watermelon', 'pineapple'] 我已經完成刪除 查無此水果 ['grape', 'blueberry', 'watermelon', 'pineapple'] In [32]: fruit=["tomato","grape","blueberry","watermelon","pineapple"] print(fruit) del_fruit="banana" if fruit.count(del_fruit)==0: print("查無此水果") else: fruit.remove(del_fruit) print("我已經完成刪除",del_fruit) print(fruit) ['tomato', 'grape', 'blueberry', 'watermelon', 'pineapple'] 查無此水果 ['tomato', 'grape', 'blueberry', 'watermelon', 'pineapple'] In [37]: scores=[100,20,90,100,90,30] ttl=0 avg=0 n=0 for score in scores: ttl=ttl+score n=n+1 avg=ttl/n print("total %d,average %6.2f" %(ttl,avg)) total 430,average 71.67 In [40]: scores=[] in_score=0 while in_score!=-1: in_score=int(input("please enter score")) if in_score !=-1: score.append(in_score) print(scores) 【afterclass practice】 1. 綜合演練 選擇題1-10 (需抄題在markdown cell ; 有程式碼的題目要有code cell ) 2. 教學影音 lesson 8 ( B ) 1. 執行下列程式,下列結果何者正確? list1 = [1, 2, 3, 4, 5] print(list1[0]) (A) 0 (B) 1 (C) 2 (D) [1, 2, 3, 4,5] In [1]: list1 = [1, 2, 3, 4, 5] print(list1[0]) 1 ( A ) 2. 執行下列程式,下列結果何者正確? list4 = ["香蕉", "蘋果", "橘子"] print(list4[-3]) (A) 香蕉 (B) 蘋果 (C) 橘子 (D) 錯誤,索引值超過範圍 In [2]: list4 = ["香蕉", "蘋果", "橘子"] print(list4[-3]) 香蕉 ( D ) 3. 執行下列程式,n 的值為多少? scores = [85, 79, 93] n=len(scores) (A) 0 (B) 1 (C) 2 (D)3 In [11]: 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 In [12]: list1 = ["香蕉","蘋果","橘子"] n = list1.index("蘋果") print(n) 1 ( A ) 5. 執行下列程式,下列結果何者正確? list1 = ["香蕉","蘋果","橘子"] n = list1.count("西瓜") (A) n=0 (B) n=1 (C) n=2 (D) 出現錯誤 In [13]: 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 In [14]: 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)台灣 In [7]: 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] In [8]: 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] In [9]: 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] In [10]: list1=[3,2,1,5] list2=sorted(list1,reverse=True) print(list2) [5, 3, 2, 1]