Python

Python安裝

  1. Python官網下載Python的安裝檔(本教學以3.12.2的版本為例)
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →
  2. 開啟下載後的安裝檔後先選擇加入環境變數後再點及安裝
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    如果沒有加到環境變數怎麼辦?

    (1) 在設定中找到進階系統設定
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    (2) 選擇環境變數
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    (3) 找到Path這個選項點兩下進去
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    (4) 將前面安Python的路徑加入環境變數按下確定就完成了
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

Python Venv

  1. 在桌面新增一個資料夾venv
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →
  2. 開啟資料夾後在上面的路徑欄輸入cmd後按下enter開啟命令提示字元
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →
  3. 輸入下面指令建立虛擬環境
python -m venv <環境名稱>

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

4. 輸入指定切換路徑到activate的路徑

cd <環境名稱>/Script

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

5. 輸入指令切換虛擬環境

activate.bat

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

6. 安裝套件的指令(需先切換到requirements.txt的路徑位置)

pip install -r requirements.txt
  1. 離開虛擬環境指令
deactivate

Python 基本語法

  • 輸入輸出
# 印出Hello World print("Hello World") # 跳出輸入框可以輸入 input("輸入的提示訊息")
  • 變數
int a = 5 float b = 2.5 str c = '你好'
  • 數學運算子
X + Y X - Y X * Y X / Y # X除以Y X // Y # X除以Y並且只取整數解 X % Y # X除以Y的餘數 X ** Y # X的Y次方
  • 流程控制
if <條件>: <執行的程式碼> elif<條件>: <執行的程式碼> else: <執行的程式碼>
  • for迴圈
for <儲存的變數> in <序列型別的資料或數字>: <執行的程式碼>
  • while迴圈
while <成立的條件>: <執行的程式碼(需要有中止條件)>