🐣 Python 基礎語法
入門
變數宣告與資料型別
Python 最基本的積木:如何儲存數字、文字、布林值,以及如何查看型別。
🐍 Python
# 整數 / 浮點數 / 字串 / 布林 age = 25 score = 98.5 name = "小明" is_ai = True # 查看型別 print(type(age)) # <class 'int'> print(type(score)) # <class 'float'> print(type(name)) # <class 'str'> # 型別轉換 num_str = str(42) # "42" num_int = int("100") # 100 num_f = float("3.14") # 3.14
Python 是動態型別語言,不需要事先宣告型別。
AI 專案常用:int(索引)、float(權重/機率)、str(文字輸入)、bool(判斷條件)。
AI 專案常用:int(索引)、float(權重/機率)、str(文字輸入)、bool(判斷條件)。
入門
字串操作與 f-string
AI 模型的輸入輸出幾乎都是字串,學好字串操作非常關鍵。
🐍 Python
name = "Claude" score = 95 # f-string(推薦寫法) msg = f"模型 {name} 得分:{score} 分" # 常用字串方法 text = " Hello, AI World! " print(text.strip()) # 去空白 print(text.lower()) # 全小寫 print(text.upper()) # 全大寫 print(text.replace("AI", "人工智慧")) # 切割與合併 words = "Python,NumPy,Pandas".split(",") joined = " | ".join(words) print(words) # ['Python', 'NumPy', 'Pandas'] print(joined) # Python | NumPy | Pandas # 字串切片 s = "AI Python" print(s[0:2]) # "AI" print(s[-6:]) # "Python"
💡 AI重點:處理 LLM 的 prompt 和 response 時,
strip()、split()、f-string 是最常用的三個技巧。
入門
條件判斷 if / elif / else
控制程式流程的基礎,AI 推論中常用來分類輸出結果。
🐍 Python
confidence = 0.87 if confidence >= 0.9: print("高信心度預測") elif confidence >= 0.7: print("中等信心度,建議人工確認") else: print("低信心度,請重新輸入") # 單行三元運算式 label = "貓" if confidence > 0.5 else "不確定" # 多條件組合 is_valid = confidence > 0.6 and confidence <= 1.0 print(f"結果有效:{is_valid}")
AI 模型輸出通常是機率值(0~1),條件判斷讓你根據信心分數決定要採取什麼行動。
入門
迴圈 for / while 與常用技巧
批次處理資料是 AI 的核心操作,掌握迴圈能大幅提高效率。
🐍 Python
models = ["GPT-4", "Claude", "Gemini"] # 基本 for 迴圈 for model in models: print(f"測試模型:{model}") # enumerate — 同時取索引與值(AI常用) for i, model in enumerate(models): print(f"[{i}] {model}") # range — 重複執行 for epoch in range(1, 6): print(f"訓練第 {epoch} 輪") # zip — 同時遍歷兩個清單 scores = [92, 88, 95] for model, score in zip(models, scores): print(f"{model}: {score}分") # while 迴圈 attempt = 0 while attempt < 3: print(f"第 {attempt+1} 次嘗試") attempt += 1
💡 AI重點:
enumerate 和 zip 是處理訓練批次、對應標籤時最常用的工具。📦 資料結構
入門
List 串列 — AI 最常用的容器
儲存多筆資料的基礎結構,訓練資料、預測結果幾乎都用 List 裝。
🐍 Python
data = [10, 20, 30, 40, 50] # 新增 / 刪除 data.append(60) # 尾端新增 data.extend([70, 80]) # 合併另一個 list data.pop() # 移除最後一個 data.pop(0) # 移除第一個 (index 0) # 切片 print(data[0:3]) # 前3筆 print(data[-3:]) # 後3筆 print(data[::2]) # 每隔一個取 # 排序 data.sort() # 升冪排序 data.sort(reverse=True) # 降冪排序 # List Comprehension(AI常用) squares = [x**2 for x in range(10)] evens = [x for x in range(20) if x % 2 == 0] print(squares) # [0, 1, 4, 9, 16, ...] print(evens) # [0, 2, 4, 6, ...]
💡 AI重點:List Comprehension 比 for 迴圈更快更簡潔,在建立特徵向量或過濾資料時非常好用。
入門
AI常用
Dictionary 字典 — 鍵值對應
儲存結構化資料、API 回應、模型參數配置都會用到字典。
🐍 Python
# 建立字典 model_config = { "model": "claude-sonnet-4-6", "temperature": 0.7, "max_tokens": 1024, "top_p": 0.9 } # 存取值 print(model_config["model"]) # 直接存取 print(model_config.get("temperature")) # 安全存取(不存在不報錯) print(model_config.get("seed", 42)) # 帶預設值 # 新增 / 修改 model_config["stream"] = True model_config.update({"temperature": 0.5, "seed": 42}) # 遍歷 for key, value in model_config.items(): print(f" {key}: {value}") # Dict Comprehension scores = {"GPT-4": 92, "Claude": 95, "Gemini": 88} high = {k: v for k, v in scores.items() if v > 90} print(high) # {'GPT-4': 92, 'Claude': 95}
API 呼叫參數、JSON 回應、模型配置幾乎都是字典格式。
.get(key, default) 比直接 [key] 更安全,不會因為 key 不存在而報錯。
入門
Tuple 與 Set — 不可變與去重
Tuple 保護重要資料不被修改,Set 快速去除重複項目。
🐍 Python
# Tuple — 不可變的串列 image_size = (224, 224, 3) # 影像 (H, W, C) h, w, c = image_size # 解包(unpacking) print(f"高:{h} 寬:{w} 通道:{c}") # 函數回傳多值也用 Tuple def get_stats(data): return min(data), max(data), sum(data)/len(data) mn, mx, avg = get_stats([10, 20, 30]) # Set — 去重與集合運算 labels_a = {"貓", "狗", "鳥"} labels_b = {"狗", "魚", "鳥"} print(labels_a & labels_b) # 交集: {'狗', '鳥'} print(labels_a | labels_b) # 聯集: {'貓','狗','鳥','魚'} print(labels_a - labels_b) # 差集: {'貓'} # 去重 tags = ["AI", "ML", "AI", "DL", "ML"] unique = list(set(tags)) # ['AI', 'ML', 'DL']
💡 AI重點:Tuple 常用於固定維度的張量形狀(shape),Set 用來快速確認類別標籤有哪些。
⚙️ 函數與模組
入門
函數 def — 可重用的程式區塊
將重複的邏輯包成函數,讓 AI 流程更模組化、易維護。
🐍 Python
# 基本函數 def greet(name: str, lang: str = "zh") -> str: if lang == "zh": return f"你好,{name}!" return f"Hello, {name}!" print(greet("小明")) # 你好,小明! print(greet("Alice", "en")) # Hello, Alice! # *args — 不定數量位置參數 def average(*nums): return sum(nums) / len(nums) print(average(80, 90, 100)) # 90.0 # **kwargs — 不定數量關鍵字參數 def build_prompt(**kwargs): return {"messages": [kwargs]} # lambda — 單行匿名函數 normalize = lambda x, mx: x / mx print(normalize(75, 100)) # 0.75
型別提示(
name: str, -> str)在 Python 3.5+ 支援,能讓 IDE 和 AI 工具更好地理解你的程式碼意圖。
入門
AI常用
import 模組 — 站在巨人的肩膀上
Python 的強大來自豐富的套件生態,AI 開發少不了 import。
🐍 Python
# 標準庫 import os import json import datetime from pathlib import Path # 常用操作 cwd = os.getcwd() # 目前目錄 now = datetime.datetime.now() files = list(Path(".").glob("*.py")) # JSON — API 回應解析常用 data = {"model": "claude", "tokens": 1024} json_str = json.dumps(data, ensure_ascii=False) parsed = json.loads(json_str) print(parsed["model"]) # claude # AI 常用套件安裝指令: # pip install numpy pandas matplotlib scikit-learn # pip install anthropic openai torch transformers # 別名導入(常見慣例) import numpy as np import pandas as pd import matplotlib.pyplot as plt
💡 AI重點:
import numpy as np、import pandas as pd 是 AI/ML 社群的標準慣例,看到這個就知道這是 AI 程式碼!
進階
高階函數 map / filter / sorted
Python 式(Pythonic)的資料轉換技巧,讓程式碼更簡潔有力。
🐍 Python
scores = [55, 72, 88, 43, 95, 61] # map — 對每個元素套用函數 normalized = list(map(lambda x: x/100, scores)) # [0.55, 0.72, 0.88, 0.43, 0.95, 0.61] # filter — 過濾符合條件的元素 passing = list(filter(lambda x: x >= 60, scores)) # [72, 88, 95, 61] # sorted — 自訂排序鍵 models = [ {"name": "GPT-4", "score": 92}, {"name": "Claude", "score": 95}, {"name": "Gemini", "score": 88}, ] ranked = sorted(models, key=lambda m: m["score"], reverse=True) for r in ranked: print(f"{r['name']}: {r['score']}") # Claude: 95 / GPT-4: 92 / Gemini: 88
🔢 NumPy — 數值運算
進階
NumPy
NumPy Array — AI 的數字骨幹
所有深度學習框架底層都是張量(Tensor),NumPy Array 是最基礎的版本。
🐍 Python
import numpy as np # 建立 Array a = np.array([1, 2, 3, 4, 5]) b = np.zeros((3, 4)) # 3x4 全零矩陣 c = np.ones((2, 3)) # 2x3 全一矩陣 d = np.arange(0, 10, 2) # [0, 2, 4, 6, 8] e = np.linspace(0, 1, 5) # 0到1均勻分5點 r = np.random.rand(3, 3) # 3x3 隨機矩陣 # 形狀操作(AI最重要!) x = np.arange(12) x2d = x.reshape(3, 4) # 變成 3x4 x4d = x.reshape(2, 2, 3) print(x2d.shape) # (3, 4) print(x2d.dtype) # int64 print(x2d.ndim) # 2(二維) print(x2d.size) # 12(元素總數)
💡 AI重點:影像資料 shape 通常是
(N, H, W, C),文字資料是 (batch, seq_len, embed_dim)。理解 shape 是 AI 工程師必備技能。
進階
NumPy
NumPy 向量化運算與廣播
比 Python 迴圈快 100 倍的秘密:向量化(vectorization)與廣播(broadcasting)。
🐍 Python
import numpy as np a = np.array([1, 2, 3, 4]) b = np.array([10, 20, 30, 40]) # 向量化 — 不需要迴圈 print(a + b) # [11, 22, 33, 44] print(a * 2) # [2, 4, 6, 8](廣播) print(a ** 2) # [1, 4, 9, 16] # 統計函數(AI訓練監控必用) data = np.array([85, 90, 78, 92, 88]) print(np.mean(data)) # 平均值 86.6 print(np.std(data)) # 標準差 print(np.max(data)) # 最大值 92 print(np.argmax(data)) # 最大值的索引 3(AI分類用) # 矩陣乘法(神經網路的核心運算) W = np.random.rand(3, 4) # 權重矩陣 x = np.random.rand(4) # 輸入向量 y = W @ x # 矩陣乘法,等同 np.dot(W, x) print(y.shape) # (3,) # Softmax(分類輸出) logits = np.array([2.1, 0.5, 1.3]) exp = np.exp(logits) probs = exp / np.sum(exp) print(probs) # 和為 1 的機率分佈
@ 是矩陣乘法運算子(Python 3.5+)。神經網路的每一層計算本質上都是 y = W @ x + b,理解這個讓你真正懂 AI 在做什麼。
🐼 Pandas — 資料處理
進階
Pandas
DataFrame — AI 資料的標準格式
讀取、查看、探索 CSV 資料集,是 AI 專案的第一步。
🐍 Python
import pandas as pd # 讀取 CSV(最常見操作) df = pd.read_csv("data.csv") # 建立 DataFrame df = pd.DataFrame({ "name": ["Alice", "Bob", "Carol"], "score": [92, 85, 95], "passed": [True, True, True] }) # 基本查看 print(df.head(3)) # 前3行 print(df.tail(2)) # 後2行 print(df.shape) # (3, 3) → 3列3欄 print(df.dtypes) # 各欄型別 print(df.describe()) # 統計摘要 print(df.info()) # 完整資訊含缺失值 # 存取欄位 scores = df["score"] # Series subset = df[["name", "score"]] # 多欄 DataFrame
💡 AI重點:AI 專案 80% 的時間是資料前處理。
df.describe() 和 df.info() 是探索資料集的必用起手式。
進階
Pandas
AI常用
Pandas 資料清理與篩選
處理缺失值、過濾條件、群組統計 — AI 訓練前的必要工序。
🐍 Python
import pandas as pd import numpy as np df = pd.DataFrame({ "age": [25, np.nan, 30, 22, np.nan], "city": ["台北", "台中", None, "高雄", "台北"], "score": [85, 90, 78, 92, 88] }) # 處理缺失值 print(df.isnull().sum()) # 各欄缺失數量 df_clean = df.dropna() # 刪除含缺失值的行 df["age"].fillna(df["age"].mean(), inplace=True) # 用平均值填補 # 條件篩選 high = df[df["score"] > 85] taipei = df[df["city"] == "台北"] multi = df[(df["score"] > 80) & (df["age"] < 30)] # 群組統計 grouped = df.groupby("city")["score"].mean() print(grouped) # 新增欄位 df["grade"] = df["score"].apply( lambda x: "優" if x >= 90 else ("良" if x >= 80 else "普") ) # 排序 df_sorted = df.sort_values("score", ascending=False)
📊 資料視覺化
進階
Matplotlib
Matplotlib — 訓練曲線與資料圖表
視覺化訓練 loss、準確率變化,快速了解模型學習狀況。
🐍 Python
import matplotlib.pyplot as plt import numpy as np epochs = range(1, 11) train_loss = [0.95, 0.75, 0.60, 0.48, 0.38, 0.30, 0.24, 0.19, 0.15, 0.12] val_loss = [0.98, 0.80, 0.67, 0.55, 0.46, 0.39, 0.34, 0.30, 0.27, 0.25] # 折線圖 — 訓練曲線 plt.figure(figsize=(8, 4)) plt.plot(epochs, train_loss, 'b-o', label='Training Loss') plt.plot(epochs, val_loss, 'r--s', label='Validation Loss') plt.title('訓練曲線') plt.xlabel('Epoch') plt.ylabel('Loss') plt.legend() plt.grid(True, alpha=0.3) plt.tight_layout() plt.savefig('training_curve.png', dpi=150) plt.show() # 子圖 — 同時顯示多張圖 fig, axes = plt.subplots(1, 2, figsize=(12, 4)) axes[0].bar(['A', 'B', 'C'], [10, 20, 15]) axes[1].hist(np.random.randn(1000), bins=30) plt.show()
💡 AI重點:訓練 AI 模型時,每隔幾個 epoch 畫一次 loss 曲線,能及早發現過擬合(overfitting)問題。
🤖 AI / ML 入門
進階
AI/ML
Scikit-learn
Scikit-learn — 5 行程式碼訓練模型
Python 最易上手的機器學習庫,從資料分割到模型評估一站搞定。
🐍 Python
from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score, classification_report # 1. 載入資料 iris = load_iris() X, y = iris.data, iris.target # 2. 切分訓練/測試集(8:2) X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42 ) # 3. 標準化(重要!) scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) # 4. 訓練模型 model = LogisticRegression(max_iter=200) model.fit(X_train, y_train) # 5. 評估 y_pred = model.predict(X_test) print(f"準確率:{accuracy_score(y_test, y_pred):.2%}") print(classification_report(y_test, y_pred, target_names=iris.target_names))
Scikit-learn 的 API 設計非常一致:建立模型 → fit() 訓練 → predict() 預測。所有模型都遵循這個介面,學一個就能舉一反三。
進階
AI/ML
用 Python 呼叫 AI API(Claude / OpenAI)
透過 API 讓 Python 程式與大型語言模型對話,打造自己的 AI 應用。
🐍 Python — Anthropic Claude API
# pip install anthropic import anthropic client = anthropic.Anthropic(api_key="YOUR_API_KEY") # 基本對話 message = client.messages.create( model="claude-sonnet-4-6", max_tokens=1024, messages=[ {"role": "user", "content": "用一句話解釋什麼是機器學習"} ] ) print(message.content[0].text) # 多輪對話 history = [] def chat(user_msg): history.append({"role": "user", "content": user_msg}) resp = client.messages.create( model="claude-sonnet-4-6", max_tokens=512, messages=history ) ai_msg = resp.content[0].text history.append({"role": "assistant", "content": ai_msg}) return ai_msg print(chat("什麼是 Python?")) print(chat("給我一個簡單範例"))
💡 AI重點:API Key 請存在環境變數
ANTHROPIC_API_KEY,不要直接寫在程式碼裡,避免洩漏!
進階
AI/ML
資料預處理 — AI 訓練前的關鍵步驟
垃圾進、垃圾出(GIGO)。良好的資料預處理是 AI 成功的一半。
🐍 Python
import numpy as np import pandas as pd from sklearn.preprocessing import ( StandardScaler, MinMaxScaler, LabelEncoder ) data = pd.DataFrame({ "age": [25, 30, 22, 35], "income": [30000, 50000, 25000, 80000], "city": ["台北", "台中", "高雄", "台北"] }) # 數值標準化(Z-score) scaler = StandardScaler() data["age_scaled"] = scaler.fit_transform(data[["age"]]) # 最小最大縮放(0~1) mms = MinMaxScaler() data["income_norm"] = mms.fit_transform(data[["income"]]) # 類別編碼 le = LabelEncoder() data["city_code"] = le.fit_transform(data["city"]) print(le.classes_) # ['台中' '台北' '高雄'] # One-Hot 編碼 dummies = pd.get_dummies(data["city"], prefix="city") data = pd.concat([data, dummies], axis=1) print(data.head())
StandardScaler:讓特徵平均為 0、標準差為 1,適合大多數模型。
MinMaxScaler:縮放到 [0,1],適合神經網路輸入層。
LabelEncoder / One-Hot:將文字類別轉成數字,模型才能處理。
MinMaxScaler:縮放到 [0,1],適合神經網路輸入層。
LabelEncoder / One-Hot:將文字類別轉成數字,模型才能處理。
入門
AI常用
Python 環境設定 — venv 與套件管理
正確設置虛擬環境,避免不同專案的套件衝突,是 AI 開發的第一步。
💻 Terminal(命令列)
# 建立虛擬環境 python3 -m venv ai_env # 啟動(Mac/Linux) source ai_env/bin/activate # 啟動(Windows) ai_env\Scripts\activate # 安裝 AI 必備套件 pip install numpy pandas matplotlib scikit-learn pip install jupyter notebook pip install anthropic # Claude API pip install torch torchvision # PyTorch # 儲存套件清單 pip freeze > requirements.txt # 其他人從清單安裝 pip install -r requirements.txt # 啟動 Jupyter Notebook jupyter notebook
💡 最佳實踐:每個 AI 專案都建立獨立的虛擬環境,避免套件版本互相干擾。
requirements.txt 讓團隊環境保持一致。
💬 留言板 — 想學哪個 Python 技巧?歡迎留言!
登入 Google 帳號即可留言