[python] 常見資料類型 (data types)



常見的資料類型包括:
  • 字串 (string):str
  • 整數 (integer):int
  • 浮點數 (float):float
一般來說,你的電腦不知道該如何混和不同的資料類型,舉例來說:
>>> print(7 + 8)
會輸出:15

如果輸入:
>>> print("Hello " + "World")
會輸出:Hello World

但如果輸入:
>>> print(7 + "8")
會輸出:
Traceback (most recent call last):
    File "<stdin>" , line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

可以看到這裡的 TypeError 告訴我們加號不能使用在整數與字串之間。

留言