变量

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