變數

Python 金融入門

Adina Howe

Professor

定義變數

在程式中儲存可引用的資訊

  • 範例msft_stockinterest_ratebonds
  • 名稱 = 值:interest_rate = 0.05

 

變數命名

  • 可用大寫/小寫字母、數字、底線
  • 不能以數字開頭
  • 部分名稱是 Python 的保留字(如 classtype),請避免使用
Python 金融入門

在 Python 中定義變數

# 正確
day_2 = 5   

# 錯誤,變數名稱以數字開頭
2_day = 5   
Python 金融入門

用變數評估股價趨勢

$$ \text{本益比} = \frac{\text{市價}}{\text{每股盈餘}} $$

price = 200
earnings = 5

pe_ratio = price / earnings
print(pe_ratio)
40
Python 金融入門

一起來練習吧!

Python 金融入門

Preparing Video For Download...