Logic Gates In Python

This Python tutorial is about Logic Gates in Python. We will try to learn each of the logic gates in Python with some easy examples.

Logic Gates in Python

All of us are familiar with the use of logic gates in the processing of electrical signals and are widely used in the electrical and electronics industry. Using the diodes and transistors these gates can be designed by proper alignment of these electronic devices. In this tutorial we will learn about the implementation of some basic gates ‘and‘, ‘or‘ ,’not‘ , ‘nand‘ ,’nor‘, ‘xnor‘, ‘xor‘  in Python 3.x or earlier.

These gates can be implemented by using functions designed in accordance with that of the truth table associated with the respective gate.

Construction of And Gate in Python

Here is the code to create AND gate in Python

 

def AND (a, b): 
    if a == 1 and b == 1: 
        return True
    else: 
        return False
  
# main function
if __name__=='__main__': 
    print(AND(0,0)) 
    print(AND(1,0)) 
    print(AND(0,1)) 
    print(AND(1,1)) 
OUTPUT:
False
False
False
True

Construction of Or Gate in Python

Here is the code to create OR gate in Python

def OR(a, b): 
    if a == 1: 
        return True
    elif b == 1: 
        return True
    else: 
        return False  
# main function
if __name__=='__main__': 
    print(OR(0,0)) 
    print(OR(1,0)) 
    print(OR(0,1)) 
    print(OR(1,1)) 
OUTPUT:
False
True
True
True

Construction of Not Gate in Python

Code to create NOT gate in Python

def NOT(a): 
    if(a == 0): 
        return 1
    elif(a == 1): 
        return 0
# main function
if __name__=='__main__': 
    print(OR(0)) 
    print(OR(1))
OUTPUT:
True
False

Construction of Nand Gate in Python (negated and)

Code to create NAND gate in Python

def NAND (a, b): 
    if a == 1 and b == 1: 
        return False
    else: 
        return True
# main function
if __name__=='__main__': 
    print(NAND(0,0)) 
    print(NAND(1,0)) 
    print(NAND(0,1)) 
    print(NAND(1,1)) 
OUTPUT:
True
True
True
False

Construction of Nor Gate in Python (negated or)

Code to create NOR gate in Python

def NOR(a, b): 
    if(a == 0) and (b == 0): 
        return True
    elif(a == 0) and (b == 1): 
        return False
    elif(a == 1) and (b == 0): 
        return False
    elif(a == 1) and (b == 1): 
        return False
# main function
if __name__=='__main__': 
    print(NOR(0,0)) 
    print(NOR(1,0)) 
    print(NOR(0,1)) 
    print(NOR(1,1))
OUTPUT:
True
False
False
False

Construction of Xor Gate in Python (exclusive or)

Code to create XOR gate in Python

def XOR (a, b): 
    if a != b: 
        return True
    else: 
        return False
# main function
if __name__=='__main__': 
    print(XOR(0,0)) 
    print(XOR(1,0)) 
    print(XOR(0,1)) 
    print(XOR(1,1)) 
OUTPUT:
False
True
True
False

Construction of Xnor Gate in Python (negated exclusive or)

Code to construct XNOR gate in Python

def XNOR(a,b): 
    if(a == b): 
        return True
    else: 
        return False
# main function
if __name__=='__main__': 
    print(XNOR(0,0)) 
    print(XNOR(1,0)) 
    print(XNOR(0,1)) 
    print(XNOR(1,1)) 
OUTPUT:
True
False
False
True

In all these types of gates, a function is defined in accordance with the Truth table of the respective gate returning back the output in the form of Boolean values (True, False).

This driving function is called for various types of inputs and the corresponding output is produced. This is the reason these are used in the construction of switches and circuit models.

Also, learn,
The conceptual understanding of operators in python
Some important basic built functions in python

Leave a Reply

Your email address will not be published. Required fields are marked *