布尔值——逻辑数据类型

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 的数据类型

Passons à la pratique !

Python 的数据类型

Preparing Video For Download...