In [1]:
def truth_table(gate):
print("*{}*".format(gate.__name__))
print("|x1|x2|y|")
print("|0 |0 |{}|".format(gate(0, 0)))
print("|0 |1 |{}|".format(gate(0, 1)))
print("|1 |0 |{}|".format(gate(1, 0)))
print("|1 |1 |{}|".format(gate(1, 1)))
In [2]:
def and_gate(x1, x2):
w1, w2, theta, = 0.5, 0.5, 0.75
if theta < x1 * w1 + x2 * w2:
y = 1
else:
y = 0
return y
In [3]:
truth_table(and_gate)
In [4]:
def or_gate(x1, x2):
w1, w2, theta, = 0.5, 0.5, 0.25
if theta < x1 * w1 + x2 * w2:
y = 1
else:
y = 0
return y
In [5]:
truth_table(or_gate)
In [6]:
def nand_gate(x1, x2):
w1, w2, theta, = -0.5, -0.5, -0.75
if theta < x1 * w1 + x2 * w2:
y = 1
else:
y = 0
return y
In [7]:
truth_table(nand_gate)
In [8]:
def xor_gate(x1, x2):
s1 = or_gate(x1, x2)
s2 = nand_gate(x1, x2)
y = and_gate(s1, s2)
return y
In [9]:
truth_table(xor_gate)