Python
程式設計
Outline
u
流程控制
u
迴圈
布林運算
u 有三種布林運算 and, or, not
運算 範例 結果
or 2==3 or 3 < 7 True
and 2==3 and 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
if
Condition
:
suite
Statement n+1
Statement n Condition suite Statement n+1 True Falseif 範例
原始碼: https://gist.github.com/chifu/f044779487741c829734#file-ex02_01-py1
2
3
4
5
6
#-‐*-‐coding:UTF-‐8 -‐*-‐
#範例程式 EX02_01.py
#判斷 2的10次方是否等於1024
if
2**10
==
1024
:
(
"2^10=1024"
)
if, else (2選1)
• 利用if/else敘述可以根據條件選擇執行區塊A或B • 用condition來選擇做suite A還是suite Bif
Condition
:
suite A
else :
suite B
• 當條件(condition)成立時(True), 執行if冒號後面的suite A程式 碼, 執行完後跳到suite B之後 一行敘述執行 • 若條件不成立(False), 則執行 suite B之程式碼, 執行完後跳 到suite B之後一行敘述執行if, else
• 流程示意圖 Statement n Condition Suite A Statement n+1 True FalseStatement n
if
Condition
:
suite A
else :
suite B
Statement n+1
Suite Bif, 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
:
(num,
'是偶數'
)
else
:
(num,
'是奇數'
)
原始碼: https://gist.github.com/chifu/f044779487741c829734#file-ex02_02-pyif, elif, else (多選1)
• 當選擇超過兩種的時候使用if, elif(else if), else的語法來決定執行區塊A 或B或C… • 用多個condition來決定做suite K! if Condition 1: suite 1 elif Condition 2: suite 2 … elif Conditionk: suite K … else: suite N • 當條件(condition k)成立時 (True), 執行冒號後面的suite K 程式碼, 執行完後跳到suite N 之後一行敘述執行 • 若條件(condition k)不成立 (False), 則往下開始判斷 condition K+1 • 所有condition皆不成立則執行 suite N 程式碼
if, elif, else (多選1)
• 流程示意圖Statement n
if
Condition 1
:
suite 1
elif
Condition 2
:
suite 2
else:
suite 3
Statement n+1
Statement n Cond 1 Suite 1 Statement n+1 True False Suite 2 Cond 2 True Suite 3 Falseif, 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) 原始碼:
if, elif, else
範例
# 計算BMI並輸出分級值 # BMI = 體重 (kg) / 身高 (m^2) 參考網頁: http://www.scpo.nccu.edu.tw/show/part1/b/B2/bmi.htm 原始碼: https://gist.github.com/chifu/f044779487741c829734 - file-ex02_04-py 分 級 ⾝身體質量指數 體重過輕 BMI < 18.5 正常範圍 18.5 ≦ BMI <24 過 重 24 ≦ BMI < 27 輕度肥胖 27 ≦ BMI < 30 中度肥胖 30 ≦ BMI < 35 重度肥胖 BMI ≧ 35for
迴圈
• for迴圈是另外一個可以重複進行運算的結構, 以下是for-‐in的基本語法 • Iterable object 是指可迭代物件, 可以想像成這種物件裡面有可數的項 目可依特定順序一個一個取出 • Var我們稱控制變數又或迴圈變數 • for迴圈的執行流程 自可迭代物件中取出一個項目, 代入至Var中 執行suite 回到第一步直到可迭代物件中的項目盡皆取出 Ø 這種依次取出(探訪)並且進行代入的動作稱為迭代• 當然, break 和 continue 也可以在 for 迴圈中出現
for
Var
in
iterableobject
:
suite
for
迴圈
• 以下是流程示意圖
Statement n
for
Var
in
iterableobject
:
suite
Statement n+1
Statement n Item in iterable object? TrueVar= fetch one item from iterable object suite Statement n+1 False
迴圈
for
迴圈 範例
while 迴圈
• 當條件成立(True)時, 進行區塊(suite)運算 • 區塊執行完畢後, 再次檢查條件, 若依然成立則執行suite否則開始執行 區塊之後的敘述 • 這種重複的結構我們稱為迴圈 • 不再繼續執行區塊的動作稱為跳出迴圈或離開迴圈while
Condition
:
suite
while 迴圈
• 以下是流程示意圖Statement n
while
Condition
:
suite
Statement n+1
Statement n Condition suite Statement n+1 True False迴圈
break敘述與continue敘述
• 對於迴圈想要擁有更高的控制性 • 利用break在任何時候跳出迴圈 • 利用continue在任何時候略過迴圈 (略過本次迴圈剩餘的運算) , 而這 些時機點通常搭配選擇結構來進 行 Statement n Condition Statement n+1 True 遇到break suite 遇到continuefor/while的使用時機
u 當需要重複進⾏行運算的時候使⽤用迴圈(for/while) u 當重複的次數可以清楚被計算或當迭代的表現明顯 時使⽤用for迴圈 u 當重複的次數難以計算(但條件清楚)或是有條件的重 複時使⽤用while額外的else敘述
• 重複結構while和for都支援額外的else敘述, 其語法如下 • 當while迴圈或for迴圈不是因為break, return或例外終止時(指迴圈正常 中止), else_suite會被執行 while Condition: while_suite else: else_suitefor Varin Condition: for_suite
else: