---
# System prepended metadata

title: wk07_串列與元組
tags: [python]

---

## wk07_1019_串列與元組 黃冠維

	
5.1 串列的使用

5.2 使用 for迴圈讀取串列

5.3 串列搜尋與計次

5.4 串列元素新增與刪除

5.5 串列排序

5.6 串列常用的方法列表

5.7 元組(Tuple)
 

## [inclass practice]
### {綜合演練}


實作2

 輸入喜歡的水果，直到Enter鍵結束，找尋fruit = ["香蕉","蘋果","橘子","鳳梨","西瓜"]水果串列是否包含此水果，並顯示該水果是串列中的第幾項




```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("香蕉")

print(list2)
```

    ['櫻桃', '葡萄', '楊桃', '香蕉']
    


```python
list2.pop()
```




    '楊桃'



pop可以不寫參數，就是把最後一個除掉


```python
print(list2)
```

    ['櫻桃', '西瓜']
    


```python
list2[1] = "西瓜"
print(list2)
```

    ['櫻桃', '西瓜']
    


```python
list2.insert(0,"西瓜")
print(list2)
```

    ['西瓜', '櫻桃', '櫻桃', '櫻桃']
    


```python
list2.remove('西瓜')
```


```python
print(list2)
```

    ['櫻桃', '櫻桃', '櫻桃']
    


```python
list2.sort()
print(list2)
list2.reverse()
print(list2)

print(max(list2))
```

    ['櫻桃', '櫻桃', '櫻桃']
    ['櫻桃', '櫻桃', '櫻桃']
    櫻桃
    

### {範例}
1.  串列出值設定 \<list1>
2.  使用 for 迴圈讀取串列元素 \<list2>
3.  利用迴圈配合索引讀取串列元素 \<list3>
4.  以串列計算班級成績 \<append1>
5.  刪除指定串列元素  \<remove>
6.  成績由大到小排序  \<sorted>


```python
list4 = [100, 89, 79]

print(max(list4))
print(min(list4))
print(len(list4))
print(list4.count(79))
print(list4.index(100))
```

    100
    79
    3
    1
    0
    

數列中，第一個數字的index為0


```python
#使用 for 迴圈讀取串列元素 <list2>

print(list4)

for n in list4 :
    print(n, end = ' ')



```

    [100, 89, 79]
    100 89 79 


```python
for i in range(len(list4)) :
    print(i, list4[i], sep= ",")
```

    0,100
    1,89
    2,79
    


```python
list2 = ["櫻桃", "葡萄", "楊桃"]

for i in range(len(list2)) :
    print(i, list2, sep= ",")
```

    0,['櫻桃', '葡萄', '楊桃']
    1,['櫻桃', '葡萄', '楊桃']
    2,['櫻桃', '葡萄', '楊桃']
    


```python
#以串列計算班級成績 <append1>


score = [100, 90, 50, 70, 100, 100, 60, 30, 70, 80]
ttl = 0
avg = 0
n = 0

for score in score :
    ttl = ttl + score
    n = n + 1


avg = ttl / n





print("總成績 :  %3.0f , 平均成績 : %0.2f" % (ttl, avg))
```

    總成績 :  750 , 平均成績 : 75.00
    


```python
score = []

inscore = 0
ttl = 0
avg = 0

while inscore !=-1 :
    inscore = int(input("enter your score , to end just enter -1 and enter"))
    if inscore != -1 :
        score.append(inscore)
        
n = 0

for n in range(len(score)) :
    ttl = ttl + score[n]
avg = ttl / n

print("共有%d人" "總成績 :  %3.0f"  "平均成績 : %0.2f" % (n, ttl, avg))

```

    enter your score , to end just enter -1 and enter100
    enter your score , to end just enter -1 and enter90
    enter your score , to end just enter -1 and enter80
    enter your score , to end just enter -1 and enter70
    enter your score , to end just enter -1 and enter60
    enter your score , to end just enter -1 and enter100
    enter your score , to end just enter -1 and enter-1
    共有5人總成績 :  500平均成績 : 100.00
    

當要相加的時候，要用range把數列的位置表示出來
for迴圈不用再n = n + 1


```python
#刪除指定串列元素 <remove>

fruits = ["櫻桃","葡萄","楊桃","香蕉","酪梨","西瓜","龍眼"]

to_del = "西瓜"

fruits.pop(5)

print(fruits)



```

    ['櫻桃', '葡萄', '楊桃', '香蕉', '酪梨', '龍眼']
    

pop可以刪除列陣中的元素

實作6

西元2021年是牛年。請你開發一個程式:
當使用者輸入他的出生西元年之後，畫面會顯示這個西元年的生肖。






## [afterclass practice]
