WeHelp
掌握基礎的 Python 觀念、語法、和程式運作流程,以及物件導向程式的入門技巧。
  1. Python 快速開始
  2. 資料、資料型態
  3. 變數的使用
  4. 運算符號
  5. IF 條件判斷
  6. Match 條件判斷
  7. While 迴圈
  8. For 迴圈
  9. 函式基礎
  10. 函式進階
  11. 類別、物件基礎
  12. 類別的繼承
  13. 模組與封包
  14. 例外處理
Python 快速開始
# 一、Python 直譯器是什麼? Python 直譯器是執行 Python 程式的軟體。安裝 Python 後,才能透過終端機或 VS Code 執行 `.py` 程式。 建議從官方網站下載: - Python 官方網站:<https://www.python.org/downloads/> - VS Code 官方網站:<https://code.visualstudio.com/> --- # 二、Windows:下載與安裝 Python ## 1. 下載 Python 1. 開啟 Python 官方下載頁面: <https://www.python.org/downloads/windows/> 2. 點選最新的 **Python 3.x.x** Windows Installer。 3. 一般 Windows 電腦選擇: - `Windows installer (64-bit)` 如果是較舊的 32 位元 Windows,才選擇 32-bit 版本。 ## 2. 安裝 Python 執行下載的安裝檔後: 1. 務必勾選: ```text Add python.exe to PATH ``` 2. 點選: ```text Install Now ``` 3. 等待安裝完成,按下 `Close`。 > `Add python.exe to PATH` 很重要,可以讓你在命令提示字元中直接使用 `python` 指令。 ## 3. 確認 Python 是否安裝成功 按下: ```text Windows 鍵 ``` 搜尋並開啟: ```text 命令提示字元 ``` 或開啟 PowerShell,輸入: ```powershell python --version ``` 如果顯示類似: ```text Python 3.13.2 ``` 表示安裝成功。 Windows 也可以使用: ```powershell py --version ``` 若 `python` 指令無法使用,但 `py` 可以使用,之後可以用 `py` 執行程式,例如: ```powershell py hello.py ``` --- # 三、Mac:下載與安裝 Python ## 1. 下載 Python 1. 開啟 Python 官方網站: <https://www.python.org/downloads/macos/> 2. 點選最新的 Python 3.x.x macOS Installer。 3. 大多數現代 Mac 都可以使用官方提供的 macOS 安裝檔。 如果你不確定 Mac 是 Intel 或 Apple Silicon,可以查看: 1. 點選左上角 Apple 選單。 2. 選擇「關於這台 Mac」。 3. 查看處理器資訊。 ## 2. 安裝 Python 1. 開啟下載的 `.pkg` 檔案。 2. 按照畫面指示按「繼續」。 3. 同意授權條款。 4. 選擇安裝位置,通常使用預設值即可。 5. 完成安裝。 ## 3. 確認 Python 是否安裝成功 開啟 Mac 的「終端機」: 1. 按下 `Command + 空白鍵` 2. 搜尋「終端機」或 `Terminal` 3. 開啟它 輸入: ```bash python3 --version ``` 如果顯示類似: ```text Python 3.13.2 ``` 表示安裝成功。 Mac 通常使用 `python3`,而不是 `python`。執行程式時也使用: ```bash python3 hello.py ``` --- # 四、安裝 VS Code VS Code 是撰寫程式的編輯器,需要另外安裝。 ## Windows 安裝方式 1. 開啟: <https://code.visualstudio.com/> 2. 點選: ```text Download for Windows ``` 3. 執行下載的安裝檔。 4. 建議在安裝選項中勾選: - Add to PATH - Add “Open with Code” action - Create a desktop icon,視需要勾選 5. 按照畫面完成安裝。 ## Mac 安裝方式 1. 開啟: <https://code.visualstudio.com/> 2. 下載適合 Mac 的版本: - Apple Silicon:Apple Silicon 版本 - Intel Mac:Intel 版本 3. 開啟下載的 `.zip` 檔案。 4. 將 `Visual Studio Code.app` 拖曳到「應用程式」資料夾。 5. 從「應用程式」或 Launchpad 開啟 VS Code。 --- # 五、在 VS Code 安裝 Python 擴充功能 Windows 和 Mac 的操作方式大致相同。 1. 開啟 VS Code。 2. 點選左側的「擴充功能」圖示,或按: ```text Ctrl + Shift + X ``` Mac 使用: ```text Command + Shift + X ``` 3. 搜尋: ```text Python ``` 4. 安裝發行者為: ```text Microsoft ``` 的 Python 擴充功能。 建議同時安裝: ```text Pylance ``` 它可以提供程式碼補全、錯誤提示和型別檢查。 --- # 六、使用 VS Code 撰寫第一隻 Python 程式 以下 Windows 和 Mac 的操作流程幾乎相同。 ## 1. 建立資料夾 例如建立一個資料夾: ```text python_beginner ``` 建議不要使用太複雜的資料夾名稱,也盡量避免路徑中包含特殊符號。 ## 2. 使用 VS Code 開啟資料夾 在 VS Code 中: 1. 選擇「檔案」→「開啟資料夾」。 2. 選擇剛才建立的 `python_beginner` 資料夾。 3. 如果出現是否信任資料夾的提示,確認是自己建立的資料夾後,可以選擇信任。 ## 3. 建立 Python 檔案 在左側檔案總管中: 1. 點選新增檔案。 2. 將檔案命名為: ```text hello.py ``` 副檔名 `.py` 代表這是一個 Python 程式檔案。 ## 4. 輸入第一個程式 在 `hello.py` 中輸入: ```python print("Hello, Python!") print("這是我的第一隻 Python 程式。") ``` 按下儲存: - Windows:`Ctrl + S` - Mac:`Command + S` --- # 七、選擇 Python 直譯器 VS Code 需要知道要使用哪一個 Python 直譯器。 ## Windows 1. 按下: ```text Ctrl + Shift + P ``` 2. 搜尋: ```text Python: Select Interpreter ``` 3. 選擇剛才安裝的 Python 3.x。 ## Mac 1. 按下: ```text Command + Shift + P ``` 2. 搜尋: ```text Python: Select Interpreter ``` 3. 選擇 Python 3.x。 如果清單中有多個 Python,通常選擇版本較新的、來自 Python 官方安裝位置的項目即可。 --- # 八、執行第一隻 Python 程式 ## 方法一:使用右上角的執行按鈕 開啟 `hello.py` 後,點選右上角的三角形按鈕: ```text Run Python File ``` 程式輸出會顯示在下方的「終端機」面板。 預期輸出: ```text Hello, Python! 這是我的第一隻 Python 程式。 ``` ## 方法二:使用 VS Code 終端機 在 VS Code 中選擇: ```text 終端機 → 新增終端機 ``` 或使用快捷鍵: - Windows:`Ctrl + `` - Mac:`Control + `` 反引號通常位於鍵盤左上方、數字 1 的左邊。 ### Windows 在終端機輸入: ```powershell python hello.py ``` 如果 `python` 無法使用,可以輸入: ```powershell py hello.py ``` ### Mac 在終端機輸入: ```bash python3 hello.py ``` --- # 九、再試一個可以互動的程式 將 `hello.py` 改成: ```python name = input("請輸入你的名字:") print(f"你好,{name}!") ``` 執行後,程式會要求輸入名字,例如: ```text 請輸入你的名字:小明 你好,小明! ``` 這個程式示範了: - `input()`:取得使用者輸入 - 變數 `name`:儲存資料 - `print()`:顯示文字 - `f"..."`:將變數放入字串中 --- # 十、常見問題 ## 1. Windows 顯示找不到 `python` 可以嘗試: ```powershell py --version ``` 如果成功,再使用: ```powershell py hello.py ``` 如果兩者都無法使用,可能是安裝 Python 時沒有勾選「Add python.exe to PATH」。可以重新執行 Python 安裝程式,或重新安裝並勾選該選項。 ## 2. Mac 顯示找不到 `python` Mac 通常應該使用: ```bash python3 --version ``` 而不是: ```bash python --version ``` 執行程式時使用: ```bash python3 hello.py ``` ## 3. VS Code 顯示沒有選擇 Python 直譯器 按下命令面板快捷鍵,選擇: ```text Python: Select Interpreter ``` 然後選取已安裝的 Python 3.x。 ## 4. 檔案變成 `hello.py.txt` Windows 有時會隱藏副檔名,導致檔案實際名稱變成 `hello.py.txt`。請在檔案總管中開啟: ```text 檢視 → 顯示 → 副檔名 ``` 確認檔案名稱確實是: ```text hello.py ``` 而不是: ```text hello.py.txt ``` --- # 最簡操作總結 ## Windows 安裝並確認: ```powershell python --version ``` 建立 `hello.py` 後執行: ```powershell python hello.py ``` 若不行: ```powershell py hello.py ``` ## Mac 安裝並確認: ```bash python3 --version ``` 建立 `hello.py` 後執行: ```bash python3 hello.py ``` VS Code 中則要安裝 Microsoft 發行的 **Python** 擴充功能,並選擇正確的 Python interpreter。
相關學習地圖、教學課程
Python 資料工程
從 0 開始,成為資料工程師的學習路徑。
Python 後端工程、資料庫
從 0 開始,成為後端工程師的學習路徑。