變數資料型別

Python 金融入門

Adina Howe

Professor

Python 資料型別

變數型別 範例
字串 'hello world'
整數 40
浮點數 3.1417
布林值 True 或 False
Python 金融入門

變數型別

變數型別 範例 縮寫
字串 'Tuesday' str
整數 40 int
浮點數 3.1417 float
布林值 True 或 False bool
Python 金融入門

變數的資料型別:type()

要辨識型別,可以使用函式 type()

type(variable_name)
pe_ratio = 40
print(type(pe_ratio))
<class 'int'>
Python 金融入門

變數運算

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
Python 金融入門

轉換變數型別

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 金融入門

一起來練習吧!

Python 金融入門

Preparing Video For Download...