📜  Python中的逻辑门

📅  最后修改于: 2020-01-17 12:35:52             🧑  作者: Mango

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

1. AND门
如果两个输入都为1,则AND门的输出为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 真值表 | 结果 |")
    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 真值表 |    结果 |
 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
# Driver code
if __name__=='__main__':
    print(NAND(1, 0))
    print("+---------------+----------------+")
    print(" | NAND 真值表 | 结果 |")
    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 真值表 |    结果 |
 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:
        return True
    elif b == 1:
        return True
    else:
        return False
# Driver code
if __name__=='__main__':
    print(OR(0, 0))
    print("+---------------+----------------+")
    print(" | OR 真值表 | 结果 |")
    print(" A = False, B = False | A AND B =",OR(False,False)," | ")
    print(" A = False, B = True | A AND B =",OR(False,True)," | ")
    print(" A = True, B = False | A AND B =",OR(True,False)," | ")
    print(" A = True, B = True | A AND B =",OR(True,True)," | ")

输出:

False
+---------------+----------------+
 | OR 真值表 |    结果 |
 A = False, B = False | A AND B = False  |
 A = False, B = True  | A AND B = True   |
 A = True, B = False  | A AND B = True   |
 A = True, B = True   | A AND B = True   |

4. XOR门
如果两个输入中的任何一个都不相同,则XOR门的输出为1,如果它们相同则为0。

# Python3代码展示xor门
def XOR (a, b):
    if a != b:
        return 1
    else:
        return 0
# Driver code
if __name__=='__main__':
    print(XOR(5, 5))
    print("+---------------+----------------+")
    print(" | XOR 真值表 | 结果 |")
    print(" A = False, B = False | A AND B =",XOR(False,False)," | ")
    print(" A = False, B = True | A AND B =",XOR(False,True)," | ")
    print(" A = True, B = False | A AND B =",XOR(True,False)," | ")
    print(" A = True, B = True | A AND B =",XOR(True,True)," | ")

输出:

0
+---------------+----------------+
 | XOR 真值表 | 结果 |
 A = False, B = False | A AND B = 0  |
 A = False, B = True  | A AND B = 1  |
 A = True, B = False  | A AND B = 1  |
 A = True, B = True   | A AND B = 0  |

5. NOT Gate
它用作逆变器。只需要一个输入。如果输入为1,则结果取反为0,反之亦然。

# Python3代码展示非门
def NOT(a):
    if(a == 0):
        return 1
    elif(a == 1):
        return 0
# Driver code
if __name__=='__main__':
    print(NOT(0))
    print("+---------------+----------------+")
    print(" | NOT 真值表 | 结果 |")
    print(" A = False | A NOT =",NOT(False)," | ")
    print(" A = True, | A NOT =",NOT(True)," | ")

输出:

1
+---------------+----------------+
 | NOT 真值表 | 结果 |
 A = False | A NOT = 1  |
 A = True, | A NOT = 0  |

6. NOR门
如果两个输入均为0,则“或非”门输出为1,否则为0。.

# Python3 代码展示nor门
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
# Driver code
if __name__=='__main__':
    print(NOR(0, 0))
    print("+---------------+----------------+")
    print(" | NOR 真值表 | 结果 |")
    print(" A = False, B = False | A AND B =",NOR(False,False)," | ")
    print(" A = False, B = True | A AND B =",NOR(False,True)," | ")
    print(" A = True, B = False | A AND B =",NOR(True,False)," | ")
    print(" A = True, B = True | A AND B =",NOR(True,True)," | ")

输出:

1
+---------------+----------------+
 | NOR 真值表 |   结果 |
 A = False, B = False | A AND B = 1  |
 A = False, B = True  | A AND B = 0  |
 A = True, B = False  | A AND B = 0  |
 A = True, B = True   | A AND B = 0  |

7. XNOR门
XNOR门(取反XOR)的输出均为1,两个输入相同,如果两个都不相同,则为0。

# Python3代码展示xnor
def XNOR(a,b):
    if(a == b):
        return 1
    else:
        return 0
# Driver code
if __name__=='__main__':
    print(XNOR(1,1))
    print("+---------------+----------------+")
    print(" | XNOR 真值表 | 结果 |")
    print(" A = False, B = False | A AND B =",XNOR(False,False)," | ")
    print(" A = False, B = True | A AND B =",XNOR(False,True)," | ")
    print(" A = True, B = False | A AND B =",XNOR(True,False)," | ")
    print(" A = True, B = True | A AND B =",XNOR(True,True)," | ")

输出:

1
+---------------+----------------+
 | XNOR 真值表 |  结果 |
 A = False, B = False | A AND B = 1  |
 A = False, B = True  | A AND B = 0  |
 A = True, B = False  | A AND B = 0  |
 A = True, B = True   | A AND B = 1  |