Python 的邏輯運算符 and、or、and not(邏輯與、析取、否定)

商業

Python 提供了邏輯運算符來執行邏輯(布爾)操作。(and,or,not)
用於描述if語句中多個條件之間的關係。

本節介紹以下內容。

  • 路口:and
  • 邏輯加法:or
  • 否認:not
  • and,or,not運算符優先級

此外,以下幾點被解釋為註意事項。

  • 非 bool 類型對象的邏輯運算符
  • and,or這些返回值不一定是 bool 類型。
  • 短路(短路評估)

路口:and

並返回兩個值的邏輯乘積。

print(True and True)
# True

print(True and False)
# False

print(False and True)
# False

print(False and False)
# False

事實上,它通常不是用於 true 或 false,而是用於使用比較運算符的條件表達式。供您參考,比較運算符如下。

  • <
  • >
a = 10
print(0 < a)
# True

print(a < 100)
# True

print(0 < a and a < 100)
# True

並且可以按如下方式連接。

print(0 < a < 100)
# True

邏輯加法:or

或返回兩個值的邏輯或。

print(True or True)
# True

print(True or False)
# True

print(False or True)
# True

print(False or False)
# False

否認:not

not” 返回值的否定;true 和 false 顛倒。

print(not True)
# False

print(not False)
# True

and,or,not運算符優先級

這些邏輯運算符的優先順序如下:not 是最高的。

  1. not
  2. and
  3. or

在下面的示例代碼中,上面的表達式被解釋為下面的表達式。由於額外的括號沒有問題,像這個例子這樣的情況可能更容易清楚地描述它們。

print(True or True and False)
# True

print(True or (True and False))
# True

如果要操作或之前和,使用括號()。

print((True or True) and False)
# False

<,>這些比較運算符的優先級甚至更高。因此,每個比較操作都不需要括號,就像上面的例子一樣。

print(0 < a and a < 100)
# True

有關 Python 中運算符優先級的摘要,請參閱下面的官方文檔。

非 bool 類型對象的邏輯運算符

With these logical operators, not only bool types (true, false), but also numbers, strings, lists, etc. are processed as boolean values.

以下對像在 Python 的邏輯操作中被認為是錯誤的。

  • 常量定義為假:None,false
  • 數字類型中的零:0,0,0j,Decimal(0),Fraction(0, 1)
  • 空序列或集合:',(),[],{},set(),range(0)

所有其他值都被認為是真的。

函數 bool() 可用於獲取對象的布爾值。請注意,字符串 ‘0’ 或 ‘False’ 被視為真。

print(bool(10))
# True

print(bool(0))
# False

print(bool(''))
# False

print(bool('0'))
# True

print(bool('False'))
# True

print(bool([]))
# False

print(bool([False]))
# True

要將字符串中的 ‘0’ 或 ‘false’ 處理為 false,請使用 distutils.util.strtobool()。

and,or這些返回值不一定是 bool 類型。

下面是一個不是 bool 類型的對象示例,顯示了每個運算符對數值的結果。

x = 10  # True
y = 0  # False

print(x and y)
# 0

print(x or y)
# 10

print(not x)
# False

從上面的例子中可以看出,Python 中的 and 和 or 不返回 bool 類型的 true 或 false,而是根據 true 或 false 返回左側或右側的值。該示例是數字,但同樣適用於其他類型,例如字符串和列表。順便說一句, not 返回 bool 類型的 true 或 false。

and 和 or 的返回值定義如下。

The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

6.11. Boolean operations — Expressions — Python 3.10.1 Documentation

當左右表達式的值分別為真假時,返回值就容易理解了。另一方面,如果兩者都為真或都為假,則返回值會因順序而異。

如果在if語句等中作為條件表達式使用,結果被判斷為布爾值並進行處理,所以不用擔心,但如果使用返回值進行進一步處理,則需要小心。

x = 10  # True
y = 100  # True

print(x and y)
# 100

print(y and x)
# 10

print(x or y)
# 10

print(y or x)
# 100
x = 0  # False
y = 0.0  # False

print(x and y)
# 0

print(y and x)
# 0.0

print(x or y)
# 0.0

print(y or x)
# 0

print(bool(x and y))
# False

如果你想把它當作真或假,你可以像上一個例子那樣做。
bool(x and y)

和和或的返回值總結在下表​​中。

xyx and yx or y
truefalseyx
falsetruexy
truetrueyx
falsefalsexy

短路(短路評估)

從上表中可以看出,如果 x 在 x 和 y 中為假,或者如果 x 在 x 或 y 中為真,則無論 y 的值如何,返回值都將為 x。

在這種情況下,不計算 y。

and,or請注意,如果您調用這些進程右側的函數或方法進行某些處理,則可能無法根據左側的結果執行該進程。

def test():
    print('function is called')
    return True

print(True and test())
# function is called
# True

print(False and test())
# False

print(True or test())
# True

print(False or test())
# function is called
# True
Copied title and URL