Create pretty-printed truth tables and abstract syntax trees from boolean expressions! Installation is as easy aspip install truth_tables
.
>>>fromtruth_tablesimportTruthTable>>>my_table=TruthTable('p or q','~p -> q','T and ~T')>>>print(my_table)┌───┬───┬────────┬─────────┬──────────┐│p │q │porq │~p->q │Tand~T │├───┼───┼────────┼─────────┼──────────┤│F │F │F │F │F ││F │T │T │T │F ││T │F │T │T │F ││T │T │T │T │F │└───┴───┴────────┴─────────┴──────────┘>>>print(my_table.ast)Or├─Variable('p')╰─Variable('q')Implies├─Negate│ ╰─Variable('p')╰─Variable('q')And├─LiteralTrue╰─Negate ╰─LiteralTrue>>>my_table=TruthTable('~((p xor (q and ~r) or q) and ~(p <-> r))')>>>print(my_table)┌───┬───┬───┬───────────────────────────────────────────┐│p|q|r|~((pxor (qand~r)orq)and~(p<->r)) │├───┼───┼───┼───────────────────────────────────────────┤│F|F|F|T ││F|F|T|T ││F|T|F|T ││F|T|T|F ││T|F|F|F ││T|F|T|T ││T|T|F|F ││T|T|T|T │└───┴───┴───┴───────────────────────────────────────────┘
Two truth tables are equal if they have the same variables and the same truth values (not necessarily the same propositions).