1. 程式人生 > >python中not,and,or的優先順序問題及用法

python中not,and,or的優先順序問題及用法

  • 優先順序問題

– 在學習python中,發現其中的Boolean Operations — and, or, not的用法與常見的布林代數用法有很大不同,其中確定優先順序是判斷一個表示式結果的關鍵,下面給出官方標準庫的解釋:

這裡寫圖片描述

These are the Boolean operations ,ordered by ascending priority.官方標準庫裡的解釋。這些布林操作,按升序提升優先順序。

即得到優先順序關係:or<and<not,同一優先順序預設從左往右計算。
  • python指令參考
    由於本機使用的是python2.7.13的版本,故查詢了對應版本的指令介紹,其中也定義了Boolean operations,如下圖:

這裡寫圖片描述

這裡,我們將對應具體的表示式進行分析討論其中的優先順序問題。例如對於 “a or b and c or d”而言,根據 or_test ::= and_test | or_test “or” and_test,在語法樹上,自頂向下看,解析為 (a or b and c) or d ,這個形式為一個or_test, 符合 or_test “or” and_test 這個形式。這個應該沒異議~//或者可嘗試解析為a or (b and c or d),看一下能否後續分解。

第二層分別看 (a or b and c) 和 d ,d是一個comparison, 同時也是一個and_test, 同時也符合 or_test形式中or_test “or” and_test 的右部 and_test形式。d已經是原子了就不向下看了。

而(a or b and c) 是第一層的or_test的左部,只能按一個or_test形式來解析,因為,假如串”a or b and c” 按and_test形式來解析,那隻能匹配形式 and_test ::= and_test “and” not_test ,也就是(a or b) and c, “and”左邊必須為另一個and_test形式,但串 “a or b” 無法匹配進and_test形式。,按or_test形式來解析,則可以匹配 a or (b and c), 這是一個or_test,”or”的左邊是一個or_test形式(單獨and_test同時也可以匹配or_test形式),右邊是一個and_test形式。因此 語法樹被解析為 ( (a) or ( (b) and (c) ) ) or (d) 。

那求值的過程,就比較容易理解了,要求or表示式的值,先要求其中的左右不分and表示式的值,求and表示式的值,先要求and左右部的comparison原子或另一個and表示式的值。

最後,可嘗試來分析a or (b and c or d)這種分解情況,滿足or_test形式,即兩邊都為and_test形式。然後分析(b and c or d),因為(b and c or d)為and_test形式,即內部可分為b 和 (c or d),然而(c or d)並不能滿足not_test 形式,即該表示式只能按照第一種情況分配。

  • False的定義及範例如下:

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:

None

False

zero of any numeric type, for example, 0, 0L, 0.0, 0j.

any empty sequence, for example, ”, (), [].

any empty mapping, for example, {}.

instances of user-defined classes, if the class defines a nonzero() or len() method, when that method returns the integer zero or bool value False. [1]
All other values are considered true — so objects of many types are always true.

Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.)

  • 求值技巧

關於求值的順序,先左部,再右部,還要加入“短路”機制,說明這點的文件原文:
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.

對應同一優先順序,我個人的理解為:or的目的是找到作用範圍內第一個True或最後一個False,and的目的是找到作用範圍第一個False或最後一個True。(其中,作用範圍內的概念必須明確)。

這裡寫圖片描述

#對於and而言,第一行返回(作用範圍內)第一個假值,第二行返回最後一個正值
#對於or而言,第一行返回(作用範圍內)第一個真值,第二行返回最後一個假值

對於包含and,not,or的表示式,通過優先順序關係,處理起來也是較為簡單的。利用短路邏輯規則:表示式從左至右運算,若 or 的左側邏輯值為 True ,則短路 or 後所有的表示式(不管是 and 還是 or),直接輸出 or 左側表示式 。表示式從左至右運算,若 and 的左側邏輯值為 False ,則短路其後所有 and 表示式,直到有 or 出現,輸出 and 左側表示式False到 or 的左側,參與接下來的邏輯運算。若 or 的左側為 False ,或者 and 的左側為 True 則不能使用短路邏輯。

這裡寫圖片描述

最後,對於not的定義比較簡單,如果x為False則not x 為True,反之亦然。

這裡寫圖片描述

注:以下連結分別為 The Python Language Reference / The Python Standard Library/參考知乎問題中江歡dalao的優質回答。