常見資料型別

Python 入門(開發者向)

Jasmin Ludolf

Senior Data Science Content Developer

小數資料型別

  • Integers:整數

$$

  • Floats:小數,如 1.5
  • 不需加引號

$$

$$

new_ingredient_quantity = 1.5
Python 入門(開發者向)

布林資料型別

$$

  • Boolean:True 或 False
  • 適合儲存是/否資訊
  • 開頭需大寫
  • 不加引號

$$

is_in_stock = True
is_in_stock = False
Python 入門(開發者向)

type 函式

  • 資料型別:
    • 字串
    • 整數
    • 浮點數
    • 布林值
type()
  • 回傳資料型別
Python 入門(開發者向)

type 函式

字串

ingredient_name = "tomatoes"

print(type(ingredient_name))
<class 'str'>

整數

ingredient_quantity = 2

print(type(ingredient_quantity))
<class 'int'>

浮點數

new_ingredient_quantity = 1.5

print(type(new_ingredient_quantity))
<class 'float'>

布林值

is_in_stock = True

print(type(is_in_stock))
<class 'bool'>
Python 入門(開發者向)

理解資料型別

  • 了解程式在做什麼、為什麼這樣做
  • type() 辨識型別
  • 判斷可執行的運算

Person working with code on multiple screens

1 Image by wutzkoh
Python 入門(開發者向)

運算子

  • 使用稱為運算子的符號進行計算

算術運算子:

  • + 加法
  • - 減法
  • * 乘法
  • / 除法

數值:

  • 與一般數學相同
    print(2 + 1.5)
    
3.5

字串:

  • 只有 +* 可用

"Hi" + "There" = "HiThere"

"Hi" * 3 = "HiHiHi"

Python 入門(開發者向)

一起來練習吧!

Python 入門(開發者向)

Preparing Video For Download...