Truth Table Generator
Enter a boolean expression using AND, OR, NOT, XOR and get a complete truth table.
Operators: AND, OR, NOT, XOR, NAND, NOR, XNOR (or &, |, !, ^)
About this tool
The Truth Table Generator takes a boolean expression built from AND, OR, NOT, and XOR and lays out the complete truth table for every possible combination of its inputs. It solves the tedious problem of checking how a logic expression behaves across all input cases without working through each row by hand.
Type a boolean expression using your variables and operators, then read the full table that shows the result for every input combination. It is handy for studying digital logic, working through discrete math, simplifying conditional logic in your code, and preparing for exams.
Keep in mind that the table grows as 2^n rows for n variables, so it doubles in size with every variable you add. Everything runs locally in your browser, so your expressions never leave your device.
Frequently Asked Questions
Code Implementation
from itertools import product
def truth_table(variables, expression):
"""Generate a truth table for a logical expression."""
headers = variables + [expression]
print(" | ".join(f"{h:>6}" for h in headers))
print("-" * (9 * len(headers)))
for values in product([False, True], repeat=len(variables)):
env = dict(zip(variables, values))
# Replace logical notation with Python
expr = expression
expr = expr.replace("AND", "and").replace("OR", "or").replace("NOT", "not")
expr = expr.replace("β§", " and ").replace("β¨", " or ").replace("Β¬", " not ")
result = eval(expr, {}, env)
row = [str(v) for v in values] + [str(result)]
print(" | ".join(f"{v:>6}" for v in row))
# Example: P AND Q
truth_table(["P", "Q"], "P and Q")
print()
# Example: P OR (NOT Q)
truth_table(["P", "Q"], "P or (not Q)")
print()
# Check tautology: P OR NOT P
truth_table(["P"], "P or (not P)")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.