当前位置: 萬仟网 > IT编程>脚本编程>Python > Python运算符教程之逻辑门详解

Python运算符教程之逻辑门详解

2022年09月18日 Python 我要评论
逻辑门是任何数字电路的基本构建块。它需要一两个输入并根据这些输入产生输出。输出可能为高 (1) 或低 (0)。逻辑门使用二极管或晶体管实现。它也可以使用真空管、光学元件、分子等电磁元件构成。在计算机中

逻辑门是任何数字电路的基本构建块。它需要一两个输入并根据这些输入产生输出。输出可能为高 (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  | 

到此这篇关于python运算符教程之逻辑门详解的文章就介绍到这了,更多相关python逻辑门内容请搜索萬仟网以前的文章或继续浏览下面的相关文章希望大家以后多多支持萬仟网!

(1)
打赏 微信扫一扫 微信扫一扫

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。

发表评论

验证码:
Copyright © 2017-2022  萬仟网 保留所有权利. 琼ICP备2022007597号