# Python 自己看的Python的小筆記 沒很詳細:) ## 資源 [AP325講義C++(中正大學 吳邦一教授)](https://jmj.cmgsh.tp.edu.tw/files/AP325_v1.3.pdf) [AP325講義Python(中正大學 吳邦一教授)](https://hackmd.io/@bangyewu/Hy2kbYLI6/%2Fg2kqHh5_Q4eQnz-mfNu3Kw) [w3schools(Python)](https://www.w3schools.com/python/default.asp) ## 註解 註釋可用於解釋Python代碼。 ```python!= #This is a comment print("Hello, World!") ``` ## print ```python!= print("Hello World") ``` 輸出 ``` Hello World ``` 如果要換行要加上`\n`。 ```python!= print("Hello\nWorld") ``` ``` Hello World ``` 如果要輸出List且以空格間隔的元素 前面加上`*` ``` print(*results) ``` ## input 常見用法,預設為字串(str)。 ```python!= n = input() ``` 可以加上int變成整數 ```python!= n = int(input()) ``` ## 變數(Variables) 變數是用於存儲數據值的容器。 Python沒有用於聲明變數的命令。 變數在您首次為其分配值時創建。 ```python!= x = 5 print(x) ``` 變數不需要使用任何特定類型聲明,可以在設置後更改類型。 ```python!= x = 4 x = "Sally" print(x) ``` 如果要指定變數的數據類型,可以通過轉換來完成。 ```python!= x = str(3) y = int(3) z = float(3) ``` 可以在一行為多個變數賦予值。 ```python!= x, y, z = "Orange", "Banana", "Cherry" print(x) print(y) print(z) ``` 可以在`print`輸出變數的值。 ```python!= x = "Python is awesome" print(x) ``` 如果需要多個,以`,`隔開。 ```python!= x = "Python" y = "is" z = "awesome" print(x, y, z) ``` 亦可以使用`+` ```python!= x = "Python " y = "is " z = "awesome" print(x + y + z) ``` 在函數外部創建的變數(如所有範例中所示 )稱為全域變數。 全域變數可以被所有人使用。 ```python!= x = "uu" def main(): print("Say " + x) main() ``` ### global 通常,當您在函數中創建變數時,該變數為local,並且只能在該函數中使用。 要在函數內創建全域變數,可以使用關鍵字`global`。 ```python!= def main(): global x x = "uu" print("Say " + x) main() ``` ## 數據類型(Data Types) 文字類型:`str` 數值類型:`int` ,`float complex` 序列類型:`list` ,`tuplerange` 映射類型:`dict` Set 型態:`set`,`frozenset` 布林類型:`bool` 二進位類型:`bytes`, `bytearray memoryview` 無類型:`NoneType` 但事實上常用的只有`str`,`int`,`float`,`list`,`bool` ### bool 當中含有`true`及`false` 您可以在 Python 中計算任何運算式,並獲取兩個表達式之一 ans,或`True` `False` 當您比較兩個值時,將計算表達式,並且 Python 返回 布林答案: ```python!= print(10 > 9) print(10 == 9) print(10 < 9) ``` 會印出 ``` True False False ``` ## 運算子 Python 將運算符分為以下幾組: 1. 算術運算子 2. 賦值運算子 3. 比較運算 4. 邏輯運算子 5. 標識運算符 6. 成員資格運算符 7. 按位運算子 ### 算術運算子 | 符號 | 意思| 如同 | | -------- | -------- | -------- | | `+` |加 | `x + y` | |`-`|減|`x - y`| |`*`|乘|`x * y`| |`/`|有小數點的除法|`x / y`| |`%`|餘數|`x % y` | |`**`| 次方| `x ** y `| |`//`| 沒有小數點的除法| `x // y`| ### 賦值運算符 | 符號 | 例如 | 如同 | | -------- | -------- | -------- | |`=`|`x = 5`|`x = 5`| |`+=`|`x += 3`|`x = x + 3`| |`-=`|`x -= 3`|`x = x - 3`| |`*= `|`x *= 3`|`x = x * 3`| |`/=`|`x /= 3`|`x = x / 3`| |`:=`|`print(x := 3)`|`x = 3`,`print(x)` ### 比較運算符 | 符號|意思 | | -------- | -------- | | `==` | 等於 | | `!=` | 不等於 | | `>` |大於 | | `<` | 小於 | | `>=` |大於等於| | `<=` | 小於等於 | ## List 第一項的索引為 0。 `-1`指最後一項,`-2`指倒數第二項,以此類推。 要確定清單包含多少項,請使用`len()`函數。 ```python!= thislist = ["apple", "banana", "cherry"] print(len(thislist)) #output 3 ``` 要插入新的清單項,而不替換任何現有值,我們可以使用`insert()`。 ```python!= thislist.insert(1, "orange") ``` 可以通過指定開始位置和位置來指定索引範圍 end 範圍。 搜索將從索引 2(包含)開始,到索引 5(不包括)結束 ```python!= thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] print(thislist[2:5]) ``` 刪除指定的項,使用`remove()`。 ```python!= thislist.remove("banana") ``` 刪除指定索引`pop()`,如果括號內沒有填任何索引,將刪除最後一項。 ```python!= thislist.pop(1) #刪除指定項 thislist.pop() #刪除最後一項 ``` 刪除指定的指數或完全刪除清單`del`。 ```python!= del thislist[0] del thislist ``` 清空清單`clear()`。 該清單仍然存在,但沒有內容。 ```python!= thislist.clear() ``` ### 循環清單 While 使用該函數確定清單的長度, 然後從 0 開始,通過引用清單項的索引來迴圈存取清單項。 請記住在每次反覆運算後將索引增加 1。 ```python!= thislist = ["apple", "banana", "cherry"] i = 0 while i < len(thislist): print(thislist[i]) i = i + 1 ``` ### 列表推導式 #### for 簡短的語法 ```python!= fruits = ["apple", "banana", "cherry", "kiwi", "mango"] newlist = [] for x in fruits: if "a" in x: newlist.append(x) print(newlist) ``` 可以改成 ```python!= fruits = ["apple", "banana", "cherry", "kiwi", "mango"] newlist = [x for x in fruits if "a" in x] print(newlist) ``` ### 排序 預設情況下,List 物件有一個按字母數位升序對陣列進行排序的方法`sort()`。 ```python!= thislist.sort() ``` 如果要降序使用keyword參數`reverse = True`。 ```python!= thislist.sort(reverse = True) ``` 反轉陣列項目的順序`reverse()`。 ### 複製陣列 使用`copy()`方法 ```python!= mylist = thislist.copy() ``` ### 總結 | 函數|描述 | | -------- | -------- | | append() | 將元素添加到陣列末尾。 | | clear()| 刪除陣列中的所有元素 | | copy() | 回傳複製的陣列 | count() | 計算指定值在陣列中出現的次數。 | | extend() | 將另一個可迭代物件的所有元素添加到陣列末尾。 | | index() | 返回指定值第一次出現的索引。 | | insert() | 在指定位置插入元素。 | | pop() | 移除指定索引位置的元素並返回該元素(默認為最後一個)。 | | remove() | 移除陣列中第一個匹配的指定值。 | |reverse() |將陣列中的元素順序反轉。 | | sort() | 對陣列中的元素進行排序。 | ## 常用的 輸入多個數字以空格隔開並轉成陣列 ```python!= n = list(map(int,input().split())) ``` 輸出一個陣列且以空格隔開 ```python!= for h in ans_list: print(" ".join(map(str,h))) ```