[英]Boolean Algebra - Why is not(True and False) True?
我正在寫《很難學習python》一書。 在練習27( http://learnpythonthehardway.org/book/ex27.html )上,它以布爾代數開始。
所以我的問題是:為什么not(True and False)
True?
以我的理解,它應該與False and True
相同。
您的解釋不正確,請參閱戴摩根定律 ; 具體地說,連詞的否定是否定的析取 。
not (True and False)
( 一個合取符 == not(a and b)
的取反 )等於False or True
( 這些取反 == (not a) or (not b)
); 注意從and
切換到or
!
您還可以制定以下步驟:
not(True and False)
True and False
-> False
not(False)
-> True
。 not (True and False)
首先簡化為not (False)
,然后進一步簡化為True
。
您應該看看De Morgan的定律 ,該定律指出(非正式地):
“不是(A和B)”與“(不是A)或(不是B)”相同
另外,“不是(A或B)”與“(不是A)和(不是B)”相同。
所以在你的情況下, not(True and False)
與not(True) or not(False)
相同,與False or True
相同,即True
讓我們逐步進行此操作:
(True and False)
評估為(False)
not (False)
評估為True
很簡單:)
按照操作順序,首先要解決括號內的內容,並使用變量以這種方式查看它:
a == True and False == False
not(True and False) == not(a) == not(False) == True
您在想的是:
not(True) and not(False)
這確實是錯誤的,因為:
a == not(True) == False
b == not(False) == True
not(True) and not(False) == a and b == False and True == False
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.