Python 教程之运算符(11)Python中的逻辑门

简介: Python 教程之运算符(11)Python中的逻辑门

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

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

1. 与门 

如果两个输入都为 1,与门的输出为 1,否则为 0。

image.png

# 说明与门工作的 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。

image.png

# 说明与非门工作的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。

image.png

# 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。


image.png

# 说明异或门工作的 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,反之亦然。

image.png

# 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。

image.png

# 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
+---------------+----------------+
 | NOR Truth Table |   Result |
 A = False, B = False | A NOR B = 1  | 
 A = False, B = True  | A NOR B = 0  | 
 A = True, B = False  | A NOR B = 0  | 
 A = True, B = True   | A NOR B = 0  | 

7. XNOR 门 

XNOR 门(取反的 XOR)输出 1,两个输入相同,如果两者不同,则输出 0。

image.png

# 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  | 
目录
相关文章
|
3天前
|
数据可视化 DataX Python
Seaborn 教程-绘图函数
Seaborn 教程-绘图函数
30 8
|
3天前
Seaborn 教程-主题(Theme)
Seaborn 教程-主题(Theme)
21 7
|
3天前
|
Python
Seaborn 教程-模板(Context)
Seaborn 教程-模板(Context)
22 4
|
3天前
|
数据可视化 Python
Seaborn 教程
Seaborn 教程
20 5
|
26天前
|
Python
Python运算符优先级
Python运算符优先级。
21 3
|
26天前
|
Python
Python成员运算符
Python成员运算符
24 2
|
26天前
|
Python
Python身份运算符
Python身份运算符。
21 1
|
26天前
|
Python
SciPy 教程 之 Scipy 显著性检验 9
SciPy 教程之 Scipy 显著性检验第9部分,介绍了显著性检验的基本概念、作用及原理,通过样本信息判断假设是否成立。着重讲解了使用scipy.stats模块进行显著性检验的方法,包括正态性检验中的偏度和峰度计算,以及如何利用normaltest()函数评估数据是否符合正态分布。示例代码展示了如何计算一组随机数的偏度和峰度。
24 1
|
2月前
|
数据可视化 IDE 开发工具
【Python篇】PyQt5 超详细教程——由入门到精通(中篇二)
【Python篇】PyQt5 超详细教程——由入门到精通(中篇二)
301 13
|
2月前
|
监控 数据可视化 搜索推荐
【Python篇】matplotlib超详细教程-由入门到精通(下篇)2
【Python篇】matplotlib超详细教程-由入门到精通(下篇)
40 8