这篇“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。