# Python 金融資訊系統期末考筆記 ## 一、Python基本觀念 ### 1.冒號":" > * 冒號用於開始程式區塊,如if(else)、for和def。 > * 冒號之後的內容應該縮排,慣例上使用四個空格。 ```python= if condition: # 若條件達成執行的動作 else: # 若條件未達成執行的動作 ``` ### 2.空行"enter" > * 在不同的程式區塊,如if(else)、for、def之間添加一個空行,以提高程式碼的可讀性。 ```python= def function1(): # 呼叫函式function1時執行的動作 def function2(): # 呼叫函式function2時執行的動作 ``` ### 3.空格"space" > * 在運算符號前後加空格,以提高程式碼的可讀性。 ```python= result = a + b # 五告讚 result=a+b # 嗨 ``` ### 4.等於"=" > * 將右側數值指定給左側使用一個 = > * 需要判斷左側是否等於右側使用兩個 == ```python= result = 10 # 將result變成10 if result == 10: # 判斷result是否為10 # 若條件達成執行的動作 else: # 若條件未達成執行的動作 ``` ## 二、期末題型 ### 使用 for 迴圈計算1~10000中5的倍數和 ![IMG_9458](https://hackmd.io/_uploads/rJCW0m7u6.jpg) ```python= total_value = 0 # 將總和初始化 for i in range(1, 10001): # 將i當作1運行此區塊,結束後將i當作2再運行一次,重複循環到i當作10000運行結束 if i % 5 == 0: # 判斷i除以5是否為0(i是否為5的倍數) total_value += i # 如果是,加到總和中 # 沒有else部分,所以當i不是5的倍數時不會執行任何動作 print("1~10000 中 5 的倍數和為:", total_value) # 印出結果 ``` ### 建構自訂函數 bond_value 計算債券價格 ![IMG_9458](https://hackmd.io/_uploads/SyBFC7X_p.jpg) ```python= import math # 匯入 math 模組,用於數學計算 def bond_value(face_value, maturity, yield_rate): # 定義一個名為bond_value的動作(函式),此函式需要用到括號中的三個變數 total_value = 0 # 每次執行此函式時將總和初始化 for t in range(1, maturity + 1): # 使用 for 迴圈計算每年的現金流,計算方式如下 coupon_payment = face_value * min(0.01 * t, 0.2) # 計算每年的票息支付 discounted_payment = coupon_payment / (1 + yield_rate)**t # 計算每年的現值 total_value += discounted_payment # 加到總和中 total_value += face_value / (1 + yield_rate)**maturity # 加上到期本金的現值 return total_value # 函式到此結束並送出函式運行結果(總債券價格) face_value = 100 maturity = 20 yield_rate = 0.06 # 在呼叫函式之前先給定所需變數,20年債券,債券面額100,年殖利率6% result = bond_value(face_value, maturity, yield_rate) # 呼叫函式並將其送出結果(return)保存在result中 print("債券價格為: " , str(round(result, 2))) # 將result取到小數點後第二位並印出結果 ``` ### 使用 random 模組隨機抽取5個不重複整數,計算最大值、最小值、中位數、四分位數 ![IMG_9458](https://hackmd.io/_uploads/ByyGkEQuT.jpg) ```python= import random # 匯入 random 模組,用於隨機抽取 import numpy as np # 匯入 numpy as np 模組,用於計算四分位數(中位數) random_numbers = random.sample(range(1, 1001), 5) # 隨機抽取5個不重複的整數並保存在random_numbers數列中 max_value = max(random_numbers) # 計算random_numbers數列中的最大值,並保存在max_value中 min_value = min(random_numbers) # 計算random_numbers數列中的最小值,並保存在min_value中 sorted_numbers = sorted(random_numbers) # 將數字由小到大排序並保存在sorted_numbers數列中 median_value = np.percentile(sorted_numbers, 50) # 計算中位數(第二四分位數) q1 = np.percentile(sorted_numbers, 25) # 計算第一四分位數 q3 = np.percentile(sorted_numbers, 75) # 計算第三四分位數 print("隨機抽取的5個數字:", random_numbers) print("最大值:", max_value) print("最小值:", min_value) print("中位數:", median_value) print("第一四分位數:", q1) print("第三四分位數:", q3) # 印出結果 ``` ### 隨機抽取整數,使用自訂函數 cal_num 計算新的數字 ![IMG_9458](https://hackmd.io/_uploads/HyIglN7up.jpg) ```python= import random # 匯入 random 模組,用於隨機抽取 import math # 匯入 math 模組,用於數學計算 def cal_num(int_num): # 定義一個名為cal_num的動作(函式),此函式需要用到括號中的變數 if int_num >= 50: # 判斷int_num是否大於等於50 return math.sqrt(int_num) # 如果是,將int_num開根號並送出結果 else: return math.exp(int_num) # 否則,將int_num取自然對數並送出結果 int_num = random.randint(1, 100) # 在呼叫函式之前先以隨機抽取1到100的整數給定所需變數 new_value = cal_num(int_num) # 呼叫cal_num函式並將運行結果保存至new_value print("隨機抽取的整數:", int_num) # 印出隨機抽取的整數 print("新的數字:", new_value) # 印出函式計算結果 ``` ### 複利計算累積本利和 ![IMG_9458](https://hackmd.io/_uploads/S18XxVm_p.jpg) ```python= def calculate_accumulated_amount(principal, annual_rate, years): # 定義一個名為calculate_accumulated_amount的動作(函式),此函式需要用括號中的三個變數 accumulated_amount = 0 # 初始化累積本利和 for year in range(1, years + 1): # 使用 for 迴圈計算每年的累積本利和 accumulated_amount += principal * (1 + annual_rate/100)**year # 更新累積本利和 return accumulated_amount # 返回累積本利和 P = 900000 annual_rate = 5 years = 20 # 在呼叫函式之前先給定所需變數,每年存入的金額、年利率、存款的年數 accumulated_amount = calculate_accumulated_amount(P, annual_rate, years) # 計算累積本利和 print(f"20年後的累積本利和為: {accumulated_amount:.2f} 元") # 印出結果 ``` ## 四、補充題型 ### x加到y ```python= x_value = 3 y_value = 10 total_sum = 0 for i in range(x_value, y_value + 1): total_sum += i print("從", x_value, "加到", y_value, "的總和為:", total_sum) ``` ### 指定1x2x3x4x5 ```python= x_value = 1 y_value = 5 result = 1 # 初始值設為1,因為任何數字乘以1都是其本身 for i in range(x_value, y_value + 1): result *= i print(x_value,”連乘到”,y_value,”的值為”,result) ```