1

# 加法、乘法用於字串與列表

``````s="HelloHelloHelloHelloHello"
``````

``````s=""
for i in range(5):
s = s + "Hello"
# s 的值變成"HelloHelloHelloHelloHello"
``````

python有很方便的運算子，加法(+)和乘法(*)，

``````s="Hello" * 5
``````

``````>>> "234"+"abc"
'234abc'
>>> "2"+"3"
'23'
>>> "真" * 5 + "高興見到你"
'真真真真真高興見到你'
``````

``````>>> [2,4]+[5,6,9]
[2, 4, 5, 6, 9]
>>> [1] * 6
[1, 1, 1, 1, 1, 1]
``````

## 範例: 階梯圖形

``````*
**
***
****
*****
******
*******
``````

``````for i in range(1,8):
print('*' * i)
``````

## 範例: 金字塔圖形

``````      *
***
*****
*******
*********
***********
*************
``````

``````for i in range(1,8):
print(' ' * (7-i) + '*' * (2*i-1))
``````

# 負索引; 切片語法(Slice)

``````L = ["香雞堡", "豬肉堡", "牛肉堡", "總匯三明治", "鮪魚三明治", "火腿三明治"]
>>> L[0]
"香雞堡"
>>> L[1]
"豬肉堡"
>>> L[-1]
"火腿三明治"
``````

L[0]是從左邊數起的第一個元素，
L[1]是從左邊數起的第二個元素，

L[-1]是L的倒數第一個元素，
L[-2]是L的倒數第二個元素，

# 切片介紹

``````foods= [L[0], L[1], L[2]] # ["香雞堡", "豬肉堡", "牛肉堡"]
``````

``````foods=[]
for i in range(3):
foods.append(foodArr[i])
``````

python的切片操作符就是用來簡化這種操作而設的

## 方法三、切片語法

``````newArr = L[起始位置(包含) : 結束位置(不包含): 步長]
``````

L[起始位置(包含) : 結束位置(不包含): 步長] 的意思為，

``````L=["香雞堡", "豬肉堡", "牛肉堡", "總匯三明治", "鮪魚三明治", "火腿三明治"]
foods= L[0:3]
``````

``````>>> L[:3]
["香雞堡", "豬肉堡", "牛肉堡"]
``````

``````>>> L[3:]
['總匯三明治', '鮪魚三明治', '火腿三明治']
``````

``````>>> L[:]
['香雞堡', '豬肉堡', '牛肉堡', '總匯三明治', '鮪魚三明治', '火腿三明治']
``````

## 切片搭配負數index使用

L[-1]是L的倒數第一個元素，
L[-2]是L的倒數第二個元素，

``````>>> L[-2:]
['鮪魚三明治', '火腿三明治']
``````

``````>>> L[1:5:2] #從index = 1的元素開始，每2個取一次，直到index = 5的元素(不包含)
['豬肉堡', '總匯三明治']
``````

# 重要觀念: N = L v.s. N = L[:]

``````L =  ['蛋糕', '蘋果', '香蕉']
N = L[:]
print(N) # ['蛋糕', '蘋果', '香蕉']
``````

`N = L`直接賦值，會把列表`L`所在的記憶體位置直接給`N`
`N = L[:]`才是複製一個列表，將`L`的元素複製一份給`N`

## 程式範例

``````L =  ['蛋糕', '蘋果', '香蕉']
N = L
N[1] = '葡萄'
print(L) # ['蛋糕', '葡萄', '香蕉']
``````

``````L =  ['蛋糕', '蘋果', '香蕉']
N = L[:]
N[1] = '葡萄'
print(L) # ['蛋糕', '蘋果', '香蕉']
``````

## 應用: 反轉一個字串

``````刷牙<->牙刷

dog<->god
desserts(甜點) <-> stressed(壓力)
live(生命) <-> evil(邪惡)
``````

``````def reverseStr(s):
return s[::-1]

print(reverseStr("desserts")) # stressed
print(reverseStr("刷牙")) #牙刷
``````

# 列表生式式語法(List Comprehensions)

，為運用一個列表生出另一個列表的語法

``````新列表 = [something for x in 舊列表(或可迭代物件)]
``````

``````新列表 = []
for x in 舊列表(或可迭代物件):
新列表.append(something)
``````

``````新列表 = [something for x in 舊列表(或可迭代物件) if 條件判斷句]
``````

# 看看列表生成式的各種應用

## 生成1~10的平方數

``````>>> [x * x for x in range(1, 11)]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
``````

## 將字串轉為字元陣列

``````>>> s="Hello"
>>> [c for c in s] #亦可直接寫 list(s)
['H', 'e', 'l', 'l', 'o']
``````

## 雙for迴圈生成全排列

``````>>> [m + n for m in 'abc' for n in '123']
['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3']
``````

## 取出列表中的偶數

``````>>> L=[18, 73, -24, 6, 34, -87, -17, -98, -53, 16]
>>> [x for x in L if x%2==0]
[18, -24, 6, 34, -98, 16]
``````

## 取出列表中的負數

``````>>> [x for x in L if x<0]
[-24, -87, -17, -98, -53]
``````

## 取出列表中的字串型態

(註: 內建函數isinstance(x, type)可以判斷前者是否為後者的變數型態)

``````>>> M = ["Horse","Tiger",25,6.2,"Tomato"]
>>> [x for x in M if isinstance(x, str)]
['Horse', 'Tiger', 'Tomato']
``````

# 融會貫通: range, 切片, 列表生成式

``````newArr = L[起始位置(包含) : 結束位置(不包含): 步長]
``````

range()函數的基礎語法為:

``````range(起始位置(包含), 結束位置(不包含), 步長)
``````

``````新列表 = [something for x in 舊列表(或可迭代物件，常常使用range)]
``````

`range(4,21,3)` 表示數字範圍 `4,7,10,13,16,19`
`L[4:21:3]` 則表示切片的列表 `[L[4], L[7], L[10], L[13], L[16], L[19]]`
`[L[i] for i in range(4,21,3)]` 亦可以表示`[L[4], L[7], L[10], L[13], L[16], L[19]]`