---
title: 程式語言與設計 (上)
description: 旗立 程式語言與設計(上),技審字第110011號,https://trinket.io/,https://www.pythonanywhere.com/
tags: python
---
# Chapter 3 資料型別與資料運算
## 3-1程式組成與語法操作
 
 
---
**:pig:編碼** 使用UTF-8
:::warning
#-\*- coding: utf-8-\*-
:::
 
 
**:pig:縮排**

<iframe src="https://trinket.io/embed/python/1f27f79095" width="100%" height="250" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
縮排錯誤提示文字:
:::warning
expected an in dented block
:::
 
 
縮排也表示程式要執行的範圍

<iframe src="https://trinket.io/embed/python/e1baa3c05b" width="100%" height="250" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
---
**: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. 不可以用保留字


:dog:範列3-1
<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
---
 
## 3-2 資料的型別
**:pig:python 內建的三類資料型別**
1. 數值(numeric)
2. 字串(string)
3. 容器(container)

 
 
---
**:pig: 變數的產生**
1. 偵測其值的資料型別
2. 新增物件實體(變數)
3. 配置記憶體位置
4. 指派值給變數

---
 
:::warning
:pig:**數值型別**
:::
1. 整數(integer,int)
2. 浮點數(float)
3. 布林(boolean,bool)
4. 二進位(binary)
5. 八進位(octal)
6. 十六進位(hexadecimal)

:dog:範例3-2
<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
設定變數 data示意圖


 
更改變數 data內容值的示意圖


 
 
 
 
:::warning
**:pig:字串型別**
:::
**:dog:字串相加及複製**


:duck:範例3-5
<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
---
 
:::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>
:::
 
 
**:dog:取出字串的定位方式**
字串是有序列的資料型別,它的內容值都是有索引值編號的。

:::warning
變數[開始位置:結束位置:間隔]
:::

:duck:範例3-5
<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
::: 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>
:::
 
 
**:dog:字串的方法**
:::warning
變數名稱.方法名稱(相關參數)
:::
 
 
- 大小寫轉換方法


<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
::: info
:duck:動動腦
- 身分證字母轉大寫,為了防止民眾在台中購物節中,輸入身分證字號時,將身分證的第一個英文字母輸入小寫,造成系統辨識錯誤,請寫一個程式:不論民眾輸入的是大寫、或小寫字母,都一律轉成大寫字母。
<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
:::
 
 
- 置換、尋找方法


<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
::: 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>
:::
 
 
- 判斷字串


<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
:::warning
**:pig:容器**
:::
**python常見的容器**
1. 串列(list)
2. 元組(tuple)
3. 集合(set)
4. 字典(dict)
 
 
**:dog:串列(list)**
1. 串列是有順序的、內容可變的。
2. 串列裏面放的資料叫做「元素」
3. 每個元素有編號,叫做索引值(index).
4. 索引值從0開始編號。
5. 使用[ ]
6. 串列可以放不同型別的資料。
7. 串列中的每個元素長度可變、內容可變。
- 建立內容


<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
 
- 串列型別專屬的方法:

- 新增串列


<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
- 移除串列

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
:dog:元組(tuple)
- tuple 元組也是序列型別,有索引值。
- 不可變。
- 使用 ( )
- 內容用「,」隔開。
- 與list串列之比較


<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
:dog:集合(set)
- 集合內「無順序性」, 無索引值。

- 集合內的元素不重覆
- 內容是可變的。
- 內容可以包含各種不同的資料型別。
- 使用 { }
- 內容不可以儲存set、list、dict
- 內容用「,」隔開。

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
- 集合的方法


<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
:::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>
:::
- 集合的數學運算



<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
 
 
:dog:字典(dict)
1. 字典無順序
2. 字典的內容是「鍵:值」對。(key:value)
3. 鍵是唯一
4. 鍵必須為不可變的資料型別
 

- 字典的方法

 
 
- 新增

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
- 取值

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
- 更新

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
- 複製、移除、清除

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="500" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
- 集合與字典之比較

 
 
 
## 3-3 資料運算與處理
 
 
- 程式中的計算
1. 「算術」運算
2. 「關係」運算
3. 「邏輯」運算
 
 
 
