Python 變數、字串與整數

Python 入門(開發者向)

Jasmin Ludolf

Senior Data Science Content Developer

認識變數

  • 儲存並重複使用資訊
  • 易於閱讀與更新
  • 免去重複輸入

廚櫥裡的罐子

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

資料型別

  • 值的種類
    • 數字
    • 文字
    • True/False
  • Python 會自動指派資料型別

資料型別

Python 入門(開發者向)

字串資料型別

  • 字串(String):文字值
  • 可含字母、數字與標點

$$

"Hello, world!"
"20-minute pasta recipe"
Python 入門(開發者向)

字串資料型別

  • 字串:文字值
  • 可含字母、數字與標點
  • 可用單引號或雙引號
  • 內含撇號時用雙引號
"Chef's secret seasoning"
'Chef's secret seasoning'
Python 入門(開發者向)

字串變數

$$

ingredient_name

$$

  • 空白以底線、並用小寫命名
Python 入門(開發者向)

字串變數

$$

ingredient_name =
Python 入門(開發者向)

字串變數

$$

ingredient_name = "Tomatoes"
Python 入門(開發者向)

整數型別與變數

  • 整數(Integer):整數值
  • 不需使用引號

$$

ingredient_quantity = 2
Python 入門(開發者向)

印出變數

print(ingredient_name)
Tomatoes
print(ingredient_quantity)
2
Python 入門(開發者向)

調整變數

print(ingredient_quantity)
2

$$

ingredient_quantity = 1

print(ingredient_quantity)
1
Python 入門(開發者向)

一起來練習吧!

Python 入門(開發者向)

Preparing Video For Download...