Skip to content
Laviyx Learn

Try

Python Lab · Experiment 36

Digital logic gates AND, OR, NOT and XOR

PythonWeek 8JNTUH R22 · CS206ES

Aim

To write a Python program that implements the digital logic gates AND, OR, NOT and EX-OR (XOR) as functions, and the NAND and NOR gates built from them, and prints the truth table of each gate.

Algorithm

  1. Write a function for each gate that takes inputs of 0 or 1 and returns 0 or 1:
    • AND returns 1 only when both inputs are 1;
    • OR returns 1 when at least one input is 1;
    • NOT returns the opposite of its one input;
    • XOR returns 1 when the two inputs are different.
  2. Build NAND as NOT of AND, and NOR as NOT of OR.
  3. Print the truth table of NOT for the inputs 0 and 1.
  4. For every pair of inputs A and B (00, 01, 10, 11), print A, B and the output of AND, OR, XOR, NAND and NOR in one row.

Program

Python
def and_gate(a, b):
    return 1 if a == 1 and b == 1 else 0


def or_gate(a, b):
    return 1 if a == 1 or b == 1 else 0


def not_gate(a):
    return 1 if a == 0 else 0


def xor_gate(a, b):
    return 1 if a != b else 0


def nand_gate(a, b):
    return not_gate(and_gate(a, b))


def nor_gate(a, b):
    return not_gate(or_gate(a, b))


print("NOT gate")
print(" A | NOT A")
for a in [0, 1]:
    print(f" {a} |   {not_gate(a)}")

print("\nTwo-input gates")
print(" A  B | AND  OR  XOR  NAND  NOR")
for a in [0, 1]:
    for b in [0, 1]:
        print(f" {a}  {b} |  {and_gate(a, b)}    {or_gate(a, b)}    {xor_gate(a, b)}"
              f"     {nand_gate(a, b)}     {nor_gate(a, b)}")

Output

Output
NOT gate
 A | NOT A
 0 |   1
 1 |   0

Two-input gates
 A  B | AND  OR  XOR  NAND  NOR
 0  0 |  0    0    0     1     1
 0  1 |  0    1    1     1     0
 1  0 |  0    1    1     1     0
 1  1 |  1    1    0     0     0

Recorded by running this exact program.

How it works

The gates work on the binary values 0 (false, low) and 1 (true, high). In Boolean algebra they are written as A·B (AND), A + B (OR), A' or Ā (NOT) and A ⊕ B (XOR).

NAND and NOR are called universal gates, because every other gate can be built using only NAND gates, or only NOR gates. For example, NOT A = A NAND A.

Python also has bitwise operators that act as these gates on 0 and 1: a & b is AND, a | b is OR, a ^ b is XOR, and 1 - a (or a ^ 1) is NOT. The if versions are used here because they read like the gate definitions. Do not use ~a for NOT: for integers ~0 is −1 and ~1 is −2, not 1 and 0.

Common mistakes

  • Using ~a for the NOT gate. In Python ~ flips all bits of an integer, so ~1 is −2. Use 1 - a, a ^ 1 or an if.

  • Using not a and printing the result directly. not 1 gives False, not 0, so the table shows True/False. Convert with int(not a) if you use it.

  • Mixing up XOR and OR. They differ only when both inputs are 1: OR gives 1 and XOR gives 0.

  • Missing a row of the truth table. Two inputs have 2² = 4 combinations; nested loops over [0, 1] produce all of them.

Viva questions

Try to answer each one aloud before you open it.

  • What is a logic gate?

    A basic digital circuit that takes one or more binary inputs (0 or 1) and gives one binary output according to a fixed rule, such as AND, OR or NOT.

  • When is the output of an XOR gate 1?

    When its two inputs are different: 0 and 1, or 1 and 0. When both inputs are the same, the output is 0.

  • Why are NAND and NOR called universal gates?

    Because any logic circuit, including AND, OR and NOT, can be built using only NAND gates or only NOR gates.

  • How many rows does the truth table of a gate with n inputs have?

    2ⁿ rows, one for each combination of inputs. A two-input gate has 4 rows.

  • Which Python operators can act as AND, OR and XOR on 0 and 1?

    The bitwise operators & (AND), | (OR) and ^ (XOR). For example, 1 ^ 1 is 0 and 1 | 0 is 1.

Learn the concepts

All Python Programming Lab experiments →