布林值:邏輯型別

Python 的資料型別

Jason Myers

Instructor

布林值作為資料型別

  • True
  • False

注意大小寫。從 Python 切換到其他語言時,很容易被這點絆住。

out_of_cookies = True
if out_of_cookies:
    print("Run to the store NOW!")
Run to the store NOW!
Python 的資料型別

Truthy 與 Falsey

  • Truthy 值在判斷時會回傳 true
  • Falsey 值在判斷時會是 false
apples=2
if apples:
     print("We have apples.")
"We have apples."
apples=0
if apple:
     print('We have apples.')
Python 的資料型別

Truthy 與 Falsey

Truthy

  • 1
  • "Cookies"
  • ["Cake", "Pie"]
  • {"key": "value"}

Falsey

  • 0
  • ""
  • []
  • {}
  • None
Python 的資料型別

運算子:布林評估情境

cookie_qty == 3
  • == 等於
  • != 不等於
  • < 小於
  • <= 小於或等於
  • > 大於
  • >= 大於或等於
Python 的資料型別

浮點數的近似問題

x = 0.1 + 1.1
x == 1.2
False
print(x)
1.2000000000000002

比較浮點數是否相等要特別小心!

Python 的資料型別

一起來練習吧!

Python 的資料型別

Preparing Video For Download...