AS01: Python Basics#
PART I: Trivial Question with AI#
Q1_1: Converting Fahrenheit to Celsius temperature degree#
請你寫出轉換攝氏溫度為華氏溫度的Code。並輸出為以下型態
例如:(攝氏tc溫度等於華氏tf溫度減32後乘以5/9)。
請你寫出轉換華氏溫度為攝氏溫度的Code。
Sample output:
tc: 10 tf: 50.0
tc = 10
# Your code should be here
Q1_2: 計算一元二次方程的解#
題目描述:已知一個一元二次方程 (ax^2 + bx + c = 0),其中 (a = 1, b = -3, c = 2),請寫一個 Python 程式計算它的兩個解。
解的公式(注意應該會有兩個答案): $\(x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}\)$
import math
# 已知 a, b, c
a = 1
b = -3
c = 2
# 在這裡寫程式碼計算解
# 在這裡輸出解
Q1_3: String as list#
在Python中,string字串也可以用list的方式來存取。如
word = "Pneumonoultramicroscopicsilicovolcanoconiosis" # 實際上存在的單字
print(word[3])
請用dictionary計算該字串中每個字元出現過幾次。
Output:
{'P': 1, 'n': 4, 'e': 1, 'u': 2, 'm': 2, 'o': 9, 'l': 3, 't': 1, 'r': 2, 'a': 2, 'i': 6, 'c': 6, 's': 4, 'p': 1, 'v': 1}
cdict = {}
word = "Pneumonoultramicroscopicsilicovolcanoconiosis"
# YOUR CODE SHOULD BE HERE
print(cdict)
{}
PART II: Accessing Taipei Bus real time data#
以下是台北市某線公車的即時資料。資料中主要分為兩大塊,包含該線公車的停靠站以及正在路上跑的公車(通常不只一台)及其位置。請觀察該資料並回答以下問題。
HINT: 這些題目其實都可以問AI把答案問出來,但要稍微思考一下,要怎麼印。
import requests
import json
url = "https://pda.5284.gov.taipei/MQS/RouteDyna?routeid=11763&nocache=1633009377839"
response = requests.get(url)
route = json.loads(response.text)
print(type(route))
route
<class 'dict'>
{'UpdateTime': '2025-09-21 21:58:45',
'Stop': [{'id': 204939,
'n1': 'N1,204939,11763,-1,,,,-1,,2,2,250921215539,00094430,250921215539,67961594'},
{'id': 204940,
'n1': 'N1,204940,11763,-1,,,,-1,,2,2,250921215539,11527372,250921215539,67961587'},
{'id': 204941,
'n1': 'N1,204941,11763,-1,,,,-1,,2,2,250921215539,11529842,250921215539,67961585'},
{'id': 15341,
'n1': 'N1,15341,11763,-1,,,,-1,,2,2,250921215539,11539394,250921215539,67963544'},
{'id': 15342,
'n1': 'N1,15342,11763,-1,,,,-1,,2,2,250921215539,11545047,250921215539,67963542'},
{'id': 15343,
'n1': 'N1,15343,11763,-1,,,,-1,,2,2,250921215539,11548833,250921215539,67963540'},
{'id': 15344,
'n1': 'N1,15344,11763,-1,,,,-1,,2,2,250921215539,11557991,250921215539,67963538'},
{'id': 130134,
'n1': 'N1,130134,11763,-1,,,,-1,,2,2,250921215539,11564228,250921215539,67978201'},
{'id': 15345,
'n1': 'N1,15345,11763,-1,,,,-1,,2,2,250921215539,11566605,250921215539,67963537'},
{'id': 134736,
'n1': 'N1,134736,11763,-1,,,,-1,,2,2,250921215653,11581654,250921215653,68054683'},
{'id': 15346,
'n1': 'N1,15346,11763,-1,,,,-1,,2,2,250921215740,11589535,250921215740,68082194'},
{'id': 15347,
'n1': 'N1,15347,11763,-1,,,,-1,,2,2,250921215834,11594194,250921215834,68116519'},
{'id': 15348,
'n1': 'N1,15348,11763,222239773,15347,15376,0,108,1,0,2,250921215820,11593438,250921215820,68106316'},
{'id': 15349,
'n1': 'N1,15349,11763,222239773,15347,15376,0,184,2,0,2,250921215820,11593439,250921215820,68106317'},
{'id': 15350,
'n1': 'N1,15350,11763,222239773,15347,15376,0,253,3,0,2,250921215820,11593440,250921215820,68106318'},
{'id': 15351,
'n1': 'N1,15351,11763,222239773,15347,15376,0,320,4,0,2,250921215820,11593441,250921215820,68106319'},
{'id': 15352,
'n1': 'N1,15352,11763,222239773,15347,15376,0,383,5,0,2,250921215820,11593442,250921215820,68106320'},
{'id': 134750,
'n1': 'N1,134750,11763,222239773,15347,15376,0,473,6,0,2,250921215820,11593443,250921215820,68106321'},
{'id': 15354,
'n1': 'N1,15354,11763,222239773,15347,15376,0,549,7,0,2,250921215820,11593444,250921215820,68106322'},
{'id': 15355,
'n1': 'N1,15355,11763,222239773,15347,15376,0,627,8,0,2,250921215820,11593445,250921215820,68106323'},
{'id': 15356,
'n1': 'N1,15356,11763,222239773,15347,15376,0,669,9,0,2,250921215820,11593446,250921215820,68106324'},
{'id': 15357,
'n1': 'N1,15357,11763,222239773,15347,15376,0,820,10,0,2,250921215820,11593447,250921215820,68106325'},
{'id': 15358,
'n1': 'N1,15358,11763,222239773,15347,15376,0,914,11,0,2,250921215820,11593448,250921215820,68106326'},
{'id': 15359,
'n1': 'N1,15359,11763,222239773,15347,15376,0,1010,12,0,2,250921215820,11593449,250921215820,68106327'},
{'id': 15360,
'n1': 'N1,15360,11763,222239773,15347,15376,0,1034,13,0,2,250921215820,11593450,250921215820,68106328'},
{'id': 15361,
'n1': 'N1,15361,11763,222239773,15347,15376,0,1062,14,0,2,250921215820,11593451,250921215820,68106332'},
{'id': 134745,
'n1': 'N1,134745,11763,222239773,15347,15376,0,1079,15,0,2,250921215820,11593452,250921215820,68106333'},
{'id': 15362,
'n1': 'N1,15362,11763,222239773,15347,15376,0,1118,16,0,2,250921215820,11593453,250921215820,68106334'},
{'id': 15363,
'n1': 'N1,15363,11763,222239773,15347,15376,0,1165,17,0,2,250921215820,11593454,250921215820,68106335'},
{'id': 15364,
'n1': 'N1,15364,11763,222239773,15347,15376,0,1200,18,0,2,250921215820,11593455,250921215820,68106337'},
{'id': 15365,
'n1': 'N1,15365,11763,222239773,15347,15376,0,1289,19,0,2,250921215820,11593456,250921215820,68106339'},
{'id': 15366,
'n1': 'N1,15366,11763,222239773,15347,15376,0,1383,20,0,2,250921215820,11593457,250921215820,68106340'},
{'id': 15367,
'n1': 'N1,15367,11763,222239773,15347,15376,0,1455,21,0,2,250921215820,11593458,250921215820,68106341'},
{'id': 15368,
'n1': 'N1,15368,11763,222239773,15347,15376,0,1515,22,0,2,250921215820,11593459,250921215820,68106342'},
{'id': 185809,
'n1': 'N1,185809,11763,222239773,15347,15376,0,1563,23,0,2,250921215825,11593658,250921215825,68109473'},
{'id': 15370,
'n1': 'N1,15370,11763,222239783,185809,15376,0,60,1,0,2,250921215813,11592878,250921215813,68102616'},
{'id': 15371,
'n1': 'N1,15371,11763,222239783,185809,15376,0,123,2,0,2,250921215813,11592879,250921215813,68102617'},
{'id': 15372,
'n1': 'N1,15372,11763,222239783,185809,15376,0,184,3,0,2,250921215813,11592880,250921215813,68102618'},
{'id': 15373,
'n1': 'N1,15373,11763,222239783,185809,15376,0,274,4,0,2,250921215813,11592881,250921215813,68102619'},
{'id': 15374,
'n1': 'N1,15374,11763,222239783,185809,15376,0,395,5,0,2,250921215813,11592882,250921215813,68102620'},
{'id': 15375,
'n1': 'N1,15375,11763,222239783,185809,15376,0,515,6,0,2,250921215813,11592883,250921215813,68102621'},
{'id': 15376,
'n1': 'N1,15376,11763,222239783,185809,15376,0,629,7,0,2,250921215813,11592884,250921215813,68102622'},
{'id': 15377,
'n1': 'N1,15377,11763,222239783,185809,15376,0,754,8,0,2,250921215813,11592885,250921215813,68102623'},
{'id': 15378,
'n1': 'N1,15378,11763,222239783,185809,15376,0,819,9,0,2,250921215813,11592886,250921215813,68102624'},
{'id': 15379,
'n1': 'N1,15379,11763,222239783,185809,15376,0,951,10,0,2,250921215813,11592887,250921215813,68102625'},
{'id': 15380,
'n1': 'N1,15380,11763,222239783,185809,15376,0,1151,11,0,2,250921215813,11592888,250921215813,68102626'},
{'id': 15381,
'n1': 'N1,15381,11763,222240411,15380,204944,0,106,1,1,2,250921215802,11592103,250921215802,68096245'},
{'id': 15563,
'n1': 'N1,15563,11763,222240411,15380,204944,0,160,2,1,2,250921215802,11592104,250921215802,68096246'},
{'id': 15695,
'n1': 'N1,15695,11763,222240411,15380,204944,0,218,3,1,2,250921215802,11592105,250921215802,68096247'},
{'id': 15939,
'n1': 'N1,15939,11763,222240411,15380,204944,0,323,4,1,2,250921215802,11592106,250921215802,68096248'},
{'id': 16129,
'n1': 'N1,16129,11763,222240411,15380,204944,0,367,5,1,2,250921215802,11592107,250921215802,68096249'},
{'id': 16277,
'n1': 'N1,16277,11763,222240411,15380,204944,0,414,6,1,2,250921215802,11592108,250921215802,68096250'},
{'id': 16391,
'n1': 'N1,16391,11763,222240411,15380,204944,0,506,7,1,2,250921215802,11592109,250921215802,68096251'},
{'id': 16561,
'n1': 'N1,16561,11763,222240411,15380,204944,0,598,8,1,2,250921215802,11592110,250921215802,68096252'},
{'id': 16613,
'n1': 'N1,16613,11763,222240411,15380,204944,0,707,9,1,2,250921215802,11592111,250921215802,68096253'},
{'id': 151309,
'n1': 'N1,151309,11763,222239780,16613,204944,0,50,1,1,2,250921215732,11584930,250921215732,68078057'},
{'id': 16615,
'n1': 'N1,16615,11763,222239780,16613,204944,0,122,2,1,2,250921215732,11584931,250921215732,68078058'},
{'id': 16655,
'n1': 'N1,16655,11763,222239780,16613,204944,0,155,3,1,2,250921215732,11584932,250921215732,68078059'},
{'id': 16657,
'n1': 'N1,16657,11763,222239780,16613,204944,0,191,4,1,2,250921215732,11584933,250921215732,68078060'},
{'id': 16673,
'n1': 'N1,16673,11763,222239780,16613,204944,0,256,5,1,2,250921215732,11584934,250921215732,68078061'},
{'id': 16791,
'n1': 'N1,16791,11763,222239780,16613,204944,0,374,6,1,2,250921215732,11584935,250921215732,68078062'},
{'id': 17009,
'n1': 'N1,17009,11763,222239780,16613,204944,0,473,7,1,2,250921215732,11584936,250921215732,68078063'},
{'id': 17011,
'n1': 'N1,17011,11763,222239780,16613,204944,0,568,8,1,2,250921215732,11584937,250921215732,68078064'},
{'id': 17013,
'n1': 'N1,17013,11763,222239780,16613,204944,0,614,9,1,2,250921215732,11584938,250921215732,68078074'},
{'id': 130094,
'n1': 'N1,130094,11763,222239780,16613,204944,0,667,10,1,2,250921215732,11584939,250921215732,68078075'},
{'id': 17015,
'n1': 'N1,17015,11763,222239780,16613,204944,0,799,11,1,2,250921215736,11585061,250921215736,68079482'},
{'id': 134751,
'n1': 'N1,134751,11763,222239780,16613,204944,0,869,12,1,2,250921215840,11598808,250921215840,68120384'},
{'id': 17017,
'n1': 'N1,17017,11763,222239772,134751,204944,0,38,1,1,2,250921215820,11593463,250921215820,68106466'},
{'id': 17019,
'n1': 'N1,17019,11763,222239772,134751,204944,0,75,2,1,2,250921215820,11593464,250921215820,68106467'},
{'id': 17089,
'n1': 'N1,17089,11763,222239772,134751,204944,0,137,3,1,2,250921215820,11593465,250921215820,68106468'},
{'id': 17201,
'n1': 'N1,17201,11763,222239772,134751,204944,0,192,4,1,2,250921215820,11593466,250921215820,68106469'},
{'id': 17234,
'n1': 'N1,17234,11763,222239772,134751,204944,0,239,5,1,2,250921215820,11593467,250921215820,68106470'},
{'id': 17400,
'n1': 'N1,17400,11763,222239772,134751,204944,0,290,6,1,2,250921215820,11593468,250921215820,68106471'},
{'id': 17402,
'n1': 'N1,17402,11763,222239772,134751,204944,0,417,7,1,2,250921215820,11593469,250921215820,68106472'},
{'id': 17404,
'n1': 'N1,17404,11763,222239772,134751,204944,0,465,8,1,2,250921215820,11593470,250921215820,68106473'},
{'id': 134737,
'n1': 'N1,134737,11763,222239772,134751,204944,0,492,9,1,2,250921215820,11593471,250921215820,68106474'},
{'id': 17406,
'n1': 'N1,17406,11763,222239772,134751,204944,0,596,10,1,2,250921215820,11593472,250921215820,68106475'},
{'id': 130135,
'n1': 'N1,130135,11763,222239772,134751,204944,0,620,11,1,2,250921215820,11593473,250921215820,68106476'},
{'id': 17408,
'n1': 'N1,17408,11763,222239772,134751,204944,0,651,12,1,2,250921215820,11593474,250921215820,68106477'},
{'id': 17410,
'n1': 'N1,17410,11763,222239772,134751,204944,0,708,13,1,2,250921215820,11593475,250921215820,68106478'},
{'id': 17412,
'n1': 'N1,17412,11763,222239772,134751,204944,0,775,14,1,2,250921215820,11593476,250921215820,68106479'},
{'id': 17414,
'n1': 'N1,17414,11763,222239772,134751,204944,0,807,15,1,2,250921215820,11593477,250921215820,68106480'},
{'id': 204942,
'n1': 'N1,204942,11763,222239772,134751,204944,0,930,16,1,2,250921215820,11593478,250921215820,68106481'},
{'id': 204943,
'n1': 'N1,204943,11763,222239772,134751,204944,0,953,17,1,2,250921215820,11593479,250921215820,68106482'},
{'id': 204944,
'n1': 'N1,204944,11763,222239772,134751,204944,0,982,18,1,2,250921215820,11593480,250921215820,68106483'}],
'Bus': [{'id': 222239780,
'num': 'EAL-0103',
'type': '1',
'a1': 'A1,10710,222239780,1,0,11763,1,121.559392,24.997787,28,129,215839,1,250921215839,34611707,250921215839,68119831',
'a2': 'A2,10710,222239780,1,0,11763,1,16613,0,215748,2,250921215748,87827503,250921215748,68085677',
'a3': None},
{'id': 222239773,
'num': 'EAL-0113',
'type': '1',
'a1': 'A1,10710,222239773,1,0,11763,0,121.584275,25.003060,32,260,215836,1,250921215835,34611614,250921215835,68117183',
'a2': 'A2,10710,222239773,1,0,11763,0,15347,0,215834,2,250921215834,87827757,250921215834,68116504',
'a3': None},
{'id': 222239772,
'num': 'EAL-0105',
'type': '1',
'a1': 'A1,10710,222239772,1,0,11763,1,121.576467,24.988335,0,046,215842,1,250921215841,34611766,250921215841,68121090',
'a2': 'A2,10710,222239772,1,0,11763,1,134751,1,215821,2,250921215820,87827683,250921215820,68106459',
'a3': None},
{'id': 222239783,
'num': 'EAL-0102',
'type': '1',
'a1': 'A1,10710,222239783,1,0,11763,0,121.544872,24.998672,0,251,215841,1,250921215841,34611757,250921215841,68120980',
'a2': 'A2,10710,222239783,1,0,11763,0,185809,0,215825,2,250921215825,87827705,250921215825,68109437',
'a3': None},
{'id': 222240411,
'num': 'EAL-1062',
'type': '1',
'a1': 'A1,10710,222240411,1,0,11763,1,121.538918,25.004917,0,188,215839,1,250921215839,34611692,250921215839,68119653',
'a2': 'A2,10710,222240411,1,0,11763,1,15380,0,215730,2,250921215730,87827418,250921215730,68076856',
'a3': None}]}
Q2_1 Accessing data#
請列印出你所抓到的資料時間。
Output sample:
2023-09-10 14:03:09
# YOUR CODE HERE
Q2_2: Number of bus#
請問在你所抓的時間點共有幾台公車?提示:你抓的時間和老師抓的時間可能不同,所以答案可能不同,公車數量可能會不同。
Output sample:
2023-09-10 14:03:09 There are 5 buses
# YOUR CODE HERE
Q2_3: 該線公車在該時間點共有幾個停靠站?#
Output Sample:
Number of stop: 85
# YOUR CODE HERE
Q2_4: Print out bus id#
印出資料抓取時間點的所有公車車牌號碼。
Output sample:
563-FZ
EAL-0102
380-U8
KKA-0668
for bus in route['Bus']:
# YOUR CODE HERE
Input In [8]
^
IndentationError: expected an indented block
## YOUR CODE SHOULD BE HERE
PART III: Reviewing with AI#
你只需要把以下問題拿去AI,把AI告訴你的結果,貼在下方的markdown cell即可。 但請你借此練習一下使用markdown,把AI告訴你的結果整理好。
Q3_1: 請用公車的資料說明,何謂List?#
Q3_2: 請用公車的資料說明,何謂Dictionary?#
Q3_3: 請用公車的資料說明,for-loop的機制與功能?#
Q3_4: 公車的資料除了以上題目外,你還可以用來做什麼?#
HINT 1:不單只考慮一條路線?
HINT 2:可否結合視覺化的功能?
HINT 3:是否可以結合其他資料?
HINT 4:可否動態呈現?
HINT 5:還可以做出什麼應用?