AU Fundamentals of Python Programming-110-考試重點提示¶
IPO-I 輸入參數¶
input()
輸入一個字串參數¶
instr = input()
print(instr)
---------------------------------------------------------------------------
StdinNotImplementedError Traceback (most recent call last)
/tmp/ipykernel_713/3106458668.py in <module>
----> 1 instr = input()
2 print(instr)
~/.local/lib/python3.8/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt)
1001 """
1002 if not self._allow_stdin:
-> 1003 raise StdinNotImplementedError(
1004 "raw_input was called, but this frontend does not support input requests."
1005 )
StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
輸入一個浮點數參數¶
instr = input()
val = float(instr)
print(val)
#合併輸入和浮點數轉換
val = float(input())
print(val)
輸入二個字串參數¶
instr = input()
paras = instr.split()
a = paras[0]
b = paras[1]
print(f"{a} {b}")
#合併輸入和字串分割
a, b = input().split()
print(f"{a} {b}")
輸入三個整數參數¶
instr = input()
paras = instr.split()
a = int(paras[0])
b = int(paras[1])
c = int(paras[2])
print(f"a={a} b={b} c={c}")
#合併輸入和數字轉換
a, b, c = map(int, input().split())
print(f"a={a} b={b} c={c}")
IPO-O 格式化輸出¶
https://docs.python.org/3/tutorial/inputoutput.html
格式化輸出多個變數¶
year = 2016
event = 'Referendum'
print(f'Results of the {year} {event}')
控制格式化輸出的寛度和排列¶
9:寛度9
2.2: 整數寛度2, 小數寛度2,
yes_votes = 42_572_654
no_votes = 43_132_495
percentage = yes_votes / (yes_votes + no_votes)
print(f'{yes_votes:9} YES votes {percentage:2.2%}')
舊式格式化輸出¶
Python 有 3 種格式化字串輸出的方法: f-string, str.format, %-formatting。第1種 f-string是主流方式。第2-3稱為舊學校 “Old-School” 字串格式化方法。
同學閱讀舊的書籍或比較舊的網站內容,往往還會看到這兩種方式。可以看,但不鼓勵同學使用舊的方式寫程式。
https://realpython.com/lessons/old-school-string-formatting-python/
str.format格式化輸出¶

quantity=6; item='bananas'; price=1.74
print('{0} {1} cost ${2}'.format(quantity,item,price ))
%-formatting格式化輸出¶

quantity=6; item='bananas'; price=1.74
print('%d %s cost $%.2f' % (quantity,item,price ))
IPO-P IF條件式¶

https://docs.python.org/3/tutorial/inputoutput.html
單一判斷的if 條件式¶
val = 3
if val % 2 == 0:
print("Even")
else:
print("Odd")
雙重判斷的if 條件式¶
month = 6
if 3<= month and month <= 5:
print ("Spring")
elif 6<= month and month <= 8:
print ("Summer")
elif 9<= month and month <= 11:
print ("Autumn")
else:
print ("Winter")
month = 6
if 3<= month <= 5:
print ("Spring")
elif 6<= month <= 8:
print ("Summer")
elif 9<= month <= 11:
print ("Autumn")
else:
print ("Winter")
IPO-P For迴圈¶

使用List的for迴圈¶
mylist = ['dog', 'cat', 'cow']
for x in mylist:
print(x)
使用range()函數的for迴圈¶
numbers = range(1,10)
for a in numbers:
for b in numbers:
print(f'{a:1d}*{b:1d} ={a*b:2d}', end=' ')
print()
IPO-P 字串的函數和排序¶
https://docs.python.org/zh-tw/3/library/stdtypes.html#text-sequence-type-str
str.format(*args, **kwargs)
str.split(sep=None, maxsplit=-1)
str.strip([chars])
str.upper()
str.lower()
str.replace(old, new[, count]) 返回字串的副本,其中出現的所有子字串 old 都將被替換為 new。如果給出了可選參數 count,則只替換前 count 次出現。
字串的取代¶
str.replace(old, new[, count]) 返回字串的副本,其中出現的所有子字串 old 都將被替換為 new。如果給出了可選參數 count,則只替換前 count 次出現。
txt = "I like bananas"
x = txt.replace("bananas", "apples")
print(x)
str.replace(old, new[, count]) 返回字串的副本,其中出現的所有子字串 old 都將被替換為 new。如果給出了可選參數 count,則只替換前 count 次出現。
字串的排序¶
sorted()函數
a = ["1234", "5678", "3333", "7777", "9999"]
x = sorted(a)
print(x)
a = ["1234", "5678", "3333", "7777", "9999"]
x = sorted(a, reverse=True)
print(x)
IPO-P 亂數的使用¶
https://docs.python.org/3/library/random.html
指定亂數起點¶
random.seed(a=None)
在指定範圍內生成整數亂數randrange()¶
random.randrange(stop)
random.randrange(start, stop[, step])
from random import randrange
print(randrange(10))
在指定範圍內生成浮點數亂數randrange()¶
random.random() Return the next random floating point number in the range [0.0, 1.0).
random.uniform(a, b) Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a.
import random
print(random.random())
print(random.uniform(1.5, 1.9))
IPO-P 月曆的使用¶
https://docs.python.org/3/library/calendar.html
每個月有幾天,第一天是星期幾 calendar.monthrange()¶
import calendar
first_day, number_of_days = calendar.monthrange(2021, 9)
print(first_day, number_of_days)