Try   HackMD

Chapter 3 資料型別與資料運算

3-1程式組成與語法操作



Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
編碼 使用UTF-8

#-*- coding: utf-8-*-



Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
縮排

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


縮排也表示程式要執行的範圍

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →



Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
註解

單列註解 ,加「#」

#從鍵盤輸入溫度,轉成整數 temp = int (input('請輸入溫度'))

多行註解,頭尾加上三個「```」

‵‵‵使用if來判斷溫度 溫度是否大於28度‵‵‵ if temp > 28 : : :

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
變數與檔案命名規則
變數:會隨著程式執行過程中,而改變的物件。

  1. 第1個字是英文字母或是底線
  2. 中間不可以有空格
  3. 可以用底線「_」,不可以用減號「-」
  4. 不可用特殊符號,如:%、#、@…等
  5. 不建議用中文。(但中文是可以用的)
  6. 大小寫不一樣,如:Hello 不等同 hello
  7. 避免用「1、0、l、O」
  8. 不可以用保留字
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
範列3-1


3-2 資料的型別

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
python 內建的三類資料型別

  1. 數值(numeric)
  2. 字串(string)
  3. 容器(container)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →



Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
變數的產生

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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
數值型別

  1. 整數(integer,int)
  2. 浮點數(float)
  3. 布林(boolean,bool)
  4. 二進位(binary)
  5. 八進位(octal)
  6. 十六進位(hexadecimal)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
範例3-2



設定變數 data示意圖

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →




Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
字串型別

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
字串相加及複製
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
範例3-5




Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
取出字串的定位方式

字串是有序列的資料型別,它的內容值都是有索引值編號的。

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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
範例3-5


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
高中生程式解題系統 a038: 數字翻轉


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
字串的方法

變數名稱.方法名稱(相關參數)


  • 大小寫轉換方法

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
動動腦

  • 身分證字母轉大寫,為了防止民眾在台中購物節中,輸入身分證字號時,將身分證的第一個英文字母輸入小寫,造成系統辨識錯誤,請寫一個程式:不論民眾輸入的是大寫、或小寫字母,都一律轉成大寫字母。


  • 置換、尋找方法


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
高中生程式解題系統 c760: 蝸牛老師的點名單


  • 判斷字串


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
容器

python常見的容器

  1. 串列(list)
  2. 元組(tuple)
  3. 集合(set)
  4. 字典(dict)


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
串列(list)

  1. 串列是有順序的、內容可變的。
  2. 串列裏面放的資料叫做「元素」
  3. 每個元素有編號,叫做索引值(index).
  4. 索引值從0開始編號。
  5. 使用[ ]
  6. 串列可以放不同型別的資料。
  7. 串列中的每個元素長度可變、內容可變。
  • 建立內容




  • 串列型別專屬的方法:

  • 新增串列



  • 移除串列




Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
元組(tuple)

  • tuple 元組也是序列型別,有索引值。
  • 不可變。
  • 使用 ( )
  • 內容用「,」隔開。
  • 與list串列之比較



Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
集合(set)

  • 集合內「無順序性」, 無索引值。

  • 集合內的元素不重覆

  • 內容是可變的。

  • 內容可以包含各種不同的資料型別。

  • 使用 { }

  • 內容不可以儲存set、list、dict

  • 內容用「,」隔開。

  • 集合的方法



Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
動動腦

輸入這一段英文句子「The quick brown fox jumps over the lazy dog」,請算出裏面不重複的字母共有幾個!

提示1:記得將句子中的大寫轉換成小寫。
提示2:set(字串),可以將字串轉成集合。
提示3:remove(" "),可以移除集合內的空白
提示4:len(集合),可以計算集合內的個數,如:len({1,2,3})=3

  • 集合的數學運算




Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
字典(dict)

  1. 字典無順序
  2. 字典的內容是「鍵:值」對。(key:value)
  3. 鍵是唯一
  4. 鍵必須為不可變的資料型別

  • 字典的方法


  • 新增


  • 取值

  • 更新

  • 複製、移除、清除

  • 集合與字典之比較



3-3 資料運算與處理


  • 程式中的計算
  1. 「算術」運算
  2. 「關係」運算
  3. 「邏輯」運算



  • 算術運算



  • 關係運算



  • 邏輯運算

邏輯值 意義 計算時替換值
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



  • 複合運算


  • 運算子的優先順序

當優先順序相同時,會由左往右依序計算


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
優先順序相同時,從左邊向右邊
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
計算。例如:

6/2*(1+2)=?




  • 內建函數
  1. 系統
  2. 資料型別轉換
  3. 資料運算與處理
  4. 格式化
  5. 容器



  • 系統函數



  • 資料型別轉換



  • 資料運算與處理



  • 格式化



TQC 101 整數格式化輸出

註解

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
「 | 符號」 在Enter鍵上方,先按住shift 後再按即可。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。

  1. x = input('請輸入年齡')
    當使用者輸入28時,則 x = 28。但這個28是屬於「文字」資料。
  2. 而「文字」資料,不能用來作
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    等的數學運算哦!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 int( )是數值函數,幫你將「文字」轉換成「整數」(正負數的整數值)。

因為input( )所得的都是「文字」資料,所以要使用eval( )轉換成數值資料,才能夠用在計算上。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
print( )用於輸出資料。輸出「數值資料」的格式設定:

  1. 「%」 宣告 格式設定
  2. 「5」表示 5個字。不足5個字,就填空格。
  3. 「d」表示 10進位整數
  4. 「-」符號表示資料靠左對齊
    「沒有 -」符號表示資料靠右對齊(預設情形)
  5. 格式設定與變數位置,相對應如下圖:
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →



TQC 102 浮點數格式化輸出

註解

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
「 | 符號」 在Enter鍵上方,先按住shift 後再按即可。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。

  1. x = input('請輸入年齡')
    當使用者輸入28時,則 x = 28。但這個28是屬於「文字」資料。
  2. 而「文字」資料,不能用來作
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    等的數學運算哦!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 float( )是浮點函數,幫你將「文字」轉換成「帶小數的數值」。

因為input( )所得的都是「文字」資料,所以要使用float( )轉換成帶小數數值資料,才能夠用在計算上。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
print( )用於輸出資料。輸出「數值資料」的格式設定:

  1. 「%」 宣告 格式設定
  2. 「7」表示 全部限7個字,而且包含小數點及小數位數。不足7個字,就填空格。
  3. 「.2」表示 保留小數2位,不足補0。超過小數2位時,4捨5入到小數第2位。
  4. 「f」表示 浮點數,就是帶有小數的數值
  5. 「-」符號表示資料靠左對齊
    「沒有 -」符號表示資料靠右對齊(預設情形)
  6. 格式設定與變數位置,相對應如下圖:
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →



TQC 103 字串格式化輸出

註解

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
「 | 符號」 在Enter鍵上方,先按住shift 後再按即可。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。

  1. x = input('請輸入年齡')
    當使用者輸入28時,則 x = 28。但這個28是屬於「文字」資料。也稱為「字串」資料
  2. 而「文字」資料,不能用來作
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    等的數學運算哦!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
print( )用於輸出資料。輸出「文字資料」的格式設定:

  1. 「%」 宣告 格式設定
  2. 「10」表示 全部限10個字。不足10個字,就填空格。
  3. 「s」表示 文字資料,也稱字串資料
  4. 「-」符號表示資料靠左對齊
    「沒有 -」符號表示資料靠右對齊(預設情形)
  5. 格式設定與變數位置,相對應如下圖:
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →



  • 容器資料型別轉換



3-4 模組簡介與應用

  • 寫好的函數放在模組中
  • 使用時,用import匯入程式中使用

例:random、numpy模組,要匯入時:

import random



  • 匯入的模組可以取「別名」(像是「匿稱」、「小名」、「簡稱」之類的…)

底下程式就是將「random」匯入程式後,叫它匿稱「rd」

import random as rd

num = rd.uniform(0,1)



TQC程式設計檢定題目

104 圓形面積計算

題目

請撰寫一程式,輸入一圓的半徑,並加以計算此圓之面積和周長,最後請印出此圓的半徑(Radius)、周長(Perimeter)和面積(Area)。

提示1:需import math模組,並使用math.pi。
提示2:輸出浮點數到小數點後第二位。

輸入1

10

輸出1

Radius = 10.00
Perimeter = 62.83
Area = 314.16

輸入2

2.5

輸出2

Radius = 2.50
Perimeter = 15.71
Area = 19.63

程式

import math r = float(input()) print('Radius = %.2f' %r) print('Perimeter = %.2f' %(2 * math.pi * r)) print('Area = %.2f' %(math.pi * r ** 2))

註解

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 import 是匯入外部模組的敘述

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 math 是數學功能模組,「math.pi」是圓周率值3.141592

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。

  1. x = input('請輸入體重')
    當使用者輸入63.4時,則 x = '63.4'。但這個'63.4'是屬於「文字」資料。也稱為「字串」資料
  2. 而「文字」資料,不能用來作
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    等的數學運算哦!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 float() 用於將「文字資料」轉換成「浮點數資料」,浮點數就是帶有小數的資料。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 print( )用於輸出資料。輸出「文字資料」的格式設定:

  1. 「' '」符號內的文字直接輸出。
  2. 「%」 宣告 格式設定
  3. 「.2」表示 不限全部的文字個數,但是小數限2位數。
  4. 「f」表示 浮點數,就是帶有小數的數值
  5. 格式設定與變數位置,相對應如下圖:
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 運算符號

運算 符號 範列 計算優先順序
+ 1+1 =2 3
- 3-2 =1 3
* 2*3 =6 2
/ 2/5 =0.4 2
次方 ** 2**2=4 1

註1:優先順序相同時,從左邊向右邊

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
計算。例如:6/2*(1+2)=?
註2:在python中『22』寫成『2**2』

執行


105 矩形面積計算

題目

請撰寫一程式,輸入兩個正數,代表一矩形之寬和高,計算並輸出此矩形之高(Height)、寬(Width)、周長(Perimeter)及面積(Area)。

提示:輸出浮點數到小數點後第二位。

輸入

23.5
19

輸出

Height = 23.50
Width = 19.00
Perimeter = 85.00
Area = 446.50 

程式

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))

註解

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。

  1. x = input('請輸入體重')
    當使用者輸入63.4時,則 x ='63.4'。但這個63.4是屬於「文字」資料。也稱為「字串」資料
  2. 而「文字」資料,不能用來作
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    等的數學運算哦!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 float() 用於將「文字資料」轉換成「浮點數資料」,浮點數就是帶有小數的資料。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 print( )用於輸出資料。輸出「文字資料」的格式設定:

  1. 「' '」符號內的文字直接輸出。
  2. 「%」 宣告 格式設定
  3. 「.2」表示 不限全部的文字個數,但是小數限2位數。
  4. 「f」表示 浮點數,就是帶有小數的數值
  5. 格式設定與變數位置,相對應如下圖:
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 運算符號

運算 符號 範列 計算優先順序
+ 1+1 =2 3
- 3-2 =1 3
* 2*3 =6 2
/ 2/5 =0.4 2
次方 ** 2**2=4 1

註1:優先順序相同時,從左邊向右邊

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
計算。例如:6/2*(1+2)=?
註2:在python中『22』寫成『2**2』

執行


106 公里英哩換算

題目

假設一賽跑選手在x分y秒的時間跑完z公里,請撰寫一程式,輸入x、y、z數值,最後顯示此選手每小時的平均英哩速度(1英哩等於1.6公里)。

提示:輸出浮點數到小數點後第一位。

輸入

10
25
3

輸出

Speed = 10.8

程式

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)))

註解

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。

  1. x = input('請輸入體重')
    當使用者輸入63.4時,則 x ='63.4'。但這個63.4是屬於「文字」資料。也稱為「字串」資料
  2. 而「文字」資料,不能用來作
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    等的數學運算哦!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 float() 用於將「文字資料」轉換成「浮點數資料」,浮點數就是帶有小數的資料。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 print( )用於輸出資料。輸出「文字資料」的格式設定:

  1. 「' '」符號內的文字直接輸出。
  2. 「%」 宣告 格式設定
  3. 「.1」表示 不限全部的文字個數,但是小數限1位數。
  4. 「f」表示 浮點數,就是帶有小數的數值
  5. 格式設定與變數位置,相對應如下圖:
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 運算符號

運算 符號 範列 計算優先順序
+ 1+1 =2 3
- 3-2 =1 3
* 2*3 =6 2
/ 2/5 =0.4 2
次方 ** 2**2=4 1

註1:優先順序相同時,從左邊向右邊

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
計算。例如:6/2*(1+2)=?
註2:在python中『22』寫成『2**2』

執行


107 數值計算

題目

請撰寫一程式,讓使用者輸入五個數字,計算並輸出這五個數字之數值、總和及平均數。

提示:輸出浮點數到小數點後第一位。

輸入1

20
40
60
80
100

輸出1

20 40 60 80 100
Sum = 300.0
Average = 60.0

輸入2

88.7
12
56
132.55
3

輸出2

88.7 12 56 132.55 3
Sum = 292.2
Average = 58.5

程式

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))

註解

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。

  1. x = input('請輸入體重')
    當使用者輸入63.4時,則 x ='63.4'。但這個63.4是屬於「文字」資料。也稱為「字串」資料
  2. 而「文字」資料,不能用來作
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    等的數學運算哦!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 eval() 用於將「文字資料」轉換成「數值資料」,數值可包含有小數。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 字串.format() 用來將資料格式化的函數。跟用「%」設定資料格式相似。

  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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 print( )用於輸出資料。輸出「文字資料」的格式設定:

  1. 「' '」符號內的文字直接輸出。
  2. 「%」 宣告 格式設定
  3. 「.1」表示 不限全部的文字個數,但是小數限1位數。
  4. 「f」表示 浮點數,就是帶有小數的數值

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 運算符號

運算 符號 範列 計算優先順序
+ 1+1 =2 3
- 3-2 =1 3
* 2*3 =6 2
/ 2/5 =0.4 2
次方 ** 2**2=4 1

註1:優先順序相同時,從左邊向右邊

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
計算。例如:6/2*(1+2)=?
註2:在python中『22』寫成『2**2』

執行


108 座標距離計算

題目

請撰寫一程式,讓使用者輸入四個數字x1、y1、x2、y2,分別代表兩個點的座標(x1, y1)、(x2, y2)。計算並輸出這兩點的座標與其歐式距離。

提示1:

=((x1x2)2+(y1y2)2)

提示2:兩座標的歐式距離,輸出到小數點後第4位。

輸入1

2
1
5.5
8

輸出1

( 2 , 1 )
( 5.5 , 8 )
Distance = 7.8262

程式

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))

註解

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 import 是匯入外部模組的敘述

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 math 是數學功能模組,「math.sqrt」是「開根號」

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。

  1. x = input('請輸入體重')
    當使用者輸入63.4時,則 x ='63.4'。但這個63.4是屬於「文字」資料。也稱為「字串」資料
  2. 而「文字」資料,不能用來作
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    等的數學運算哦!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 eval() 用於將「文字資料」轉換成「數值資料」,數值可包含有小數。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 字串.format() 用來將資料格式化的函數。跟用「%」設定資料格式相似。

  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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 print( )用於輸出資料。輸出「文字資料」的格式設定:

  1. 「' '」符號內的文字直接輸出。
  2. 「%」 宣告 格式設定
  3. 「.4」表示 不限全部的文字個數,但是小數限4位數。
  4. 「f」表示 浮點數,就是帶有小數的數值

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 運算符號

運算 符號 範列 計算優先順序
+ 1+1 =2 3
- 3-2 =1 3
* 2*3 =6 2
/ 2/5 =0.4 2
次方 ** 2**2=4 1

註1:優先順序相同時,從左邊向右邊

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
計算。例如:6/2*(1+2)=?
註2:在python中『22』寫成『2**2』

執行


109 正五邊形面積計算

題目

請撰寫一程式,讓使用者輸入一個正數s,代表正五邊形之邊長,計算並輸出此正五邊形之面積(Area)。

提示1:建議使用import math模組的math.pow及math.tan
提示2:正五邊形面積的公式:

Area=5×s24×tan(π5)
提示3:輸出浮點數到小數點後第4位。

輸入1

5

輸出1

Area = 43.0119

程式

import math s = eval(input()) print('Area = %.4f' %((5 * s ** 2) / (4 * math.tan(math.pi / 5))))

註解

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 import 是匯入外部模組的敘述

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 math 是數學功能模組


「math.pi」是「圓周率3.14159…」
「math.tan」是三角函數「tan」
「math.pow」是取「某數的n次方」,本題也可以用運算符號『**』取次方就可以了。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。

  1. x = input('請輸入體重')
    當使用者輸入63.4時,則 x ='63.4'。但這個63.4是屬於「文字」資料。也稱為「字串」資料
  2. 而「文字」資料,不能用來作
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    等的數學運算哦!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 eval() 用於將「文字資料」轉換成「數值資料」,數值可包含有小數。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 print( )用於輸出資料。輸出「文字資料」的格式設定:

  1. 「' '」符號內的文字直接輸出。
  2. 「%」 宣告 格式設定
  3. 「.4」表示 不限全部的文字個數,但是小數限4位數。
  4. 「f」表示 浮點數,就是帶有小數的數值

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 運算符號

運算 符號 範列 計算優先順序
+ 1+1 =2 3
- 3-2 =1 3
* 2*3 =6 2
/ 2/5 =0.4 2
次方 ** 2**2=4 1

註1:優先順序相同時,從左邊向右邊

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
計算。例如:6/2*(1+2)=?
註2:在python中『22』寫成『2**2』

執行


110 正n邊形面積計算

題目

請撰寫一程式,讓使用者輸入兩個正數n、s,代表正n邊形之邊長為s,計算並輸出此正n邊形之面積(Area)。

提示1:建議使用import math模組的math.pow及math.tan
提示2:正n邊形面積的公式如下:

Area=n×s24×tan(πn)

提示3:輸出浮點數到小數點後第4位。

輸入1

8
6

輸出1

Area = 173.8234

程式

import math n = eval(input()) s = eval(input()) print('Area = %.4f' %((n * s ** 2) / (4 * math.tan(math.pi / n))))

註解

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 import 是匯入外部模組的敘述

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 math 是數學功能模組


「math.pi」是「圓周率3.14159…」
「math.tan」是三角函數「tan」
「math.pow」是取「某數的n次方」,本題也可以用運算符號『**』取次方就可以了。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 input( )是輸入函數,可以提供使用者輸入資料。輸入的資料都會被轉換成「文字」資料。

  1. x = input('請輸入體重')
    當使用者輸入63.4時,則 x ='63.4'。但這個63.4是屬於「文字」資料。也稱為「字串」資料
  2. 而「文字」資料,不能用來作
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    等的數學運算哦!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 eval() 用於將「文字資料」轉換成「數值資料」,數值可包含有小數。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 print( )用於輸出資料。輸出「文字資料」的格式設定:

  1. 「' '」符號內的文字直接輸出。
  2. 「%」 宣告 格式設定
  3. 「.4」表示 不限全部的文字個數,但是小數限4位數。
  4. 「f」表示 浮點數,就是帶有小數的數值

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 運算符號

運算 符號 範列 計算優先順序
+ 1+1 =2 3
- 3-2 =1 3
* 2*3 =6 2
/ 2/5 =0.4 2
次方 ** 2**2=4 1

註1:優先順序相同時,從左邊向右邊

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
計算。例如:6/2*(1+2)=?
註2:在python中『22』寫成『2**2』

執行


Chapter 4 選擇結構程式設計


4-1 結構化程式設計

  1. 循序結構:按順序一步一步執行
  2. 選擇結構:經由「條件」決定下一步
  3. 重覆結構:執行重複的步驟


4-2 if

判斷條件成立(True)執行,判斷條件不成立(False)就不執行。

if 條件式 之後要記得加半形的「:」
底下指令要縮排。如果沒有縮排,會發生

縮排錯誤
Indentation Error










4-3 if else

判斷條件成立(True)執行,判斷條件不成立(False)執行else底下程式。













4-4 if elif else

if:判斷條件成立(True)執行,如果不成立,判斷其它elif條件成立(True)執行其它程式,如果全都不成立,則執行else:底下程式。





TQC 201 偶數判斷

題目

請使用選擇敘述撰寫一程式,讓使用者輸入一個正整數,然後判斷它是否為偶數(even)。

輸入1

56

輸出1

56 is an even number.

輸入2

21

輸出2

21 is not an even number.











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

55

輸出1

55 is a multiple of 5.

輸入2

36

輸出2

36 is a multiple of 3.

輸入3

92

輸出3

92 is not a multiple of 3 or 5.

輸入4

15

輸出4

15 is a multiple of 3 and 5.