Se ha denunciado esta presentación.
Se está descargando tu SlideShare. ×
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Próximo SlideShare
Python 迴圈作業
Python 迴圈作業
Cargando en…3
×

Eche un vistazo a continuación

1 de 32 Anuncio

Más Contenido Relacionado

Presentaciones para usted (20)

Anuncio

Más de 吳錫修 (ShyiShiou Wu) (20)

Más reciente (20)

Anuncio

Python分支作業

  1. 1. Python分支作業 Revised on August 16, 2021  條件運算式  使用關係運算子  使用邏輯運算子  使用 in 運算子  使用 is 運算子  選擇結構  單向選擇  雙向選擇  多向選擇  巢狀選擇
  2. 2.  在⽇常⽣活中,我們常常需要⾯臨⼀些抉擇,在某些情況下,決定做 什麼;或是不做什麼,例如:  如果天氣有些涼的話,出門需要加件衣服  如果下雨的話,出門需要拿把傘  如果颱風的話,就取消旅行  上述描述是人類的思考邏輯,轉換到程式語言,就是使用條件運算式 (conditional expressions) 來描述條件和執行運算  條件運算式的運算結果只有 2 種值  條件成立 → 真 (true)  條件不成立 → 假 (false) 條件運算式 1/8 2
  3. 3.  Python 語言可使用以下運算子來組成條件運算式  關係運算子  邏輯運算子  in 運算子  is 運算子  Python 布林 (bool) 資料型別所提供的值為 True 和 False,若是將布 林值進行整數運算時,True 會轉成 1,False 會轉成 0 條件運算式 2/8
  4. 4. 條件運算式 3/8 4  關係運算子 運算子 說明 實例 運算結果 == 左側運算元與右側運算元是否相等 4 + 2 == 1 + 5 True 15 == 3 False != 左側運算元與右側運算元是否不相等 2 * 3 != 3 * 2 False 7 != 9 True > 左側運算元是否大於右側運算元 12 > 10 True 6 * 2 > 3 * 4 False < 左側運算元是否小於右側運算元 8 < 9 True 3 < 9 – 6 False >= 左側運算元是否大於或等於右側運算元 12 >= 10 True 6 * 2 >= 3 * 4 True <= 左側運算元是否小於或等於右側運算元 8 <= 9 True 3 <= 9 - 6 True
  5. 5.  測試關係運算式 i = 'A' > 'B' #False j = ((5 + i) == 5) #True k = 5 + (100 < 50) * 3 + (-20 != 20) * 2 #7 print('以字串顯示:') print('i = %s' % i) #i = False print('j = %s' % j) #j = True print('k = %s' % k) #k = 7 print('以整數顯示:') print('i = %d' % i) #i = 0 print('j = %d' % j) #j = 1 print('k = %d' % k) #k = 7 條件運算式 4/8 5
  6. 6.  邏輯運算子可用來連接其它運算式,組合成較複雜的條件式  not 運算的回傳值為布林值,但 and 與 or 運算的回傳值則不⼀定是布林值  and 運算時,如果第⼀個運算元為 False,就直接回傳第⼀個運算元,⽽不 會再處理第二個運算元  or 運算時,如果第⼀個運算元為 True,就直接回傳第⼀個運算元,⽽不 會再處理第二個運算元  數值資料只要不是整數 0 就是 True,字串不是空字串就是 True 條件運算式 5/8 6 運算子 語法 執行結果 說明 and A and B 當A為False時回傳A;否則回傳B 只有A為True時,B才會被執行 or A or B 當A為True時回傳A;否則回傳B 只有A為False時,B才會被執行 not not A 當A為True時回傳False;否則回傳True not之優先序比非邏輯運算低 not A==B執行順序為not(A==B)
  7. 7.  溫度 (temperature) 高於 30 度⽽且不超過 38 度的條件式寫法 (temperature > 30) and (temperature <= 38)  分數 (score) 必須介於 0~100 之間,則無效分數的條件式寫法 (score < 0) or (score > 100)  測試邏輯運算式 x = 8 y = 3 print(y == 3 and x < 3) #False print(x + y == 11 or y > 8) #True print(not (x > 0 and y > 0 or 'A' > 'C')) #False print(not 2) #False print(2 and 3) #3 (回傳第2個運算元) print(2 or 3) #2 (回傳第1個運算元) print('a' or 'b') #a (回傳第1個運算元) print(0 and 3) #0 (回傳第1個運算元) print('' or 'b') #b (回傳第1個運算元) 條件運算式 6/8 7
  8. 8.  in 稱為成員運算子 (Membership operator),用來判斷第⼀個運算元 是否為第二個運算元的元素,若是就回傳True;否則回傳 False。  not in 運算子用來判斷第⼀個運算元是否不屬於第二個運算元的元素  第二個運算元為字串、串列...等物件 print('P' in 'Python') #True print('x' not in 'Python') #True print(1 in [1, 2, 3]) #True print(2 not in [1, 2, 3]) #False 註:[1, 2, 3]為串列資料,在後續單元介紹 條件運算式 7/8 8
  9. 9.  is 稱為身分運算子 (Identity operator),用來判斷兩運算元的 id 是否相 同,若是就回傳 True;否則回傳 False。所以 x is y 敘述,就等於 id(x) == id(y) 敘述  not is 運算子用來判斷兩運算元的 id 是否不相同  要特別注意,is 運算子是用來判斷兩運算元是否引用⾃同⼀個物件,⽽ == 運算子則是判斷兩運算元的值是否相同 x = 2.5; y = 2.5 print(id(x), id(y)) #2081398578096 2081399329328 (隨執行環境變動) print(x is y, x == y) #False True z = x print(id(z)) #2081398578096 print(z is x, z == x) #True True 條件運算式 8/8 9
  10. 10.  Python 的 if 選擇結構敘述如下  單向選擇: if …  雙向選擇: if … else …  多向選擇: if … elif … else  巢狀選擇 選擇結構 1/9
  11. 11.  單向選擇 if… if (條件式): 執行區塊  if敘述要以「:」冒號為結尾  選擇區塊要向右縮排 4 個空白字元  求 num 的絕對值 if (num < 0): num = -num  成績在 55 分以上未達 60 分者,以 60 分計分 score = int(input('score:')) if (score >= 55) and (score < 60): score = 60 print('加分後勉予及格') 選擇結構 2/9 True if(條件式): 區塊 False
  12. 12.  縮排的重要性 score = int(input('score:')) if (score >= 55) and (score < 60): score = 60 print('加分後勉予及格') #永遠都會執行 score = int(input('score:')) if (score >= 55) and (score < 60): score = 60 print('加分後勉予及格') #只有score大於54且小於60時,才會執行 選擇結構 3/9 12
  13. 13.  雙向選擇 if…else… if (條件式): 執行區塊⼀ else: 執行區塊二  門票 300 元,未滿 10 歲兒童或 65 歲以上⻑者半價 age = int(input('age:')) if (age < 10) or (age >= 65): price = 150 else: price = 300 print('price = ', price) 選擇結構 4/9 13 True if(條件式): 區塊1 False 區塊2
  14. 14.  多向選擇if…elif…else if (條件式1): 執行區塊1 elif (條件式2): 執行區塊2 ... elif (條件式N): 執行區塊N else: 執行區塊N+1 選擇結構 5/9 if(條件式1): True if(條件式2): False True if(條件式N): False True 區塊1 區塊2 區塊N 區塊N+1
  15. 15.  設計程式,依據輸入的分數,顯示成績的等級及評語 測試: 輸入研習的成就分數:73 研習成績 "C" 評語:差強人意 選擇結構 6/9 15 分數 90-100 80-89 70-79 60-69 0-59 等級 A B C D F 評語 成就非凡 表現良好 差強人意 仍須努力 待加強
  16. 16. score = int(input('請輸入研習的成就分數:')) if (score >= 90 and score <= 100) : print('研習成績 "A"') print('評語:成就非凡') elif (score >= 80 and score <= 89) : print('研習成績 "B"') print('評語:表現良好') elif (score >= 70 and score <= 79) : print('研習成績 "C"') print('評語:差強人意') elif (score >= 60 and score <= 69) : print('研習成績 "D"') print('評語:仍須努力') elif (score >= 0 and score <= 59) : print('研習成績 "F"') print('評語:待加強') else: print('輸入錯誤!') print('分數須在0~100之間') 選擇結構 7/9 16
  17. 17.  巢狀選擇if…else…  是指在if或else的程式區塊裡,還有if…或if…else…選擇結構 if (條件式1): if (條件式2): 執行區塊1 else: 執行區塊2 else: if (條件式3): 執行區塊3 else: 執行區塊4 選擇結構 8/9 if(條件式1): True False if(條件式2): True False if(條件式3): True False 區塊4 區塊3 區塊2 區塊1
  18. 18.  給3個整數,使用巢狀選擇結構找出最大值 n1 = int(input('n1:')) n2 = int(input('n2:')) n3 = int(input('n3:')) print(f'三個整數分別為 {n1}, {n2}, {n3}') if (n1 > n2): #判斷 n1 是否大於 n2 if (n1 > n3): #判斷 n1 是否大於 n3 max = n1 else: max = n3 else: if(n2 > n3): #判斷 n2 是否大於 n3 max = n2 else: max = n3 print() print(f'比較結果:最大數為 {max}') 選擇結構 9/9
  19. 19.  下列運算式的結果為何? A. 0 or 5 B. bool(0) C. bool(-1) D. None is None E. -5<0<5 A. 5 B. False C. True D. True E. True 自我評量 1/4
  20. 20.  下列條件式的運算結果,何者為False? A. 15 > 13 B. 'c' < 'f' C. 'a' == 97 D. (90 / 3 > 100) or (30 * 3 != 120) 自我評量 2/4 20
  21. 21.  優待票的年齡條件為低於15歲或滿65歲,其條件的寫法為何? A. age < 15 and age >= 65 B. age < 15 or age >= 65 C. age >= 15 or age < 65 D. age >= 15 and age < 65 自我評量 3/4 21
  22. 22.  執行下列程式區段,a值結果為何? a = 8 b = 0 if (a >= 6): b = 9 if (b < 6): a = -2 A. 9 B. 6 C. 8 D. -2 自我評量 4/4 22
  23. 23.  使用巢狀if…else結構,設計程式判斷輸入非零的整數為正數或負數, 以及奇數或偶數的程式 提示:  大於零的數為正數  能被2整除的整數為偶數 測試: 輸入⼀個非零的整數: -23 所輸入的整數為 負奇數 實作練習I 1/2 23
  24. 24.  參考程式 num = int(input('輸入⼀個非零的整數:')) if (num > 0) : if (num % 2 == 0) : data = '正偶數' else : data = '正奇數' else : if (num % 2 == 0) : data = '負偶數' else : data = '負奇數' print('所輸入的整數為 ' + data) 實作練習I 2/2 24
  25. 25.  設計程式,輸入月份值,輸出季節名稱  春天:3、4、5月  夏天:6、7、8月  秋天:9、10、11月  冬天:12、1、2月 測試: 輸入⼀個 1~12 之間的月份: 4 春天 實作練習II 1/2 25
  26. 26.  參考程式 month = int(input('請輸入⼀個 1~12 之間的月份:')) if (month==3 or month==4 or month==5) : print('春天') elif (month==6 or month==7 or month==8) : print('夏天') elif (month==9 or month==10 or month==11) : print('秋天') elif (month==12 or month==1 or month==2) : print('冬天') else: print('輸入錯誤!') 實作練習II 2/2 26
  27. 27.  設計程式,輸入綜合所得淨額,計算所得稅稅額 測試: 輸入綜合所得淨額:1250000 所得稅額是:115400.0 實作練習III 1/2 27 綜合所得淨額 稅率 累進差額 0~540,000 5% 0 540,001~1,210,000 12% 37,800 1,210,001~2,420,000 20% 134,600 2,420,001~4,530,000 30% 376,600 4,530,000~以上 40% 829,600
  28. 28.  參考程式 income = int(input('輸入綜合所得淨額:')) if (income <= 540000): tax = 0.05 * income if ((income > 540000) and (income <= 1210000)): tax = 0.12 * income - 37800 if ((income > 1210000) and (income <= 2420000)): tax = 0.2 * income - 134600 if ((income > 2420000) and (income <= 4530000)): tax = 0.3 * income - 376600 if (income > 4530000): tax = 0.4 * income - 829600 print('所得稅額是:%.1f'%tax) 實作練習III 2/2 28
  29. 29.  撰寫程式,輸入空氣品質指數,輸出空氣品質等級 測試: 輸入AQI(>=0):120 空氣品質:對敏感族群不健康 實作練習IV 1/2 29
  30. 30.  參考程式 aqi = float(input('輸入AQI(>=0):')) if (aqi <= 50): print("空氣品質:良好") elif (aqi <= 100): print("空氣品質:普通") elif (aqi <= 150): print("空氣品質:對敏感族群不健康") elif (aqi <= 200): print("空氣品質:對所有族群不健康") elif (aqi <= 300): print("空氣品質:非常不健康") else: print("空氣品質:危害") 實作練習IV 2/2 30
  31. 31.  寫⼀程式,輸入用電度數,輸出非夏月電費 測試: 輸入用電度數(>=0):1005 電費 = 3321元 實作練習V 1/2 31
  32. 32.  參考程式 consumption = float(input('輸入用電度數(>=0):')) bill1 = 120 * 1.63 bill2 = bill1 + 210 * 2.1 bill3 = bill2 + 170 * 2.89 bill4 = bill3 + 200 * 3.94 bill5 = bill4 + 300 * 4.60 if (consumption <= 120): bill = consumption * 1.63 elif (consumption <= 330): bill = bill1 + (consumption - 120) * 2.1 elif (consumption <= 500): bill = bill2 + (consumption - 330) * 2.89 elif (consumption <= 700): bill = bill3 + (consumption - 500) * 3.94 elif (consumption <= 1000): bill = bill4 + (consumption - 700) * 4.60 else: bill = bill5 + (consumption - 1000) * 5.03 print('電費 = %.0f' %bill) 實作練習V 2/2 32

×