# Lesson 1 | 環境安裝及Python基本介紹 ## Python特性 * Python被用來建設大型軟體系統、架設網頁服務、分析資料、開發機器學習與人工智慧演算法。 1. 是適合快速開發的直譯式語言。 2. 是物件導向式語言架構。 3. 第三方開源套件眾多。 - Python在資訊軟體、影像處理等其他領域亦常被使用。 - 與其他程式語言相比,Python適合多人協作或用來快速開發大型系統。 - Python擁有功能豐富的機器學習套件[scikit-learn](https://scikit-learn.org/stable/),亦常被用來開發人工智慧模型,支援[PyTorch](https://pytorch.org/)與[TensorFlow](https://www.tensorflow.org/?hl=zh-tw)等深度學習框架。 - Python可用於影像處理,例如使用[OpenCV](https://docs.opencv.org/4.x/d6/d00/tutorial_py_root.html)套件;用於開發網頁,例如基於[Django](https://www.djangoproject.com/)框架;建立web API,如[Flask](https://flask.palletsprojects.com/en/2.2.x/)。 截止至2022年12月,Python在[TIOBE Index](https://www.tiobe.com/tiobe-index/)排名第1位。 ## 開始的第一步:安裝Python(1) * 首先至[Python官方網站](https://www.python.org/)下載。 * 以下為安裝於Windows環境的範例。 ![L1-1](https://i.imgur.com/qA4my6Y.png) * 搜尋Python 3.8.10版 Windows installer。 ![L1-2](https://i.imgur.com/qSFZufX.png) ## 開始的第一步:安裝Python(2) * 接下來安裝整合式開發環境(Integrated Development Environment, IDE),IDE讓我們能更有效地撰寫程式。 * 常見的IDE如[Visual Studio Code (VScode)](https://code.visualstudio.com/)、[Jupyter](https://jupyter.org/)、[PyCharm](https://www.jetbrains.com/pycharm/)、[Spyder](https://www.spyder-ide.org/)等。 * 以下範例為安裝Jupyter於Windows環境(需先安裝Python)。 * 首先點擊Install。 ![L1-3](https://i.imgur.com/uL2RVI9.png) * 往下滑即可看到安裝指令。 ![L1-4](https://i.imgur.com/3FdvZEl.png) * 於Windows介面左下的"開始"執行cmd(命令提示字元),並輸入指令。 ![L1-5](https://i.imgur.com/txwouNd.png) ## 開始的第一步:安裝Python (3) * 安裝完畢後,請從檔案總管進入D槽或C槽,建立新資料夾。 ![L1-6](https://i.imgur.com/Wubnk3B.png) * 滑鼠點選右鍵後點選cmd(或是加上左shift)點選Windows PowerShell,並輸入下行。 ```terminal jupyter notebook ``` * Jupyter Notebook會在預設瀏覽器中自動打開。再點選New,於出現的下拉式選單中選取Python 3 (ipykernel)。 ![L1-7](https://i.imgur.com/lvDVGNl.png) * 進入你所建立的第一個Jupyter Notebook檔案(.ipynb)。可於上方更改檔名。 ![L1-8](https://i.imgur.com/qlbP1Ki.png) ## 第一節:開始撰寫Python程式碼(1) * 我們可以將Python視為與電腦交談的工具,Python將依序(**由左至右、從上到下的**)解析我們的輸入,並讓電腦執行。 * 我們先把Python當作計算機使用,並試著輸入下列程式碼。 * Python會直接執行你所輸入的東西。 ```python= # 四則運算 加+減-乘*除/ 2 + 2 ``` ```python= # 括弧優先規則 5 * (3 + 2) ``` ```python= # 指數運算 12 ** 4 ``` * 接下來先搶先體驗一下程式語言中重要的東西,"函式"(function),函式後面會帶有小括號,小括號內可輸入東西,這個東西稱為"參數"(parameter或argument)。 * 每個函式都有不同的功能,而我們第一個要嘗試的函式是print(): * print()這個函數會將你所輸入的參數顯示出來。 ```python= print("Hello World!") ``` ## 第一節:開始撰寫Python程式碼(2) * 如果我們想將這些數字儲存起來呢?在Python我們可以將它指派成"變數"(variable),我們可以使用等於符號 ("=") 用來為變數賦值。 * 變數的型態有很多種,我們將在後續的課程中詳細介紹。 ```python= x = 10 print(x) ``` ```python= width = 20 print(width * 5) ``` ```python= width = 20 height = 5 * 9 print(width * height) ``` * 你可以直接輸入變數來回傳該變數。注意,在Jupyter Notebook中若不使用print(),則只會回傳cell中的最後一個東西。 ```python= width ``` ## 練習1:熟悉變數的運用 * 當我們理解變數與基本操作後,我們來進一步練習。 * 問題1:請試著利用變數,在一個cell中撰寫程式碼,使其回傳2, 4, 6, 8, 10的3次方,並使用另一個cell,回傳2, 4, 6, 8, 10的平方根。 <details> <summary>解答</summary> ```python= x = 3 print(2 ** x) print(4 ** x) print(6 ** x) print(8 ** x) print(10 ** x) ``` ```python= x = 0.5 print(2 ** x) print(4 ** x) print(6 ** x) print(8 ** x) print(10 ** x) ``` </details> * 問題2:請試著告訴我「//」這個運算子的意義。 <details> <summary>解答</summary> - 拿到整數的結果(即去除所有小數點的部份)。 ```python= x = 2 print(3 // x) print(5 // x) print(7 // x) print(9 // x) print(11 // x) ``` </details> ## 第二節:組合元素 * 若我們要將很多個數字儲存成一個變數時,我們常用"[]"來做這件事情,這樣的變數的"資料型態"(data type)在Python中為list。 * list可以儲存數字。 ```python= squares = [1, 4, 9, 16, 25] print(squares) ``` * list也可以儲存文字,一個list甚至可以儲存不同型態的元素。 ```python= python_string = ["p", "y", "t", "h", "o", "n"] print(python_string) ``` * 我們可以"索引"(index)出list當中的特定"元素"(item)。 * 注意,在Python中索引從0開始。 ```python= print(python_string[0]) print(python_string[1]) print(python_string[2]) ``` * 也可以用代表倒序的負數取元素。 ```python= print(python_string[-1]) print(python_string[-2]) print(python_string[-3]) ``` * 若我們要一次取多個元素呢?可以透過"切片"(slice)來做到這樣的事情。 * 同學們可以藉由這個圖來思考indexing與slicing。 ```python +---+---+---+---+---+---+ | P | y | t | h | o | n | +---+---+---+---+---+---+ 0 1 2 3 4 5 6 -6 -5 -4 -3 -2 -1 ``` * 注意,一樣也是從0開始。 ```python= print(python_string[0:2]) print(python_string[2:6]) ``` * 我們亦可以透過索引來更改list當中的元素。 * 利用等於符號來指派特定位置的數值。 ```python= y = [1, 3, 4, 12, 16, 192, 208] print(y) y[0] = 0 print(y) ``` * 也可以這樣操作。 ```python= y = [1, 3, 4, 12, 16, 192, 208] print(y) y[2] = y[0] + y[1] print(y) y[5] = y[3] + y[4] print(y) ``` ## 練習2:索引與指派 * 當我們熟悉索引與指派後,請完成下列練習。 * 問題1:請先儲存1, 2, 0, 0, 0, 0, 0於一個list中,你可以任意更改前兩個數字,並依序讓第三個數字(奇數)為前兩個相加,第四個數字(偶數)為前兩個數字相乘,第五個數字(奇數)為前兩個相加,依此類推。 <details> <summary>解答</summary> - 我們可以利用索引與指派依序填入數值。 ```python= x = [1, 2, 0, 0, 0, 0, 0] x[2] = x[0] + x[1] x[3] = x[1] * x[2] x[4] = x[2] + x[3] x[5] = x[3] * x[4] x[6] = x[4] + x[5] print(x) ``` - 依照規律我們可以提出idx,理解這樣的邏輯將有助於使用"迴圈"撰寫程式。 ```python= x = [1, 2, 0, 0, 0, 0, 0] idx = 1 x[idx+1] = x[idx-1] + x[idx] idx = 2 x[idx+1] = x[idx-1] * x[idx] idx = 3 x[idx+1] = x[idx-1] + x[idx] idx = 4 x[idx+1] = x[idx-1] * x[idx] idx = 5 x[idx+1] = x[idx-1] + x[idx] print(x) ``` </details> ## 總結 * 這堂課主要讓學生們初步理解Python程式語言。 * 我們已經學到基本的程式運作規律、儲存變數,並已有能力索引與更改元素。 * 同學們請務必回去練習,並在自己的電腦安裝Python,也可以嘗試看看其他IDE。