Logic Gates in Python

A Logic gate is an elementary building block of any digital circuits. It takes one or two inputs and produces output based on those inputs. Outputs may be high (1) or low (0). Logic gates are implemented using diodes or transistors. It can also be constructed using vacuum tubes, electromagnetic elements like optics, molecules, etc. In a computer, most of the electronic circuits are made up logic gates. Logic gates are used to create a circuit that performs calculations, data storage or shows off object-oriented programming especially the power of inheritance. 
There are seven basic logic gates defined, these are: AND gate, OR gate, NOT gate, NAND gate, NOR gate , XOR gate, and XNOR gate. 
1. AND Gate 
The AND gate gives an output of 1 if both the two inputs are 1, it gives 0 otherwise. 
 

 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to illustrate
# working of AND gate
 
def AND (a, b):
 
    if a == 1 and b == 1:
        return True
    else:
        return False
 
# Driver code
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)," | ")

chevron_right


Output: 
 

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. NAND Gate 
The NAND gate (negated AND) gives an output of 0 if both inputs are 1, it gives 1 otherwise. 
 



 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to illustrate
# working of NAND gate
 
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 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)," | ")

chevron_right


Output: 
 

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. OR Gate 
The OR gate gives an output of 1 if either of the two inputs are 1, it gives 0 otherwise. 
 

 

Python3″ 1=”=


# Python3 program to illustrate
# working of OR gate

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 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)," | ")