This article comprehensively covers the different logic gates in Python. Logic gates are the most basic materials to implement digital components. The use of logic gates ranges from computer architecture to the field of electronics.

These gates deal with binary values, either 0 or 1. Different types of gates take different numbers of input, but all of them provide a single output. These logic gates when combined form complicated circuits.

Let us try to implement logic gates in Python Language.

Table of Contents

## Basic Logic Gates in Python

There are three most basic logic gates in circuit development.

### OR Gate

This gate provides the output as 1 if either of the inputs is 1. It is similar to an *“addition”* operation, with respect to binary numbers.

The table displayed above is the truth table. It is used to showcase all the combinations of values for inputs to a `'OR'`

gate. The figure alongside the table denotes an `'OR'`

gate.

Implementation in Python:

```
# Function to simulate OR Gate
def OR(A, B):
return A | B
print("Output of 0 OR 0 is", OR(0, 0))
print("Output of 0 OR 1 is", OR(0, 1))
print("Output of 1 OR 0 is", OR(1, 0))
print("Output of 1 OR 1 is", OR(1, 1))
```

We get the following output:

```
Output of 0 OR 0 is 0
Output of 0 OR 1 is 1
Output of 1 OR 0 is 1
Output of 1 OR 1 is 1
```

### AND Gate

This gate provides an output of 0 if either of the inputs are 0. This operation is considered as multiplication in binary numbers.

We can see in the truth table that whenever either of the two inputs is 0, the output is 0 too. The alongside figure denotes the `'AND'`

gate.

Implementation in Python:

```
# Function to simulate AND Gate
def AND(A, B):
return A & B
print("Output of 0 AND 0 is", AND(0, 0))
print("Output of 0 AND 1 is", AND(0, 1))
print("Output of 1 AND 0 is", AND(1, 0))
print("Output of 1 AND 1 is", AND(1, 1))
```

Output:

```
Output of 0 AND 0 is 0
Output of 0 AND 1 is 0
Output of 1 AND 0 is 0
Output of 1 AND 1 is 1
```

### NOT Gate

This gate provides the negation of the input given. This gate supports only a single input.

The above table clearly displays the reversal of bits. The adjoining figure represents the `'NOT'`

gate.

Python implementation of binary NOT Gate:

```
# Function to simulate NOT Gate
def NOT(A):
return ~A+2
print("Output of NOT 0 is", NOT(0))
print("Output of NOT 1 is", NOT(1))
```

Output:

```
Output of NOT 0 is 1
Output of NOT 1 is 0
```

Note:The`'NOT()'`

function provides correct results for bit values 0 and 1.

## Universal Logic Gates in Python

There are two universal logic gates, `'NAND'`

and `'NOR'`

. They are named universal because any boolean circuit can be implemented using only these gates.

### NAND Gate

The `'NAND'`

gate is a combination of `'AND'`

gate followed by `'NOT'`

gate. Opposite to `'AND'`

gate, it provides an output of 0 only when both the bits are set, otherwise 1.

In Python, `'NAND()'`

function can be implemented using the `'AND()'`

and `'OR()'`

functions created before.

```
# Function to simulate AND Gate
def AND(A, B):
return A & B;
# Function to simulate NOT Gate
def NOT(A):
return ~A+2
# Function to simulate NAND Gate
def NAND(A, B):
return NOT(AND(A, B))
print("Output of 0 NAND 0 is", NAND(0, 0))
print("Output of 0 NAND 1 is", NAND(0, 1))
print("Output of 1 NAND 0 is", NAND(1, 0))
print("Output of 1 NAND 1 is", NAND(1, 1))
```

We get the following output:

```
Output of 0 NAND 0 is 1
Output of 0 NAND 1 is 1
Output of 1 NAND 0 is 1
Output of 1 NAND 1 is 0
```

### NOR Gate

The `'NOR'`

gate is a result of cascading of `'OR'`

gate followed by `'NOT'`

gate. Contrary to `'OR'`

gate, it provides an output of 1, when all the inputs are 0.

Similar to `'NAND()'`

function, `'NOR()'`

can be implemented using already created functions.

```
# Function to calculate OR Gate
def OR(A, B):
return A | B;
# Function to simulate NOT Gate
def NOT(A):
return ~A+2
# Function to simulate NOR Gate
def NOR(A, B):
return NOT(OR(A, B))
print("Output of 0 NOR 0 is", NOR(0, 0))
print("Output of 0 NOR 1 is", NOR(0, 1))
print("Output of 1 NOR 0 is", NOR(1, 0))
print("Output of 1 NOR 1 is", NOR(1, 1))
```

Output:

```
Output of 0 NOR 0 is 1
Output of 0 NOR 1 is 0
Output of 1 NOR 0 is 0
Output of 1 NOR 1 is 0
```

## Exclusive Logic Gates in Python

There are two special types of logic gates, XOR and XNOR, that focus on the number of inputs of 0 or 1, rather than individual values.

### XOR Gate

An acronym for Exclusive-OR, `'XOR'`

gate provides an output of 1 when the number of 1s in the input is odd.

We can clearly see the output for the XOR Gate in the table above. It provides an output of 1 when the number of ones in the input is 1, that is odd.

We can implement `'XOR()'`

function easily in Python by:

```
# Function to simulate XOR Gate
def XOR(A, B):
return A ^ B
print("Output of 0 XOR 0 is", XOR(0, 0))
print("Output of 0 XOR 1 is", XOR(0, 1))
print("Output of 1 XOR 0 is", XOR(1, 0))
print("Output of 1 XOR 1 is", XOR(1, 1))
```

We get the following output:

```
Output of 0 XOR 0 is 0
Output of 0 XOR 1 is 1
Output of 1 XOR 0 is 1
Output of 1 XOR 1 is 0
```

### XNOR Gate

It is formed as a result of the combination of `'XOR'`

and `'NOT'`

gates. Opposite to `'XOR'`

, it provides an output of 1, when the number of 1s in the input is even.

The `'XNOR()'`

function can be implemented by using the `'XOR()'`

and `'NOT()'`

functions in Python.

```
# Function to simulate XOR Gate
def XOR(A, B):
return A ^ B
# Function to simulate NOT Gate
def NOT(A):
return ~A+2
# Function to simulate XNOR Gate
def XNOR(A, B):
return NOT(XOR(A, B))
print("Output of 0 XNOR 0 is", XNOR(0, 0))
print("Output of 0 XNOR 1 is", XNOR(0, 1))
print("Output of 1 XNOR 0 is", XNOR(1, 0))
print("Output of 1 XNOR 1 is", XNOR(1, 1))
```

Output:

```
Output of 0 XNOR 0 is 1
Output of 0 XNOR 1 is 0
Output of 1 XNOR 0 is 0
Output of 1 XNOR 1 is 1
```

## Conclusion

The implementation of logic gates in Python is very easy. As a programmer, you need to be aware of logic gates and operators in Python. We hope that this article enlightened the reader about the basics and execution of logic gates in Python.

For further reading, check out our other Python tutorials.

Decent article. Understood perfectly.

Excellent article. Fairly easy to follow even for a beginner

Well done.

Thank you for the feedback.