一、布爾類型描述
布爾類型是計算機中最基本的類型,它是計算機二進制世界的展現,一切都是
0
和
1
。Python中的布爾類型隻有兩種值:
True
和
False
。
(注意:首字母都是大寫,與C++、JavaScript中的小寫有所不同)
布爾類型回答的是
是非
問題,那麼什麼情況下是
True
,什麼情況下是
False
呢? Python裡面實作了一個
類型對象
叫做
bool
,bool是一個
int
的子類,内置的
True
和
False
就是bool僅有的兩個執行個體對象。
python 中布爾值使用常量 True 和 False來表示;注意 T F 大小寫
bool
是
int
的子類(繼承
int
),故
True == 1 False == 0
是會傳回
Ture
bool
類型隻有兩種狀态真或假
使用bool我們就可以對對象進行布爾真假判斷:
為假的情況有:
print(bool(None))
print(bool(0))
print(bool(0.0))
print(bool(0j)) # 虛數
print(bool(Decimal(0))) # 0 from decimal import Decimal
print(bool(Fraction(0, 1))) # 0 from fractions import Fraction
print(bool('')) # 空字元串
print(bool({})) # 空字典
print(bool(set())) # 空集合
print(bool(())) # 空元組
print(bool([])) # 空清單

二、布爾運算
優先級:not > and > or
運算 | 表達式 | 結果 | 說明 |
或運算 | | 如果x為False則取決于y;如果x為True則不考慮y | (1) |
與運算 | | 如果x為False則不考慮y;如果x為True則取決于y | (2) |
非運算 | | 如果x為False則為True,否則為False | (2) |
說明:
(1)
or
是一種“短路運算符”,隻有當第一個為False時才去驗證第二個。即:兩個變量隻要有一個為True則為True。
(2)
and
也是種“短路運算符”,隻有當第一個為True時才去驗證第二個。即:兩個變量都為True時結果才為True。
(3)
not
的優先級比非布爾運算符底,是以
not a == b
解釋為
not (a == b)
,并且
a == not b
是文法錯誤。
print(1 > 2 or 2 > 1)
print(1 < 2 and 1 < 3)
print(not 1 == 2)
三、比較運算
前面提到,布爾值反應的是“是非”,有比較才有是非。Python中有8中比較運算。它們有相同的優先級,比布爾運算的優先級高。比較運算符可以任意的連寫,比如:
x < y <=z
相當于
x < y and y <= z
。
運算 | 含義 |
| 小于 |
| 小于等于 |
| 大于 |
| 大于等于 |
| 等于 |
| 不等于 |
| 是對象 |
| 不是對象 |
四、總結
布爾類型(True, False)表示“是非”,是比較運算的結果,是條件判斷的結果,進而決定程式的流程和分支走向。
預設情況下,所有類型都可以轉化為布爾類型
from decimal import Decimal
from fractions import Fraction
print(bool(None))
print(bool(0))
print(bool(0.0))
print(bool(0j)) # 虛數
print(bool(Decimal(0))) # 0 from decimal import Decimal
print(bool(Fraction(0, 1))) # 0 from fractions import Fraction
print(bool('')) # 空字元串
print(bool({})) # 空字典
print(bool(set())) # 空集合
print(bool(())) # 空元組
print(bool([])) # 空清單
print(1 > 2 or 2 > 1)
print(1 < 2 and 1 < 3)
print(not 1 == 2)
五、源碼
bool(x) -> bool
Returns True when the argument x is true, False otherwise.
The builtins True and False are the only two instances of the class bool.
The class bool is a subclass of the class int, and cannot be subclassed.
class bool(int):
"""
bool(x) -> bool
Returns True when the argument x is true, False otherwise.
The builtins True and False are the only two instances of the class bool.
The class bool is a subclass of the class int, and cannot be subclassed.
"""
def __and__(self, *args, **kwargs): # real signature unknown
""" Return self&value. """
pass
def __init__(self, x): # real signature unknown; restored from __doc__
pass
@staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass
def __or__(self, *args, **kwargs): # real signature unknown
""" Return self|value. """
pass
def __rand__(self, *args, **kwargs): # real signature unknown
""" Return value&self. """
pass
def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass
def __ror__(self, *args, **kwargs): # real signature unknown
""" Return value|self. """
pass
def __rxor__(self, *args, **kwargs): # real signature unknown
""" Return value^self. """
pass
def __xor__(self, *args, **kwargs): # real signature unknown
""" Return self^value. """
pass