您好,登錄后才能下訂單哦!
這篇“Python運算符之邏輯門是什么及怎么用”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Python運算符之邏輯門是什么及怎么用”文章吧。
邏輯門是任何數字電路的基本構建塊。它需要一兩個輸入并根據這些輸入產生輸出。輸出可能為高 (1) 或低 (0)。邏輯門使用二極管或晶體管實現。它也可以使用真空管、光學元件、分子等電磁元件構成。在計算機中,大多數電子電路都是由邏輯門組成的。邏輯門用于執行計算、數據存儲或展示面向對象編程(尤其是繼承的力量)的電路。
定義了七個基本邏輯門:與門、或門、非門、與非門、或非門、異或門、異或門。
如果兩個輸入都為 1,與門的輸出為 1,否則為 0。
# 說明與門工作的 Python3 程序 def AND (a, b): if a == 1 and b == 1: return True else: return False # 驅動程序代碼 if __name__=='__main__': print(AND(1, 1)) print("+---------------+----------------+") print(" | AND Truth Table | Result |") print(" A = False, B = False | A AND B =",AND(False,False)," | ") print(" A = False, B = True | A AND B =",AND(False,True)," | ") print(" A = True, B = False | A AND B =",AND(True,False)," | ") print(" A = True, B = True | A AND B =",AND(True,True)," | ")
輸出:
True
+---------------+----------------
| AND Truth Table | Result |
A = False, B = False | A AND B = False |
A = False, B = True | A AND B = False |
A = True, B = False | A AND B = False |
A = True, B = True | A AND B = True |
如果兩個輸入都是 1,與非門(取反)輸出 0,否則輸出 1。
# 說明與非門工作的Python3程序 def NAND (a, b): if a == 1 and b == 1: return False else: return True # 驅動程序代碼 if __name__=='__main__': print(NAND(1, 0)) print("+---------------+----------------+") print(" | NAND Truth Table | Result |") print(" A = False, B = False | A AND B =",NAND(False,False)," | ") print(" A = False, B = True | A AND B =",NAND(False,True)," | ") print(" A = True, B = False | A AND B =",NAND(True,False)," | ") print(" A = True, B = True | A AND B =",NAND(True,True)," | ")
輸出:
True
+---------------+----------------
| NAND Truth Table | Result |
A = False, B = False | A AND B = True |
A = False, B = True | A AND B = True |
A = True, B = False | A AND B = True |
A = True, B = True | A AND B = False |
如果兩個輸入中的任何一個為 1,或門的輸出為 1,否則為 0。
# Python3 程序來說明或門的工作 def OR(a, b): if a == 1 or b ==1: return True else: return False # 驅動程序代碼 if __name__=='__main__': print(OR(0, 0)) print("+---------------+----------------+") print(" | OR Truth Table | Result |") print(" A = False, B = False | A OR B =",OR(False,False)," | ") print(" A = False, B = True | A OR B =",OR(False,True)," | ") print(" A = True, B = False | A OR B =",OR(True,False)," | ") print(" A = True, B = True | A OR B =",OR(True,True)," | ")
輸出:
False
+---------------+----------------+
| OR Truth Table | Result |
A = False, B = False | A OR B = False |
A = False, B = True | A OR B = True |
A = True, B = False | A OR B = True |
A = True, B = True | A OR B = True |
門 如果輸入中的任何一個不同,異或門的輸出為 1,如果它們相同,則輸出為 0。
# 說明異或門工作的 Python3 程序 def XOR (a, b): if a != b: return 1 else: return 0 # 驅動程序代碼 if __name__=='__main__': print(XOR(5, 5)) print("+---------------+----------------+") print(" | XOR Truth Table | Result |") print(" A = False, B = False | A XOR B =",XOR(False,False)," | ") print(" A = False, B = True | A XOR B =",XOR(False,True)," | ") print(" A = True, B = False | A XOR B =",XOR(True,False)," | ") print(" A = True, B = True | A XOR B =",XOR(True,True)," | ")
輸出:
0
+---------------+----------------+
| XOR Truth Table | Result |
A = False, B = False | A XOR B = 0 |
A = False, B = True | A XOR B = 1 |
A = True, B = False | A XOR B = 1 |
A = True, B = True | A XOR B = 0 |
它作為一個反相器。它只需要一個輸入。如果輸入為 1,它會將結果反轉為 0,反之亦然。
# Python3 程序來說明非門的工作原理 def NOT(a): return not a # 驅動程序代碼 if __name__=='__main__': print(NOT(0)) print("+---------------+----------------+") print(" | NOT Truth Table | Result |") print(" A = False | A NOT =",NOT(False)," | ") print(" A = True, | A NOT =",NOT(True)," | ")
輸出:
1
+---------------+----------------+
| NOT Truth Table | Result |
A = False | A NOT = 1 |
A = True, | A NOT = 0 |
NOR 門(取反的 OR)如果兩個輸入都為 0,則輸出為 1,否則為 0。
# Python3程序來說明或非門的工作 def NOR(a, b): if(a == 0) and (b == 0): return 1 elif(a == 0) and (b == 1): return 0 elif(a == 1) and (b == 0): return 0 elif(a == 1) and (b == 1): return 0 # 驅動程序代碼 if __name__=='__main__': print(NOR(0, 0)) print("+---------------+----------------+") print(" | NOR Truth Table | Result |") print(" A = False, B = False | A NOR B =",NOR(False,False)," | ") print(" A = False, B = True | A NOR B =",NOR(False,True)," | ") print(" A = True, B = False | A NOR B =",NOR(True,False)," | ") print(" A = True, B = True | A NOR B =",NOR(True,True)," | ")
輸出:
1
+---------------+----------------+
| NOT Truth Table | Result |
A = False | A NOT = 1 |
A = True, | A NOT = 0 |
XNOR 門(取反的 XOR)輸出 1,兩個輸入相同,如果兩者不同,則輸出 0。
# Python3 程序來說明非門的工作原理 def XNOR(a,b): if(a == b): return 1 else: return 0 # 驅動程序代碼 if __name__=='__main__': print(XNOR(1,1)) print("+---------------+----------------+") print(" | XNOR Truth Table | Result |") print(" A = False, B = False | A XNOR B =",XNOR(False,False)," | ") print(" A = False, B = True | A XNOR B =",XNOR(False,True)," | ") print(" A = True, B = False | A XNOR B =",XNOR(True,False)," | ") print(" A = True, B = True | A XNOR B =",XNOR(True,True)," | ")
輸出:
1
+---------------+----------------+
| XNOR Truth Table | Result |
A = False, B = False | A XNOR B = 1 |
A = False, B = True | A XNOR B = 0 |
A = True, B = False | A XNOR B = 0 |
A = True, B = True | A XNOR B = 1 |
以上就是關于“Python運算符之邏輯門是什么及怎么用”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。