亲宝软件园·资讯

展开

Python逻辑门

海拥 人气:2

逻辑门是任何数字电路的基本构建块。它需要一两个输入并根据这些输入产生输出。输出可能为高 (1) 或低 (0)。逻辑门使用二极管或晶体管实现。它也可以使用真空管、光学元件、分子等电磁元件构成。在计算机中,大多数电子电路都是由逻辑门组成的。逻辑门用于执行计算、数据存储或展示面向对象编程(尤其是继承的力量)的电路。 

定义了七个基本逻辑门:与门、或门、非门、与非门、或非门、异或门、异或门。 

1. 与门 

如果两个输入都为 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   | 

2. 与非门 

如果两个输入都是 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 | 

3. 或门 

如果两个输入中的任何一个为 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   | 

4. 异或 

门 如果输入中的任何一个不同,异或门的输出为 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  | 

5. NOT Gate 

它作为一个反相器。它只需要一个输入。如果输入为 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  | 

6. NOR 门 

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  | 

7. XNOR 门 

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  | 

加载全部内容

相关教程
猜你喜欢
用户评论