Python 金融入门
Adina Howe
Professor
| 变量类型 | 示例 |
|---|---|
| 字符串 | 'hello world' |
| 整数 | 40 |
| 浮点数 | 3.1417 |
| 布尔值 | True 或 False |
| 变量类型 | 示例 | 缩写 |
|---|---|---|
| 字符串 | 'Tuesday' | str |
| 整数 | 40 | int |
| 浮点数 | 3.1417 | float |
| 布尔值 | True 或 False | bool |
要识别类型,可使用函数 type():
type(variable_name)
pe_ratio = 40
print(type(pe_ratio))
<class 'int'>
x = 5
print(x * 3)
15
print(x + 3)
8
y = 'stock'
print(y * 3)
'stockstockstock'
print(y + 3)
TypeError: must be str, not int
pi = 3.14159
print(type(pi))
<class 'float'>
pi_string = str(pi)
print(type(pi_string))
<class 'str'>
print('I love to eat ' + pi_string + '!')
I love to eat 3.14159!
Python 金融入门