Python運(yùn)算符教程之邏輯門(mén)詳解
邏輯門(mén)是任何數(shù)字電路的基本構(gòu)建塊。它需要一兩個(gè)輸入并根據(jù)這些輸入產(chǎn)生輸出。輸出可能為高 (1) 或低 (0)。邏輯門(mén)使用二極管或晶體管實(shí)現(xiàn)。它也可以使用真空管、光學(xué)元件、分子等電磁元件構(gòu)成。在計(jì)算機(jī)中,大多數(shù)電子電路都是由邏輯門(mén)組成的。邏輯門(mén)用于執(zhí)行計(jì)算、數(shù)據(jù)存儲(chǔ)或展示面向?qū)ο缶幊蹋ㄓ绕涫抢^承的力量)的電路。
定義了七個(gè)基本邏輯門(mén):與門(mén)、或門(mén)、非門(mén)、與非門(mén)、或非門(mén)、異或門(mén)、異或門(mén)。
1. 與門(mén)
如果兩個(gè)輸入都為 1,與門(mén)的輸出為 1,否則為 0。
# 說(shuō)明與門(mén)工作的 Python3 程序 def AND (a, b): if a == 1 and b == 1: return True else: return False # 驅(qū)動(dòng)程序代碼 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 |
2. 與非門(mén)
如果兩個(gè)輸入都是 1,與非門(mén)(取反)輸出 0,否則輸出 1。
# 說(shuō)明與非門(mén)工作的Python3程序 def NAND (a, b): if a == 1 and b == 1: return False else: return True # 驅(qū)動(dòng)程序代碼 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 |
3. 或門(mén)
如果兩個(gè)輸入中的任何一個(gè)為 1,或門(mén)的輸出為 1,否則為 0。
# Python3 程序來(lái)說(shuō)明或門(mén)的工作 def OR(a, b): if a == 1 or b ==1: return True else: return False # 驅(qū)動(dòng)程序代碼 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 |
4. 異或
門(mén) 如果輸入中的任何一個(gè)不同,異或門(mén)的輸出為 1,如果它們相同,則輸出為 0。
# 說(shuō)明異或門(mén)工作的 Python3 程序 def XOR (a, b): if a != b: return 1 else: return 0 # 驅(qū)動(dòng)程序代碼 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 |
5. NOT Gate
它作為一個(gè)反相器。它只需要一個(gè)輸入。如果輸入為 1,它會(huì)將結(jié)果反轉(zhuǎn)為 0,反之亦然。
# Python3 程序來(lái)說(shuō)明非門(mén)的工作原理 def NOT(a): return not a # 驅(qū)動(dòng)程序代碼 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 |
6. NOR 門(mén)
NOR 門(mén)(取反的 OR)如果兩個(gè)輸入都為 0,則輸出為 1,否則為 0。
# Python3程序來(lái)說(shuō)明或非門(mén)的工作 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 # 驅(qū)動(dòng)程序代碼 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 |
7. XNOR 門(mén)
XNOR 門(mén)(取反的 XOR)輸出 1,兩個(gè)輸入相同,如果兩者不同,則輸出 0。
# Python3 程序來(lái)說(shuō)明非門(mén)的工作原理 def XNOR(a,b): if(a == b): return 1 else: return 0 # 驅(qū)動(dòng)程序代碼 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 |
到此這篇關(guān)于Python運(yùn)算符教程之邏輯門(mén)詳解的文章就介紹到這了,更多相關(guān)Python邏輯門(mén)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python調(diào)用百度REST API實(shí)現(xiàn)語(yǔ)音識(shí)別
這篇文章主要為大家詳細(xì)介紹了python調(diào)用百度REST API實(shí)現(xiàn)語(yǔ)音識(shí)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08使用python實(shí)現(xiàn)生成用戶(hù)信息
這篇文章主要介紹了使用python實(shí)現(xiàn)生成用戶(hù)信息的相關(guān)代碼,非常的簡(jiǎn)單實(shí)用,需要的朋友可以參考下2017-03-03PyQt5 QSerialPort子線程操作的實(shí)現(xiàn)
這篇文章主要介紹了PyQt5 QSerialPort子線程操作的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04