• 沒有找到結果。

Python 02

N/A
N/A
Protected

Academic year: 2021

Share "Python 02"

Copied!
25
0
0

加載中.... (立即查看全文)

全文

(1)

Python

程式設計

(2)

Outline

u

流程控制

u

迴圈

(3)

布林運算

u 有三種布林運算 and, or, not

運算 範例 結果

or 2==3 or 3 < 7 True

and 2==3 and 3 < 7 False

(4)

比較運算子

運算符號 描述 < 小於 <= 小於或等於 > 大於 >= 大於或等於 == 比較值是否相等 != 比較值是否不相等 is 比較是否同一個物件(id) is not 比較是否不同個物件(id)

(5)

邏輯運算

u

邏輯運算是針對真假值(布林值)的運算

u

布林型態只有兩種值:

True

False

u

布林語境(Boolean context) : 談論真假, 運算真

假的情境

u

在布林語境中, 0和任何的空資料代表False, 其

他代表True(通常True會 跟1連結), None在布林

語境中也是False

(6)

if (如果…就…)

l if是最單純的一種條件分支敘述句 l If 後⾯面接 condition,最後需要加上冒號: l 冒號:之後的下⼀一⾏行程式碼記得要縮排 l 當條件(condition)成⽴立時(True), 執⾏行冒號後⾯面的suite程式 碼, 若條件不成⽴立(False), 略過整個suite開始執⾏行suite之後 ⼀一⾏行程式碼 l ⽤用condition來選擇suite程式碼做或不做

if

Condition

:

suite

Condition是⼀一個完整 的敘述,並且python 會在布林語境中解讀 所以會是⼀一個真假敘 述,最常使⽤用的是⽐比較 運算式 縮排

(7)

if

• 以下是流程示意圖

Statement n

if

Condition

:

suite

Statement n+1

Statement n Condition suite Statement n+1 True False

(8)

if 範例

原始碼: https://gist.github.com/chifu/f044779487741c829734#file-ex02_01-py

1

2

3

4

5

6

#-­‐*-­‐coding:UTF-­‐8  -­‐*-­‐

#範例程式 EX02_01.py

#判斷 2的10次方是否等於1024

if

2**10

==

1024

:  

print

(

"2^10=1024"

)

(9)

if, else (2選1)

• 利用if/else敘述可以根據條件選擇執行區塊A或B • 用condition來選擇做suite A還是suite B

if

Condition

:

suite A

else :

suite B

• 當條件(condition)成立時(True),   執行if冒號後面的suite A程式 碼, 執行完後跳到suite B之後 一行敘述執行 • 若條件不成立(False), 則執行 suite B之程式碼, 執行完後跳 到suite B之後一行敘述執行

(10)

if, else

• 流程示意圖 Statement n Condition Suite A Statement n+1 True False

Statement n

if

Condition

:

suite A

else :

suite B

Statement n+1

Suite B

(11)

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

(12)

if, 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 程式碼

(13)

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 False

(14)

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) 原始碼:

(15)

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 ≧ 35

(16)

for

迴圈

• for迴圈是另外一個可以重複進行運算的結構, 以下是for-­‐in的基本語法 • Iterable object  是指可迭代物件, 可以想像成這種物件裡面有可數的項 目可依特定順序一個一個取出 • Var我們稱控制變數又或迴圈變數 • for迴圈的執行流程  自可迭代物件中取出一個項目, 代入至Var中 ‚ 執行suite ƒ 回到第一步直到可迭代物件中的項目盡皆取出 Ø 這種依次取出(探訪)並且進行代入的動作稱為迭代

• 當然, break 和 continue 也可以在 for 迴圈中出現

for

Var

in

iterableobject

:

suite

(17)

for

迴圈

• 以下是流程示意圖

Statement n

for

Var

in

iterableobject

:  

suite

Statement n+1

Statement n Item in iterable object? True

Var= fetch one item from   iterable object suite Statement n+1 False

迴圈

(18)

for

迴圈 範例

(19)

while 迴圈

• 當條件成立(True)時, 進行區塊(suite)運算 • 區塊執行完畢後, 再次檢查條件, 若依然成立則執行suite否則開始執行 區塊之後的敘述 • 這種重複的結構我們稱為迴圈 • 不再繼續執行區塊的動作稱為跳出迴圈或離開迴圈

while

Condition

:

suite

(20)

while 迴圈

• 以下是流程示意圖

Statement n

while

Condition

:

suite

Statement n+1

Statement n Condition suite Statement n+1 True False

迴圈

(21)

break敘述與continue敘述

• 對於迴圈想要擁有更高的控制性 • 利用break在任何時候跳出迴圈 • 利用continue在任何時候略過迴圈 (略過本次迴圈剩餘的運算) , 而這 些時機點通常搭配選擇結構來進 行 Statement n Condition Statement n+1 True 遇到break suite 遇到continue

(22)

for/while的使用時機

u 當需要重複進⾏行運算的時候使⽤用迴圈(for/while) u 當重複的次數可以清楚被計算或當迭代的表現明顯 時使⽤用for迴圈 u 當重複的次數難以計算(但條件清楚)或是有條件的重 複時使⽤用while

(23)

額外的else敘述

• 重複結構while和for都支援額外的else敘述, 其語法如下 • 當while迴圈或for迴圈不是因為break, return或例外終止時(指迴圈正常 中止), else_suite會被執行 while   Condition:   while_suite else: else_suite

for  Varin    Condition:   for_suite

else:

(24)

巢狀結構

u 不論是if/elif/else結構,while迴圈或for迴圈都⽀支援巢 狀(層疊式)的撰寫, 各層之間的縮排務必清楚, 冒號也要 記得加上 u 雙重迴圈(多重迴圈)是程式中重要的結構, 是處理多 層(多軌)迭代或是運算的必要⼿手段 u 多重迴圈的運⾏行次序可回到基本定義上想

(25)

homework 2

u 分別用for,while迴圈各寫⼀一個nxn的乘法表 程式 可以讀取使用者輸入的值 n, n>1 u 輸出樣式: (n=3) 上傳連結: https://goo.gl/zQT47x

參考文獻

相關文件

眼睛一眨,她就到了後面。最後,她直直地往 天上飛,飛到快看不見了,一轉身又直直的衝

求出 Select Case 運算式之值,並逐一與 Case 運算式值串列比對,若符合則執行該 Case 之後的敘述區段。1. 如果所有的

下手傳球是接發球, 後排防守, 保護的主要技術. 正面下手傳球是最 基本的傳球方法, 是各種傳球技術的基礎, 適合於接各種發球, 扣球 等.. ──

另外因為 Gal(L/F ) 是 Gal(L/K) 的 normal subgroup, 所以由 Second Fundamental Theorem 4.1.8 知 F/K 也是 Galois extension, 而且 Gal(F/K) isomorphic to Gal(L/K)/Gal(L/F )

A subgroup N which is open in the norm topology by Theorem 3.1.3 is a group of norms N L/K L ∗ of a finite abelian extension L/K.. Then N is open in the norm topology if and only if

2-1 註冊為會員後您便有了個別的”my iF”帳戶。完成註冊後請點選左方 Register entry (直接登入 my iF 則直接進入下方畫面),即可選擇目前開放可供參賽的獎項,找到iF STUDENT

(答案宜正反看法都能兼顧,如只單純提出一面的答案,最多得 3 分;能 同時指出兩面答案者,最多得 4 分。).. 4

Britain–s £50 Note Will Honor Computing Pioneer Alan Turing.. 盧政良 台大資訊系