Gates in Code · Theory

Ch 2 · NOT, AND, OR — card 3 of 3.

Every gate is just a rule. Here they are as functions — running them prints each gate's full truth table.

def NOT(a): return 1 - a
def AND(a, b): return 1 if (a and b) else 0
def OR(a, b):  return 1 if (a or b)  else 0

print("A B | AND OR")
for a in (0, 1):
    for b in (0, 1):
        print(f"{a} {b} |  {AND(a,b)}   {OR(a,b)}")

An AND gate outputs 1 only when...