## 02 Python基本語法教學 ###### - Python 小社課 - ###### **Gino** --- ## **錯誤訊息** ---- ![](https://i.imgur.com/JS9wec7.png) 出了什麼錯? ---- ![](https://i.imgur.com/Gd3vep5.png) 觀察一下Python給的訊息 (**TypeError**) ---- ![](https://i.imgur.com/iVTZSp6.png) ---- ## 常見錯誤訊息類別 ---- TypeError - 型態錯誤 ![](https://i.imgur.com/paSXctO.png) ---- ValueError - 運算值錯誤 ![](https://i.imgur.com/pASzRHL.png) 字串"one"不能轉成int ---- NameError - 變數未定義 ![](https://i.imgur.com/fxuvZ8d.png) ---- IndentationError - 縮排沒縮好 ![](https://i.imgur.com/OtCqA5P.png) ---- SyntaxError - 語法有問題 ![](https://i.imgur.com/k6YcfyB.png) (第2行少了一個冒號) ---- ZeroDivisionError - 除以0 ![](https://i.imgur.com/cksfeRi.png) --- ## 例外處理 **要怎麼避免錯誤發生** ---- ![](https://i.imgur.com/1HCzz3K.png) if-else ---- ![](https://i.imgur.com/MLAzkcv.png) try-except ---- ```python= try: # 試試看這段程式碼能否正確執行 except 錯誤類別: # 抓到錯誤,程式進入這裡 ``` ---- 一次檢查多個錯誤 ```python= try: ... except 錯誤1 or 錯誤2 or ...: ... ``` ---- Python還可以自己產生錯誤 ```python= raise ZeroDivisionError("b為0,除法無效") ``` ---- ![](https://i.imgur.com/aUtSZ5B.png) --- ## 條件判斷 - 比較運算子 - 條件運算子 - if-else ---- ## 比較運算子 ---- ### 常見的: - $<$ - $>$ - $<$ - $>=$ - $==$ (注意不是一個等於!) - $!=$ ---- ### $=$ v.s. $==$ ```python= a = 3 # 將變數a設為3 a == 3 # 檢查a是否等於3 ``` ---- ## 條件運算子 ---- not and or (優先度:not > and > or) ---- ### **not** ```python= print(3 == 5) # False print(not 3 == 5) # True ``` ```cpp= cout << (3 == 5) << endl; cout << !(3 == 5) << endl; ``` ---- ### **and** ```python= print(5 < 3 and 10 > 6) # False print(5 > 3 and 10 > 6) # True ``` ```cpp= cout << (5 < 3 && 10 > 6) << endl; cout << (5 > 3 && 10 > 6) << endl; ``` ---- ### **or** ```python= print(5 < 3 or 10 < 6) # False print(5 > 3 or 10 < 6) # True ``` ```cpp= cout << (5 < 3 || 10 < 6) << endl; cout << (5 > 3 || 10 < 6) << endl; ``` ---- ## if-else ---- ### **if** ```python= if 條件: (條件成立要做的事) # ^ 這裡記得要縮排(鍵盤左上角tab) ``` ```cpp= if (條件) { 條件成立要做的事 } ``` ---- ### **巢狀if - if裡面還有if** ```python= if 條件A: if 條件B: (要做的事) ``` ```cpp= if (A) { if (B) { ... } } ``` ---- ### **if else** ```python= if 條件: ... else: (條件不成立要做的事) ``` ```cpp= if (條件) { ... } else { (條件不成立要做的事) } ``` ---- ### **elif (否則如果)** ```python= if 條件A: ... elif 條件B: # A不成立才會判斷B ... elif 條件C: # AB不成立才會判斷C ... else: # ABC都不成立 ... ``` ```cpp= if (A) { ... } else if (B) { ... } else if (C) { ... } else { ... } ``` --- ## 迴圈 - while - for ---- ## while ---- ### **while 迴圈** ```python= while 條件: ... # 只要條件成立就會一直執行 ``` ```cpp= while (條件) { ... } ``` ---- ### **無窮迴圈** ```python= while True: ... ``` ```cpp= while (1) { ... } ``` ---- ### **break** ```python= while ...: ... if 某件事情發生了: break # 直接離開while迴圈 ... ``` ```cpp= while (...) { ... if (某件事情發生了) { break; } ... } ``` ---- ### **continue** ```python= while ...: ... if 某件事情發生了: continue # 跳到下一次迴圈 ... ``` ```cpp= while (...) { ... if (某件事情發生了) { continue; } ... } ``` ---- ## for ---- ### **計數迴圈** ```python= for i in range(10): ... ``` ```cpp= for (int i = 0; i < 10; i++) { ... } ``` ---- 備註: range()可以建構一個整數序列 ```python= range(開始, 結束, 間隔) ``` 開始值 - 預設為0 結束值 - 一定要有 間隔值 - 預設為1 ---- ```python= range(5) # 0, 1, 2, 3, 4 range(2, 5) # 2, 3, 4 range(1, 10, 2) # 1, 3, 5, 7, 9 range(9, 0, -1) # 9, 8, 7, 6, 5, 4, 3, 2, 1 ``` ---- ### **巢狀迴圈 - 迴圈裡面的迴圈** ```python= for i in range(10): for j in range(10): ... ``` ```cpp= for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { ... } } ``` ---- for迴圈還有很多用法 之後的小社課就會介紹喔! --- ## 函式 - 介紹 - 參數 - return ---- ### **工具人** 只要你好好告訴他要幹嘛,他使命必達 ```python= def greet(): print("Hello") greet() # 呼叫工具人 ``` ```cpp= void greet() { cout << "Hello" << endl; } greet(); ``` ---- ### **參數** ```python= def greet(name): print("Hello,", name) greet("Gino") # Hello, Gino ``` ```cpp= void greet(string name) { cout << "Hello, " << name << endl; } greet("Gino") ``` ---- ### **return** 函式結束後回傳的東西(任何東西都可以) ---- ### **國中學的函數** #### $f(x) = 5x + 3$ ```python= def f(x): return 5 * x + 3 print(f(4)) # 23 ``` ```cpp= int f(int x) { return 5 * x + 3; } cout << f(4) << endl; ``` ---- ### **比較x, y的大小** ```python= def compare(x, y): if x > y: return True else: return False print(compare(5, 3)) # True print(compare(3, 5)) # False ``` ```cpp= bool compare(int x, int y) { if (x > y) { return true; } else { return false; } } cout << compare(5, 3) << endl; cout << compare(3, 5) << endl; ``` --- ## 複習! ---- - 錯誤訊息與例外處理 - 比較、條件運算子 - **if elif else** - **while**迴圈 - **for**迴圈 - 函式、參數、函式回傳值 ---- ## 練習! ---- - 0001 Hello OJ!(★★) - 0004 長方形愛好者(★★) - 0009 社課不能當人(★★★) - 0010 計時(★★★) - 0020 星座判斷(★★★) - 0021 分糖果(★★★★) - 0022 分糖果2(★★★★) - 0027 飛機上的奧客(★★★★★)
{"metaMigratedAt":"2023-06-15T14:23:46.423Z","metaMigratedFrom":"YAML","title":"02 Python基本語法教學","breaks":true,"slideOptions":"{\"transition\":\"fade\",\"spotlight\":{\"enabled\":false}}","contributors":"[{\"id\":\"ac1507e0-f05c-4708-bdd2-c56d13fb0dbb\",\"add\":5834,\"del\":717}]"}
    403 views