- 算術運算


<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
- 關係運算


<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="200" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
- 邏輯運算

| 邏輯值 | 意義 | 計算時替換值 |
|:------:|:----------------:|:------------:|
| True |是、不是、真的| 1 |
| False |否、不對、假的| 0 |
 
- Or (或),邏輯加法
| A | B | A or B |
|:---:|:---:|:------:|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
 
- And (且),邏輯減法
| A | B |A and B |
|:---:|:---:|:------:|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
 
- not (相反)
| A | not A |
|:---:|:-----:|
| 1 | 0 |
| 0 | 1 |

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
- 複合運算



<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
- 運算子的優先順序

**<U>當優先順序相同時,會由左往右依序計算</U>**
 
 
:::info
:dog:優先順序相同時,從左邊向右邊:arrow_right:計算。例如:
<font size=50 >[6/2*(1+2)=?](https://youtu.be/EWhCQqhthWg)
</font>
:::
 
 
 
 
- 內建函數
1. 系統
2. 資料型別轉換
3. 資料運算與處理
4. 格式化
5. 容器

 
 
 
- 系統函數


<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
- 資料型別轉換


<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>

 
 
 
- 資料運算與處理


<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="500" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
- 格式化


<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="500" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
- print( )函數


<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
### TQC 101 整數格式化輸出

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="250" 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: int( )是數值函數,幫你將「文字」轉換成「整數」(正負數的整數值)。
>[color=#fff]
>
> 因為input( )所得的都是「文字」資料,所以要使用eval( )轉換成數值資料,才能夠用在計算上。
:four: print( )用於輸出資料。輸出「數值資料」的格式設定:
>[color=#fff]
>1. 「%」 宣告 格式設定
>2. 「5」表示 5個字。不足5個字,就填空格。
>3. 「d」表示 10進位整數
>4. 「-」符號表示資料靠左對齊
「沒有 -」符號表示資料靠右對齊(預設情形)
>5. 格式設定與變數位置,相對應如下圖:
>
:::
 
 
 
### TQC 102 浮點數格式化輸出

<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: 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. 格式設定與變數位置,相對應如下圖:
>
:::
 
 
 
### TQC 103 字串格式化輸出

<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. 格式設定與變數位置,相對應如下圖:
>
:::
 
 
 
- 容器資料型別轉換


<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
## 3-4 模組簡介與應用
- 寫好的函數放在模組中
- 使用時,用import匯入程式中使用
例:random、numpy模組,要匯入時:
```python =1
import random
```

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="200" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
- 匯入的模組可以取「別名」(像是「匿稱」、「小名」、「簡稱」之類的…)
底下程式就是將「random」匯入程式後,叫它匿稱「rd」
```python =1
import random as rd
num = rd.uniform(0,1)
```

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="200" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 






---
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. 格式設定與變數位置,相對應如下圖:
: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. 格式設定與變數位置,相對應如下圖:
>
: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. 格式設定與變數位置,相對應如下圖:
>
: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 選擇結構程式設計
 
 
## 4-1 結構化程式設計
1. 循序結構:按順序一步一步執行
2. 選擇結構:經由「條件」決定下一步
3. 重覆結構:執行重複的步驟
 
 
## 4-2 if
判斷條件成立(True)執行,判斷條件不成立(False)就不執行。

if 條件式 之後要記得加半形的「:」
底下指令要縮排。如果沒有縮排,會發生
:::warning
<font color=#f00>縮排錯誤</font>
<font color=#f00>Indentation Error</font>
:::
 

 
 

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 


 
 
 
<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
## 4-3 if else
判斷條件成立(True)執行,判斷條件不成立(False)執行else底下程式。
 
 
 

 
 
 

 
 
 

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
## 4-4 if elif else
if:判斷條件成立(True)執行,如果不成立,判斷其它elif條件成立(True)執行其它程式,如果全都不成立,則執行else:底下程式。

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>

 
 
 

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
### 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>
 
 
 
 
 

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 

<iframe src="https://trinket.io/embed/python/e3a3658356" width="100%" height="800" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 
 
 
### 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>