--- title: 程式語言與設計 (上) description: 旗立 程式語言與設計(上),技審字第110011號,https://trinket.io/,https://www.pythonanywhere.com/ tags: python --- # Chapter 3 資料型別與資料運算 ## 3-1程式組成與語法操作 &emsp; &emsp; --- **:pig:編碼** 使用UTF-8 :::warning #-\*- coding: utf-8-\*- ::: &emsp; &emsp; **:pig:縮排** ![](https://i.imgur.com/ouGOx3r.png) <iframe src="https://trinket.io/embed/python/1f27f79095" width="100%" height="250" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; 縮排錯誤提示文字: :::warning expected an in dented block ::: &emsp; &emsp; 縮排也表示程式要執行的範圍 ![](https://i.imgur.com/9epuyI3.png) <iframe src="https://trinket.io/embed/python/e1baa3c05b" width="100%" height="250" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; --- **:pig:註解** 單列註解 ,加「#」 ```python=1 #從鍵盤輸入溫度,轉成整數 temp = int (input('請輸入溫度')) ``` 多行註解,頭尾加上三個「\`\`\`」 ```python=1 ‵‵‵使用if來判斷溫度 溫度是否大於28度‵‵‵ if temp > 28 : : : ``` --- **:pig:變數與檔案命名規則** 變數:會隨著程式執行過程中,而改變的物件。 1. 第1個字是英文字母或是底線 2. 中間不可以有空格 3. 可以用底線「_」,不可以用減號「-」 4. 不可用特殊符號,如:%、#、@…等 5. 不建議用中文。(但中文是可以用的) 6. 大小寫不一樣,如:Hello 不等同 hello 7. 避免用「1、0、l、O」 8. 不可以用保留字 ![](https://i.imgur.com/DcqSJYC.png) ![](https://i.imgur.com/YodFfmR.png) :dog:範列3-1 <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> --- &emsp; ## 3-2 資料的型別 **:pig:python 內建的三類資料型別** 1. 數值(numeric) 2. 字串(string) 3. 容器(container) ![](https://i.imgur.com/k6HKxcv.png) &emsp; &emsp; --- **:pig: 變數的產生** 1. 偵測其值的資料型別 2. 新增物件實體(變數) 3. 配置記憶體位置 4. 指派值給變數 ![](https://i.imgur.com/2M4fTyd.png) --- &emsp; :::warning :pig:**數值型別** ::: 1. 整數(integer,int) 2. 浮點數(float) 3. 布林(boolean,bool) 4. 二進位(binary) 5. 八進位(octal) 6. 十六進位(hexadecimal) ![](https://i.imgur.com/ALvbbWY.png) :dog:範例3-2 <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; 設定變數 data示意圖 ![](https://i.imgur.com/n0eD3Pz.png) ![](https://i.imgur.com/EjUkS79.png) &emsp; 更改變數 data內容值的示意圖 ![](https://i.imgur.com/yhOSV2H.png) ![](https://i.imgur.com/Hzvg7Tq.png) &emsp; &emsp; &emsp; &emsp; :::warning **:pig:字串型別** ::: **:dog:字串相加及複製** ![](https://i.imgur.com/FpwXdrv.png) ![](https://i.imgur.com/vYtlRYB.png) :duck:範例3-5 <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; --- &emsp; :::info [高中生程式解題系統 d483: hello, world](https://zerojudge.tw/ShowProblem?problemid=d483) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ::: :::info [高中生程式解題系統 a001: 哈囉](https://zerojudge.tw/ShowProblem?problemid=a001) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ::: &emsp; &emsp; **:dog:取出字串的定位方式** 字串是有序列的資料型別,它的內容值都是有索引值編號的。 ![](https://i.imgur.com/tKam27Y.png) :::warning 變數[開始位置:結束位置:間隔] ::: ![](https://i.imgur.com/t9m6ZBX.png) :duck:範例3-5 <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; ::: info :duck: [高中生程式解題系統 a038: 數字翻轉](https://zerojudge.tw/ShowProblem?problemid=a038) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ::: &emsp; &emsp; **:dog:字串的方法** :::warning 變數名稱.方法名稱(相關參數) ::: &emsp; &emsp; - 大小寫轉換方法 ![](https://i.imgur.com/1fJFB2u.png) ![](https://i.imgur.com/eAGska2.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; ::: info :duck:動動腦 - 身分證字母轉大寫,為了防止民眾在台中購物節中,輸入身分證字號時,將身分證的第一個英文字母輸入小寫,造成系統辨識錯誤,請寫一個程式:不論民眾輸入的是大寫、或小寫字母,都一律轉成大寫字母。 <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ::: &emsp; &emsp; - 置換、尋找方法 ![](https://i.imgur.com/NeGxkHS.png) ![](https://i.imgur.com/PGrDuu8.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; ::: info :duck:[高中生程式解題系統 c760: 蝸牛老師的點名單](https://zerojudge.tw/ShowProblem?problemid=c760) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ::: &emsp; &emsp; - 判斷字串 ![](https://i.imgur.com/QMVVsJs.png) ![](https://i.imgur.com/InTwB2z.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; :::warning **:pig:容器** ::: **python常見的容器** 1. 串列(list) 2. 元組(tuple) 3. 集合(set) 4. 字典(dict) &emsp; &emsp; **:dog:串列(list)** 1. 串列是有順序的、內容可變的。 2. 串列裏面放的資料叫做「元素」 3. 每個元素有編號,叫做索引值(index). 4. 索引值從0開始編號。 5. 使用[ ] 6. 串列可以放不同型別的資料。 7. 串列中的每個元素長度可變、內容可變。 - 建立內容 ![](https://i.imgur.com/dus74uG.png) ![](https://i.imgur.com/3d71R3U.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; &emsp; - 串列型別專屬的方法: ![](https://i.imgur.com/0LfLLkd.png) - 新增串列 ![](https://i.imgur.com/x4wUAKm.png) ![](https://i.imgur.com/BIwRjkM.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; - 移除串列 ![](https://i.imgur.com/HyyvBE3.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; :dog:元組(tuple) - tuple 元組也是序列型別,有索引值。 - 不可變。 - 使用 ( ) - 內容用「,」隔開。 - 與list串列之比較 ![](https://i.imgur.com/v8FdeZR.png) ![](https://i.imgur.com/RajjTLy.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; :dog:集合(set) - 集合內「無順序性」, 無索引值。 ![](https://i.imgur.com/koFKXrM.png) - 集合內的元素不重覆 - 內容是可變的。 - 內容可以包含各種不同的資料型別。 - 使用 { } - 內容不可以儲存set、list、dict - 內容用「,」隔開。 ![](https://i.imgur.com/0NuqsLT.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; - 集合的方法 ![](https://i.imgur.com/OWckjNN.png) ![](https://i.imgur.com/p68cgUT.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; :::info :duck:動動腦 輸入這一段英文句子「The quick brown fox jumps over the lazy dog」,請算出裏面不重複的字母共有幾個! 提示1:記得將句子中的大寫轉換成小寫。 提示2:set(字串),可以將字串轉成集合。 提示3:remove(" "),可以移除集合內的空白 提示4:len(集合),可以計算集合內的個數,如:len({1,2,3})=3 <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ::: - 集合的數學運算 ![](https://i.imgur.com/PzeoqDl.png) ![](https://i.imgur.com/PRrtVVc.png) ![](https://i.imgur.com/6kIspw9.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; &emsp; &emsp; :dog:字典(dict) 1. 字典無順序 2. 字典的內容是「鍵:值」對。(key:value) 3. 鍵是唯一 4. 鍵必須為不可變的資料型別 &emsp; ![](https://i.imgur.com/H5m8vFH.png) - 字典的方法 ![](https://i.imgur.com/lrUINbA.png) &emsp; &emsp; - 新增 ![](https://i.imgur.com/zEGaQnb.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; - 取值 ![](https://i.imgur.com/z2Uylyg.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; - 更新 ![](https://i.imgur.com/tZVuh9W.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; - 複製、移除、清除 ![](https://i.imgur.com/rRpYLzY.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="500" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; - 集合與字典之比較 ![](https://i.imgur.com/ZY3uoUG.png) &emsp; &emsp; &emsp; ## 3-3 資料運算與處理 &emsp; &emsp; - 程式中的計算 1. 「算術」運算 2. 「關係」運算 3. 「邏輯」運算 &emsp; &emsp; &emsp; - 算術運算 ![](https://i.imgur.com/MHJFzyX.png) ![](https://i.imgur.com/n5bvOtY.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; - 關係運算 ![](https://i.imgur.com/haSzxIn.png) ![](https://i.imgur.com/lzBEmxI.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="200" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; - 邏輯運算 ![](https://i.imgur.com/WJ5KvWW.png) | 邏輯值 | 意義 | 計算時替換值 | |:------:|:----------------:|:------------:| | True |是、不是、真的| 1 | | False |否、不對、假的| 0 | &emsp; - Or (或),邏輯加法 | A | B | A or B | |:---:|:---:|:------:| | 0 | 0 | 0 | | 0 | 1 | 1 | | 1 | 0 | 1 | | 1 | 1 | 1 | &emsp; - And (且),邏輯減法 | A | B |A and B | |:---:|:---:|:------:| | 0 | 0 | 0 | | 0 | 1 | 0 | | 1 | 0 | 0 | | 1 | 1 | 1 | &emsp; - not (相反) | A | not A | |:---:|:-----:| | 1 | 0 | | 0 | 1 | ![](https://i.imgur.com/qJfzbhX.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; - 複合運算 ![](https://i.imgur.com/mrpU64c.png) ![](https://i.imgur.com/bHlG7i9.png) ![](https://i.imgur.com/piKsK8G.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> - 運算子的優先順序 ![](https://i.imgur.com/43layUI.png) **<U>當優先順序相同時,會由左往右依序計算</U>** &emsp; &emsp; :::info :dog:優先順序相同時,從左邊向右邊:arrow_right:計算。例如: <font size=50 >[6/2*(1+2)=?](https://youtu.be/EWhCQqhthWg) </font> ::: &emsp; &emsp; &emsp; &emsp; - 內建函數 1. 系統 2. 資料型別轉換 3. 資料運算與處理 4. 格式化 5. 容器 ![](https://i.imgur.com/V7mbfhL.png) &emsp; &emsp; &emsp; - 系統函數 ![](https://i.imgur.com/V0Qt98r.png) ![](https://i.imgur.com/jsVX4vR.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; - 資料型別轉換 ![](https://i.imgur.com/2Ga49iE.png) ![](https://i.imgur.com/0L9rjbH.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ![](https://i.imgur.com/BgYFTIH.png) &emsp; &emsp; &emsp; - 資料運算與處理 ![](https://i.imgur.com/gfls1nd.png) ![](https://i.imgur.com/OjEiZfD.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="500" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; - 格式化 ![](https://i.imgur.com/uh5UR9a.png) ![](https://i.imgur.com/0h7y601.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="500" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; - print( )函數 ![](https://i.imgur.com/3jnIPo0.png) ![](https://i.imgur.com/BPgJaX5.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; ### TQC 101 整數格式化輸出 ![](https://i.imgur.com/IHfTnnI.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="250" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ![](https://i.imgur.com/mQ35aEa.png) **註解** :::info :one: 「 | 符號」 在Enter鍵上方,先按住shift 後再按即可。 >[color=#fff] :two: input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。 >[color=#fff] >1. x = input('請輸入年齡') 當使用者輸入28時,則 x = 28。但這個28是屬於「文字」資料。 >2. 而「文字」資料,不能用來作 :heavy_plus_sign: :heavy_minus_sign: :heavy_multiplication_x: :heavy_division_sign: ... 等的數學運算哦! :three: int( )是數值函數,幫你將「文字」轉換成「整數」(正負數的整數值)。 >[color=#fff] > > 因為input( )所得的都是「文字」資料,所以要使用eval( )轉換成數值資料,才能夠用在計算上。 :four: print( )用於輸出資料。輸出「數值資料」的格式設定: >[color=#fff] >1. 「%」 宣告 格式設定 >2. 「5」表示 5個字。不足5個字,就填空格。 >3. 「d」表示 10進位整數 >4. 「-」符號表示資料靠左對齊 「沒有 -」符號表示資料靠右對齊(預設情形) >5. 格式設定與變數位置,相對應如下圖:![](https://i.imgur.com/85Rxq33.png) > ::: &emsp; &emsp; &emsp; ### TQC 102 浮點數格式化輸出 ![](https://i.imgur.com/m1xok9K.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ![](https://i.imgur.com/pHZGwsX.png) **註解** :::info :one: 「 | 符號」 在Enter鍵上方,先按住shift 後再按即可。 >[color=#fff] :two: input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。 >[color=#fff] >1. x = input('請輸入年齡') 當使用者輸入28時,則 x = 28。但這個28是屬於「文字」資料。 >2. 而「文字」資料,不能用來作 :heavy_plus_sign: :heavy_minus_sign: :heavy_multiplication_x: :heavy_division_sign: ... 等的數學運算哦! :three: float( )是浮點函數,幫你將「文字」轉換成「帶小數的數值」。 >[color=#fff] > > 因為input( )所得的都是「文字」資料,所以要使用float( )轉換成帶小數數值資料,才能夠用在計算上。 :four: print( )用於輸出資料。輸出「數值資料」的格式設定: >[color=#fff] >1. 「%」 宣告 格式設定 >2. 「7」表示 全部限7個字,而且包含小數點及小數位數。不足7個字,就填空格。 >3. 「.2」表示 保留小數2位,不足補0。超過小數2位時,4捨5入到小數第2位。 >3. 「f」表示 浮點數,就是帶有小數的數值 >4. 「-」符號表示資料靠左對齊 「沒有 -」符號表示資料靠右對齊(預設情形) >5. 格式設定與變數位置,相對應如下圖:![](https://i.imgur.com/Z2ilvlD.png) > ::: &emsp; &emsp; &emsp; ### TQC 103 字串格式化輸出 ![](https://i.imgur.com/GdW9WVJ.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> **註解** :::info :one: 「 | 符號」 在Enter鍵上方,先按住shift 後再按即可。 >[color=#fff] :two: input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。 >[color=#fff] >1. x = input('請輸入年齡') 當使用者輸入28時,則 x = 28。但這個28是屬於「文字」資料。也稱為「字串」資料 >2. 而「文字」資料,不能用來作 :heavy_plus_sign: :heavy_minus_sign: :heavy_multiplication_x: :heavy_division_sign: ... 等的數學運算哦! :three: print( )用於輸出資料。輸出「文字資料」的格式設定: >[color=#fff] >1. 「%」 宣告 格式設定 >2. 「10」表示 全部限10個字。不足10個字,就填空格。 >3. 「s」表示 文字資料,也稱字串資料 >4. 「-」符號表示資料靠左對齊 「沒有 -」符號表示資料靠右對齊(預設情形) >5. 格式設定與變數位置,相對應如下圖:![](https://i.imgur.com/VniH2xP.png) > ::: &emsp; &emsp; &emsp; - 容器資料型別轉換 ![](https://i.imgur.com/Mdbff2h.png) ![](https://i.imgur.com/DQFO5m8.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; ## 3-4 模組簡介與應用 - 寫好的函數放在模組中 - 使用時,用import匯入程式中使用 例:random、numpy模組,要匯入時: ```python =1 import random ``` ![](https://i.imgur.com/3Ze1iei.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="200" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; - 匯入的模組可以取「別名」(像是「匿稱」、「小名」、「簡稱」之類的…) 底下程式就是將「random」匯入程式後,叫它匿稱「rd」 ```python =1 import random as rd num = rd.uniform(0,1) ``` ![](https://i.imgur.com/XrN9pvy.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="200" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; ![](https://i.imgur.com/SkeLBfE.png) ![](https://i.imgur.com/GIbmY5u.png) ![](https://i.imgur.com/orPf2vb.png) ![](https://i.imgur.com/FLgGBlU.png) ![](https://i.imgur.com/BokNiBN.png) ![](https://i.imgur.com/NR79hAq.png) --- TQC程式設計檢定題目 --- ### 104 圓形面積計算 **題目** 請撰寫一程式,輸入一圓的半徑,並加以計算此圓之面積和周長,最後請印出此圓的半徑(Radius)、周長(Perimeter)和面積(Area)。 >提示1:需import math模組,並使用math.pi。 >提示2:輸出浮點數到小數點後第二位。 **輸入1** <pre> 10 </pre> **輸出1** <pre> Radius = 10.00 Perimeter = 62.83 Area = 314.16 </pre> **輸入2** <pre> 2.5 </pre> **輸出2** <pre> Radius = 2.50 Perimeter = 15.71 Area = 19.63 </pre> **程式** ```python=1 import math r = float(input()) print('Radius = %.2f' %r) print('Perimeter = %.2f' %(2 * math.pi * r)) print('Area = %.2f' %(math.pi * r ** 2)) ``` **註解** :::info :one: import 是匯入外部模組的敘述 >[color=#fff] > :two: math 是數學功能模組,「math.pi」是圓周率值3.141592... >[color=#fff] :three: input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。 >[color=#fff] >1. x = input('請輸入體重') 當使用者輸入63.4時,則 x = '63.4'。但這個'63.4'是屬於「文字」資料。也稱為「字串」資料 >2. 而「文字」資料,不能用來作 :heavy_plus_sign: :heavy_minus_sign: :heavy_multiplication_x: :heavy_division_sign: ... 等的數學運算哦! :four: float() 用於將「文字資料」轉換成「浮點數資料」,浮點數就是帶有小數的資料。  >[color=#fff] :five: print( )用於輸出資料。輸出「文字資料」的格式設定: >[color=#fff] >1. 「' '」符號內的文字直接輸出。 >2. 「%」 宣告 格式設定 >3. 「.2」表示 不限全部的文字個數,但是小數限2位數。 >4. 「f」表示 浮點數,就是帶有小數的數值 >5. 格式設定與變數位置,相對應如下圖:![](https://i.imgur.com/24OQuHi.png) :six: 運算符號 >[color=#fff] > >| 運算 | 符號 | 範列 |計算優先順序| >| --- | ----| --------|--------| >| 加 | + | 1+1 =2 |3| >| 減 | - | 3-2 =1 |3| >| 乘 | * | 2\*3 =6 |2| >| 除 | / | 2/5 =0.4 |2| >| 次方 | ** | 2\*\*2=4 |1| > > >註1:優先順序相同時,從左邊向右邊:arrow_right:計算。例如:6/2*(1+2)=[?](https://youtu.be/EWhCQqhthWg) >註2:在python中『2^2^』寫成『2**2』 ::: **執行** :::warning <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ::: --- ### 105 矩形面積計算 **題目** 請撰寫一程式,輸入兩個正數,代表一矩形之寬和高,計算並輸出此矩形之高(Height)、寬(Width)、周長(Perimeter)及面積(Area)。 >提示:輸出浮點數到小數點後第二位。 **輸入** <pre> 23.5 19 </pre> **輸出** <pre> Height = 23.50 Width = 19.00 Perimeter = 85.00 Area = 446.50 </pre> **程式** ```python=1 h = float(input()) w = float(input()) print('Height = %.2f' %h) print('Width = %.2f' %w) print('Perimeter = %.2f' %((h + w) * 2)) print('Area = %.2f' %(h * w)) ``` **註解** :::info :one: input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。 >[color=#fff] >1. x = input('請輸入體重') 當使用者輸入63.4時,則 x ='63.4'。但這個63.4是屬於「文字」資料。也稱為「字串」資料 >2. 而「文字」資料,不能用來作 :heavy_plus_sign: :heavy_minus_sign: :heavy_multiplication_x: :heavy_division_sign: ... 等的數學運算哦! :two: float() 用於將「文字資料」轉換成「浮點數資料」,浮點數就是帶有小數的資料。 >[color=#fff] :three: print( )用於輸出資料。輸出「文字資料」的格式設定: >[color=#fff] >1. 「' '」符號內的文字直接輸出。 >2. 「%」 宣告 格式設定 >3. 「.2」表示 不限全部的文字個數,但是小數限2位數。 >4. 「f」表示 浮點數,就是帶有小數的數值 >5. 格式設定與變數位置,相對應如下圖:![](https://i.imgur.com/3mWN1lg.png) > :four: 運算符號 >[color=#fff] > >| 運算 | 符號 | 範列 |計算優先順序| >| --- | ----| --------|--------| >| 加 | + | 1+1 =2 |3| >| 減 | - | 3-2 =1 |3| >| 乘 | * | 2\*3 =6 |2| >| 除 | / | 2/5 =0.4 |2| >| 次方 | ** | 2\*\*2=4 |1| > > >註1:優先順序相同時,從左邊向右邊:arrow_right:計算。例如:6/2*(1+2)=[?](https://youtu.be/EWhCQqhthWg) >註2:在python中『2^2^』寫成『2**2』 > ::: **執行** :::warning <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ::: --- ### 106 公里英哩換算 **題目** 假設一賽跑選手在x分y秒的時間跑完z公里,請撰寫一程式,輸入x、y、z數值,最後顯示此選手每小時的平均英哩速度(1英哩等於1.6公里)。 >提示:輸出浮點數到小數點後第一位。 **輸入** <pre> 10 25 3 </pre> **輸出** <pre> Speed = 10.8 </pre> **程式** ```python=1 x_min = float(input()) y_sec = float(input()) z_km = float(input()) print('Speed = %.1f' %((z_km / 1.6) / ((y_sec / 60 + x_min) / 60))) ``` **註解** :::info :one: input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。 >[color=#fff] >1. x = input('請輸入體重') 當使用者輸入63.4時,則 x ='63.4'。但這個63.4是屬於「文字」資料。也稱為「字串」資料 >2. 而「文字」資料,不能用來作 :heavy_plus_sign: :heavy_minus_sign: :heavy_multiplication_x: :heavy_division_sign: ... 等的數學運算哦! :two: float() 用於將「文字資料」轉換成「浮點數資料」,浮點數就是帶有小數的資料。 >[color=#fff] :three: print( )用於輸出資料。輸出「文字資料」的格式設定: >[color=#fff] >1. 「' '」符號內的文字直接輸出。 >2. 「%」 宣告 格式設定 >3. 「.1」表示 不限全部的文字個數,但是小數限1位數。 >4. 「f」表示 浮點數,就是帶有小數的數值 >5. 格式設定與變數位置,相對應如下圖:![](https://i.imgur.com/l81b48B.png) > :four: 運算符號 >[color=#fff] > >| 運算 | 符號 | 範列 |計算優先順序| >| --- | ----| --------|--------| >| 加 | + | 1+1 =2 |3| >| 減 | - | 3-2 =1 |3| >| 乘 | * | 2\*3 =6 |2| >| 除 | / | 2/5 =0.4 |2| >| 次方 | ** | 2\*\*2=4 |1| > > >註1:優先順序相同時,從左邊向右邊:arrow_right:計算。例如:6/2*(1+2)=[?](https://youtu.be/EWhCQqhthWg) >註2:在python中『2^2^』寫成『2**2』 > ::: **執行** :::warning <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ::: --- ### 107 數值計算 **題目** 請撰寫一程式,讓使用者輸入五個數字,計算並輸出這五個數字之數值、總和及平均數。 >提示:輸出浮點數到小數點後第一位。 **輸入1** <pre> 20 40 60 80 100 </pre> **輸出1** <pre> 20 40 60 80 100 Sum = 300.0 Average = 60.0 </pre> **輸入2** <pre> 88.7 12 56 132.55 3 </pre> **輸出2** <pre> 88.7 12 56 132.55 3 Sum = 292.2 Average = 58.5 </pre> **程式** ```python=1 num1 = eval(input()) num2 = eval(input()) num3 = eval(input()) num4 = eval(input()) num5 = eval(input()) Sum = num1 + num2 + num3 + num4 + num5 print('{} {} {} {} {}' .format(num1, num2, num3, num4, num5)) print('Sum = %.1f' %Sum) print('Average = %.1f' %(Sum / 5)) ``` **註解** :::info :one: input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。 >[color=#fff] >1. x = input('請輸入體重') 當使用者輸入63.4時,則 x ='63.4'。但這個63.4是屬於「文字」資料。也稱為「字串」資料 >2. 而「文字」資料,不能用來作 :heavy_plus_sign: :heavy_minus_sign: :heavy_multiplication_x: :heavy_division_sign: ... 等的數學運算哦! :two: eval() 用於將「文字資料」轉換成「數值資料」,數值可包含有小數。 >[color=#fff] :three: 字串.format() 用來將資料格式化的函數。跟用「%」設定資料格式相似。 >[color=#fff] >1. print('{ }{ }'.format(34,56)):依序呈現資料。 >34 56 >2. print('{2}{0}{1}'.format(34,56,77)):設定輸出順序。 >77 34 56 >3. print('{:.2f}'.format(3.14159)):取至小數第2位。 >3.14 >4. print('{:,}'.format(1000000)):逗號取千位。 >1,000,000 :four: print( )用於輸出資料。輸出「文字資料」的格式設定: >[color=#fff] >1. 「' '」符號內的文字直接輸出。 >2. 「%」 宣告 格式設定 >3. 「.1」表示 不限全部的文字個數,但是小數限1位數。 >4. 「f」表示 浮點數,就是帶有小數的數值 > :five: 運算符號 >[color=#fff] > >| 運算 | 符號 | 範列 |計算優先順序| >| --- | ----| --------|--------| >| 加 | + | 1+1 =2 |3| >| 減 | - | 3-2 =1 |3| >| 乘 | * | 2\*3 =6 |2| >| 除 | / | 2/5 =0.4 |2| >| 次方 | ** | 2\*\*2=4 |1| > > >註1:優先順序相同時,從左邊向右邊:arrow_right:計算。例如:6/2*(1+2)=[?](https://youtu.be/EWhCQqhthWg) >註2:在python中『2^2^』寫成『2**2』 > ::: **執行** :::warning <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ::: --- ### 108 座標距離計算 **題目** 請撰寫一程式,讓使用者輸入四個數字x1、y1、x2、y2,分別代表兩個點的座標(x1, y1)、(x2, y2)。計算並輸出這兩點的座標與其歐式距離。 >提示1: $$歐式距離 =\sqrt {((x1−x2)^2+(y1−y2)^2)}$$ >提示2:兩座標的歐式距離,輸出到小數點後第4位。 **輸入1** <pre> 2 1 5.5 8 </pre> **輸出1** <pre> ( 2 , 1 ) ( 5.5 , 8 ) Distance = 7.8262 </pre> **程式** ```python=1 import math x1 = eval(input()) y1 = eval(input()) x2 = eval(input()) y2 = eval(input()) print('( {} , {} )' .format(x1, y1)) print('( {} , {} )' .format(x2, y2)) print('Distance = %.4f' %math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)) ``` **註解** :::info :one: import 是匯入外部模組的敘述 >[color=#fff] > :two: math 是數學功能模組,「math.sqrt」是「開根號」 >[color=#fff] :three: input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。 >[color=#fff] >1. x = input('請輸入體重') 當使用者輸入63.4時,則 x ='63.4'。但這個63.4是屬於「文字」資料。也稱為「字串」資料 >2. 而「文字」資料,不能用來作 :heavy_plus_sign: :heavy_minus_sign: :heavy_multiplication_x: :heavy_division_sign: ... 等的數學運算哦! :four: eval() 用於將「文字資料」轉換成「數值資料」,數值可包含有小數。 >[color=#fff] :five: 字串.format() 用來將資料格式化的函數。跟用「%」設定資料格式相似。 >[color=#fff] >1. print('{ }{ }'.format(34,56)):依序呈現資料。 >34 56 >2. print('{2}{0}{1}'.format(34,56,77)):設定輸出順序。 >77 34 56 >3. print('{:.2f}'.format(3.14159)):取至小數第2位。 >3.14 >4. print('{:,}'.format(1000000)):逗號取千位。 >1,000,000 :six: print( )用於輸出資料。輸出「文字資料」的格式設定: >[color=#fff] >1. 「' '」符號內的文字直接輸出。 >2. 「%」 宣告 格式設定 >3. 「.4」表示 不限全部的文字個數,但是小數限4位數。 >4. 「f」表示 浮點數,就是帶有小數的數值 > :seven: 運算符號 >[color=#fff] > >| 運算 | 符號 | 範列 |計算優先順序| >| --- | ----| --------|--------| >| 加 | + | 1+1 =2 |3| >| 減 | - | 3-2 =1 |3| >| 乘 | * | 2\*3 =6 |2| >| 除 | / | 2/5 =0.4 |2| >| 次方 | ** | 2\*\*2=4 |1| > > >註1:優先順序相同時,從左邊向右邊:arrow_right:計算。例如:6/2*(1+2)=[?](https://youtu.be/EWhCQqhthWg) >註2:在python中『2^2^』寫成『2**2』 > ::: **執行** :::warning <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ::: --- ### 109 正五邊形面積計算 **題目** 請撰寫一程式,讓使用者輸入一個正數s,代表正五邊形之邊長,計算並輸出此正五邊形之面積(Area)。 >提示1:建議使用import math模組的math.pow及math.tan >提示2:正五邊形面積的公式:$$Area = \frac{5\times s^{2}}{4\times tan(\frac{\pi}{5})}$$ >提示3:輸出浮點數到小數點後第4位。 **輸入1** <pre> 5 </pre> **輸出1** <pre> Area = 43.0119 </pre> **程式** ```python=1 import math s = eval(input()) print('Area = %.4f' %((5 * s ** 2) / (4 * math.tan(math.pi / 5)))) ``` **註解** :::info :one: import 是匯入外部模組的敘述 >[color=#fff] > :two: math 是數學功能模組 >[color=#fff] >「math.pi」是「圓周率3.14159…」 >「math.tan」是三角函數「tan」 >「math.pow」是取「某數的n次方」,本題也可以用運算符號『**』取次方就可以了。 > :three: input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。 >[color=#fff] >1. x = input('請輸入體重') 當使用者輸入63.4時,則 x ='63.4'。但這個63.4是屬於「文字」資料。也稱為「字串」資料 >2. 而「文字」資料,不能用來作 :heavy_plus_sign: :heavy_minus_sign: :heavy_multiplication_x: :heavy_division_sign: ... 等的數學運算哦! :four: eval() 用於將「文字資料」轉換成「數值資料」,數值可包含有小數。 >[color=#fff] :five: print( )用於輸出資料。輸出「文字資料」的格式設定: >[color=#fff] >1. 「' '」符號內的文字直接輸出。 >2. 「%」 宣告 格式設定 >3. 「.4」表示 不限全部的文字個數,但是小數限4位數。 >4. 「f」表示 浮點數,就是帶有小數的數值 > :six: 運算符號 >[color=#fff] > >| 運算 | 符號 | 範列 |計算優先順序| >| --- | ----| --------|--------| >| 加 | + | 1+1 =2 |3| >| 減 | - | 3-2 =1 |3| >| 乘 | * | 2\*3 =6 |2| >| 除 | / | 2/5 =0.4 |2| >| 次方 | ** | 2\*\*2=4 |1| > > >註1:優先順序相同時,從左邊向右邊:arrow_right:計算。例如:6/2*(1+2)=[?](https://youtu.be/EWhCQqhthWg) >註2:在python中『2^2^』寫成『2**2』 > ::: **執行** :::warning <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ::: --- ### 110 正n邊形面積計算 **題目** 請撰寫一程式,讓使用者輸入兩個正數n、s,代表正n邊形之邊長為s,計算並輸出此正n邊形之面積(Area)。 >提示1:建議使用import math模組的math.pow及math.tan 提示2:正n邊形面積的公式如下: $$Area = \frac{n\times s^{2}}{4\times tan(\frac{\pi}{n})}$$ >提示3:輸出浮點數到小數點後第4位。 **輸入1** <pre> 8 6 </pre> **輸出1** <pre> Area = 173.8234 </pre> **程式** ```python=1 import math n = eval(input()) s = eval(input()) print('Area = %.4f' %((n * s ** 2) / (4 * math.tan(math.pi / n)))) ``` **註解** :::info :one: import 是匯入外部模組的敘述 >[color=#fff] > :two: math 是數學功能模組 >[color=#fff] >「math.pi」是「圓周率3.14159…」 >「math.tan」是三角函數「tan」 >「math.pow」是取「某數的n次方」,本題也可以用運算符號『**』取次方就可以了。 > :three: input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。 >[color=#fff] >1. x = input('請輸入體重') 當使用者輸入63.4時,則 x ='63.4'。但這個63.4是屬於「文字」資料。也稱為「字串」資料 >2. 而「文字」資料,不能用來作 :heavy_plus_sign: :heavy_minus_sign: :heavy_multiplication_x: :heavy_division_sign: ... 等的數學運算哦! :four: eval() 用於將「文字資料」轉換成「數值資料」,數值可包含有小數。 >[color=#fff] :five: print( )用於輸出資料。輸出「文字資料」的格式設定: >[color=#fff] >1. 「' '」符號內的文字直接輸出。 >2. 「%」 宣告 格式設定 >3. 「.4」表示 不限全部的文字個數,但是小數限4位數。 >4. 「f」表示 浮點數,就是帶有小數的數值 > :six: 運算符號 >[color=#fff] > >| 運算 | 符號 | 範列 |計算優先順序| >| --- | ----| --------|--------| >| 加 | + | 1+1 =2 |3| >| 減 | - | 3-2 =1 |3| >| 乘 | * | 2\*3 =6 |2| >| 除 | / | 2/5 =0.4 |2| >| 次方 | ** | 2\*\*2=4 |1| > > >註1:優先順序相同時,從左邊向右邊:arrow_right:計算。例如:6/2*(1+2)=[?](https://youtu.be/EWhCQqhthWg) >註2:在python中『2^2^』寫成『2**2』 > ::: **執行** :::warning <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ::: --- # Chapter 4 選擇結構程式設計 &emsp; &emsp; ## 4-1 結構化程式設計 1. 循序結構:按順序一步一步執行 2. 選擇結構:經由「條件」決定下一步 3. 重覆結構:執行重複的步驟 &emsp; &emsp; ## 4-2 if 判斷條件成立(True)執行,判斷條件不成立(False)就不執行。 ![](https://i.imgur.com/vIGhG4u.png) if 條件式 之後要記得加半形的「:」 底下指令要縮排。如果沒有縮排,會發生 :::warning <font color=#f00>縮排錯誤</font> <font color=#f00>Indentation Error</font> ::: &emsp; ![](https://i.imgur.com/FwR2sm6.png) &emsp; &emsp; ![](https://i.imgur.com/BOn2Dxf.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; ![](https://i.imgur.com/BM8gzRB.png) ![](https://i.imgur.com/hqYzl81.png) &emsp; &emsp; &emsp; <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; ## 4-3 if else 判斷條件成立(True)執行,判斷條件不成立(False)執行else底下程式。 &emsp; &emsp; &emsp; ![](https://i.imgur.com/ZQjhp2c.png) &emsp; &emsp; &emsp; ![](https://i.imgur.com/ZAsjpgF.png) &emsp; &emsp; &emsp; ![](https://i.imgur.com/VyCp91W.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; ![](https://i.imgur.com/hyO83uR.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; ## 4-4 if elif else if:判斷條件成立(True)執行,如果不成立,判斷其它elif條件成立(True)執行其它程式,如果全都不成立,則執行else:底下程式。 ![](https://i.imgur.com/L66VdBL.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ![](https://i.imgur.com/D8jDwME.png) &emsp; &emsp; &emsp; ![](https://i.imgur.com/HORMQLt.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; ![](https://i.imgur.com/E7Rm6C3.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; ### TQC 201 偶數判斷 **題目** 請使用選擇敘述撰寫一程式,讓使用者輸入一個正整數,然後判斷它是否為偶數(even)。 **輸入1** <pre> 56 </pre> **輸出1** <pre> 56 is an even number. </pre> **輸入2** <pre> 21 </pre> **輸出2** <pre> 21 is not an even number. </pre> <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; &emsp; &emsp; ![](https://i.imgur.com/6cmuAZb.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; ![](https://i.imgur.com/WIFzD0v.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; ![](https://i.imgur.com/tZqKd8V.png) <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="800" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> &emsp; &emsp; &emsp; ### TQC 202 倍數判斷 **題目** 請使用選擇敘述撰寫一程式,讓使用者輸入一個正整數,然後判斷它是3或5的倍數,顯示【x is a multiple of 3.】或【x is a multiple of 5.】;若此數值同時為3與5的倍數,顯示【x is a multiple of 3 and 5.】;如此數值皆不屬於3或5的倍數,顯示【x is not a multiple of 3 or 5.】,將使用者輸入的數值代入x。 **輸入1** <pre> 55 </pre> **輸出1** <pre> 55 is a multiple of 5. </pre> **輸入2** <pre> 36 </pre> **輸出2** <pre> 36 is a multiple of 3. </pre> **輸入3** <pre> 92 </pre> **輸出3** <pre> 92 is not a multiple of 3 or 5. </pre> **輸入4** <pre> 15 </pre> **輸出4** <pre> 15 is a multiple of 3 and 5. </pre> <iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>