Python 程式設計 林奇賦 daky1983@gmail.com
Outline u 流程控制 u 迴圈
布林運算 u 有三種布林運算 and, or, not 運算範例結果 or 2==3 or 3 < 7 True and 2==3 and 3 < 7 False not not 3 < 7 False
比較運算子 運算符號 描述 < 小於 <= 小於或等於 > 大於 >= 大於或等於 == 比較值是否相等!= 比較值是否不相等 is 比較是否同一個物件 (id) is not 比較是否不同個物件 (id)
邏輯運算 u 邏輯運算是針對真假值 ( 布林值 ) 的運算 u 布林型態只有兩種值 : True 跟 False u 布林語境 (Boolean context) : 談論真假, 運算真假的情境 u 在布林語境中, 0 和任何的空資料代表 False, 其他代表 True( 通常 True 會跟 1 連結 ), None 在布林語境中也是 False
if ( 如果 就 ) l if 是最單純的一種條件分支敘述句 l If 後 面接 condition, 最後需要加上冒號 : l 冒號 : 之後的下 一 行程式碼記得要縮排 l 當條件 (condition) 成 立時 (True), 執 行冒號後 面的 suite 程式 碼, 若條件不成 立 (False), 略過整個 suite 開始執 行suite 之後 一 行程式碼 l 用 condition 來選擇 suite 程式碼做或不做 縮排 if Condition : suite Condition 是 一個完整的敘述, 並且 python 會在布林語境中解讀所以會是 一個真假敘述, 最常使 用的是 比較運算式
if 以下是流程示意圖 Statement n Statement n if Condition : suite Statement n+1 Condition True suite False Statement n+1
if 範例 1 2 3 4 5 6 #- *- coding:utf- 8 - *- # 範例程式 EX02_01.py # 判斷 2 的 10 次方是否等於 1024 if 2**10 == 1024: print("2^10=1024") 原始碼 : https://gist.github.com/chifu/f044779487741c829734#file-ex02_01-py
if, else (2 選 1) 利用 if/else 敘述可以根據條件選擇執行區塊 A 或 B if Condition : suite A else : suite B 當條件 (condition) 成立時 (True), 執行 if 冒號後面的 suite A 程式碼, 執行完後跳到 suite B 之後一行敘述執行 若條件不成立 (False), 則執行 suite B 之程式碼, 執行完後跳到 suite B 之後一行敘述執行 用 condition 來選擇做 suite A 還是 suite B
if, else 流程示意圖 Statement n Statement n if Condition : suite A else : suite B True Suite A Condition False Suite B Statement n+1 Statement n+1
if, else 範例 1 2 3 4 5 6 7 8 9 10 #- *- coding:utf- 8 - *- # 範例程式 EX02_02.py # 判斷輸入的數字是奇數還是偶數 num = int(input('please input a num:')) if num % 2 == 0: print(num,' 是偶數 ') else: print(num,' 是奇數 ') 原始碼 : https://gist.github.com/chifu/f044779487741c829734#file-ex02_02-py
if, elif, else ( 多選 1) 當選擇超過兩種的時候使用 if, elif(else if), else 的語法來決定執行區塊 A 或 B 或 C if Condition 1: suite 1 elif Condition 2: suite 2 elif Condition k: suite K else: suite N 當條件 (condition k) 成立時 (True), 執行冒號後面的 suite K 程式碼, 執行完後跳到 suite N 之後一行敘述執行 若條件 (condition k) 不成立 (False), 則往下開始判斷 condition K+1 所有 condition 皆不成立則執行 suite N 程式碼 用多個 condition 來決定做 suite K!
if, elif, else ( 多選 1) 流程示意圖 Statement n Statement n if Condition1: suite 1 elif Condition2: suite 2 else: suite 3 True Cond 1 Suite 1 False True Cond 2 Suite 2 False Suite 3 Statement n+1 Statement n+1
if, elif, else 範例 ( 比大小 ) 1 2 3 4 5 6 7 8 9 10 11 12 13 #- *- coding:utf- 8 - *- # 範例程式 EX02_03.py # 輸入兩個數字比大小 num1 = int(input('please input a num1:')) num2 = int(input('please input a num2:')) if num1 == num2: print(num1,' 等於 ',num2) elif num1 < num2: print(num1,' 小於 ',num2) else: print(num1,' 大於 ',num2) 原始碼 : https://gist.github.com/chifu/f044779487741c829734 - file-ex02_03-py
if, elif, else 範例 # 計算 BMI 並輸出分級值 # BMI = 體重 (kg) / 身高 (m^2) 分級 身體質量指數 體重過輕 BMI < 18.5 正常範圍 18.5 BMI <24 過重 24 BMI < 27 輕度肥胖 27 BMI < 30 中度肥胖 30 BMI < 35 重度肥胖 BMI 35 原始碼 : https://gist.github.com/chifu/f044779487741c829734 - file-ex02_04-py 參考網頁 : http://www.scpo.nccu.edu.tw/show/part1/b/b2/bmi.htm
for 迴圈 for 迴圈是另外一個可以重複進行運算的結構, 以下是 for- in 的基本語法 forvariniterableobject : suite Iterable object 是指可迭代物件, 可以想像成這種物件裡面有可數的項目可依特定順序一個一個取出 Var 我們稱控制變數又或迴圈變數 for 迴圈的執行流程 自可迭代物件中取出一個項目, 代入至 Var 中 執行 suite 回到第一步直到可迭代物件中的項目盡皆取出 Ø 這種依次取出 ( 探訪 ) 並且進行代入的動作稱為迭代 當然, break 和 continue 也可以在 for 迴圈中出現
for 迴圈 以下是流程示意圖 Statement n Statement n for Var in iterableobject : suite Statement n+1 迴圈 Item in iterable object? True False Var= fetch one item from iterable object suite Statement n+1
for 迴圈範例 u 輸入一個數字 n, 計算 1+2+3+...+n 的總和為多少?
while 迴圈 當條件成立 (True) 時, 進行區塊 (suite) 運算 while Condition : suite 區塊執行完畢後, 再次檢查條件, 若依然成立則執行 suite 否則開始執行區塊之後的敘述 這種重複的結構我們稱為迴圈 不再繼續執行區塊的動作稱為跳出迴圈或離開迴圈
while 迴圈 以下是流程示意圖 Statement n Statement n while Condition : suite Statement n+1 迴圈 Condition True False suite Statement n+1
break 敘述與 continue 敘述 對於迴圈想要擁有更高的控制性 利用 break 在任何時候跳出迴圈 利用 continue 在任何時候略過迴圈 ( 略過本次迴圈剩餘的運算 ), 而這些時機點通常搭配選擇結構來進行 Statement n Condition True suite 遇到 continue 遇到 break Statement n+1
for/while 的使用時機 u u u 當需要重複進 行運算的時候使 用迴圈 (for/while) 當重複的次數可以清楚被計算或當迭代的表現明顯時使 用 for 迴圈 當重複的次數難以計算 ( 但條件清楚 ) 或是有條件的重複時使 用 while
額外的 else 敘述 重複結構 while 和 for 都支援額外的 else 敘述, 其語法如下 while Condition : while_suite else: else_suite for Var in Condition : for_suite else: else_suite 當 while 迴圈或 for 迴圈不是因為 break, return 或例外終止時 ( 指迴圈正常中止 ), else_suite 會被執行
巢狀結構 u 不論是 if/elif/else 結構,while 迴圈或 for 迴圈都 支援巢狀 ( 層疊式 ) 的撰寫, 各層之間的縮排務必清楚, 冒號也要記得加上 u u 雙重迴圈 ( 多重迴圈 ) 是程式中重要的結構, 是處理多層 ( 多軌 ) 迭代或是運算的必要 手段 多重迴圈的運 行次序可回到基本定義上想
homework 2 上傳連結 : https://goo.gl/zqt47x u 分別用 for,while 迴圈各寫 一個 nxn 的乘法表 程式 可以讀取使用者輸入的值 n, n>1 u 輸出樣式 : (n=3)