Package: truth-table-generator
truth-table-generatortruth-table-generatoris a tool that allows to generate a truth table.
It is a fork oftruthsbytr3buchet.It merges some of the pull requests in the original and other external helpers.
The following are some of the changes and enhancements from the original:tabulateinstead of obsoleteprettytableas main tool to
represent tabular data in ASCII tables (PrettyTable version is still available).so there are many table formats available as such LaTeX, Org Tables, HTML
and all others cited ontabulate docsthe table is now a Pandas DataFrame so you can make the output more visually
appealing withPandas Styling.
See examples below.new functionvaluationthat eval a proposition as a tautology, contradiction
or contingency.new command line interface (CLI) for printing a truth table from terminal.Installationpip install truth-table-generatorUsageImporting and syntaxFirst, let's import the package.ttgstands fortruth-table-generator.importttgA truth table has one column for each input variable (for example,pandq),
and one final column showing all of the possible results of the logical
operation that the table represents. If the input has only one list of strings,
each string is considered an input variable:print(ttg.Truths(['p','q','r']))+-----+-----+-----+
|  p  |  q  |  r  |
|-----+-----+-----|
|  1  |  1  |  1  |
|  1  |  1  |  0  |
|  1  |  0  |  1  |
|  1  |  0  |  0  |
|  0  |  1  |  1  |
|  0  |  1  |  0  |
|  0  |  0  |  1  |
|  0  |  0  |  0  |
+-----+-----+-----+A second list of strings can be passed with propositional expressions created
with logical operators.print(ttg.Truths(['p','q','r'],['p and q and r','p or q or r','(p or (~q)) => r']))+-----+-----+-----+-----------------+---------------+--------------------+
|  p  |  q  |  r  |  p and q and r  |  p or q or r  |  (p or (~q)) => r  |
|-----+-----+-----+-----------------+---------------+--------------------|
|  1  |  1  |  1  |        1        |       1       |         1          |
|  1  |  1  |  0  |        0        |       1       |         0          |
|  1  |  0  |  1  |        0        |       1       |         1          |
|  1  |  0  |  0  |        0        |       1       |         0          |
|  0  |  1  |  1  |        0        |       1       |         1          |
|  0  |  1  |  0  |        0        |       1       |         1          |
|  0  |  0  |  1  |        0        |       1       |         1          |
|  0  |  0  |  0  |        0        |       0       |         0          |
+-----+-----+-----+-----------------+---------------+--------------------+Operators and their representations:negation:'not','-','~'logical disjunction:'or'logical nor:'nor'exclusive disjunction:'xor','!='logical conjunction:'and'logical NAND:'nand'material implication:'=>','implies'logical biconditional:'='Note: Use parentheses! Especially with the negation operator. Use tables
above and below as reference. Although precedence rules are used, sometimes
precedence between conjunction and disjunction is unspecified requiring to
provide it explicitly in given formula with parentheses.Showing words (True / False)If you prefer the words True and False instead of numbers 0 and 1, there is a
third parameter, boolean type,intsthat can be set toFalse:print(ttg.Truths(['p','q'],['p and q','p or q','(p or (~q)) => (~p)'],ints=False))+-------+-------+-----------+----------+-----------------------+
|   p   |   q   |  p and q  |  p or q  |  (p or (~q)) => (~p)  |
|-------+-------+-----------+----------+-----------------------|
| True  | True  |   True    |   True   |         False         |
| True  | False |   False   |   True   |         False         |
| False | True  |   False   |   True   |         True          |
| False | False |   False   |  False   |         True          |
+-------+-------+-----------+----------+-----------------------+Reverse output optionIf you prefer to have propositions in ascending order (0/False before 1/True),
there is theascendingparameter that can be set toTrue. Let's change
the above example to ascending order:print(ttg.Truths(['p','q'],['p and q','p or q','(p or (~q)) => (~p)'],ints=False,ascending=True))+-------+-------+-----------+----------+-----------------------+
|   p   |   q   |  p and q  |  p or q  |  (p or (~q)) => (~p)  |
|-------+-------+-----------+----------+-----------------------|
| False | False |   False   |  False   |         True          |
| False | True  |   False   |   True   |         True          |
| True  | False |   False   |   True   |         False         |
| True  | True  |   True    |   True   |         False         |
+-------+-------+-----------+----------+-----------------------+Formatting options with PrettyTable and TabulateFor more formatting options, let's create a truth table variable:table=ttg.Truths(['p','q'],['p => q','p = q'])The commandprint(table)renders the standard table as seen on above examples:+-----+-----+----------+---------+
|  p  |  q  |  p => q  |  p = q  |
|-----+-----+----------+---------|
|  1  |  1  |    1     |    1    |
|  1  |  0  |    0     |    0    |
|  0  |  1  |    1     |    0    |
|  0  |  0  |    1     |    1    |
+-----+-----+----------+---------+The commandprint(table.as_prettytable())renders the table with PrettyTable
package as on the original version of this package:+---+---+--------+-------+
| p | q | p => q | p = q |
+---+---+--------+-------+
| 1 | 1 |   1    |   1   |
| 1 | 0 |   0    |   0   |
| 0 | 1 |   1    |   0   |
| 0 | 0 |   1    |   1   |
+---+---+--------+-------+As can be seen, the PrettyTable output has less blank spaces. However, the
PrettyTable package has much less output options and it is deprecated. So I
decided to use the Tabulate package as standard.The commandprint(table.as_tabulate())renders the table with Tabulate
package. The first column presents line numbers (that can be disabled with
the parameterindex=False):+----+-----+-----+----------+---------+
|    |  p  |  q  |  p => q  |  p = q  |
|----+-----+-----+----------+---------|
| 1  |  1  |  1  |    1     |    1    |
| 2  |  1  |  0  |    0     |    0    |
| 3  |  0  |  1  |    1     |    0    |
| 4  |  0  |  0  |    1     |    1    |
+----+-----+-----+----------+---------+Using Tabulate, we can use any of the formats available. Let's output a LaTeX
table without the line number column:print(table.as_tabulate(index=False,table_format='latex'))\begin{tabular}{cccc}
\hline
  p  &  q  &  p =\ensuremath{>} q  &  p = q  \\
\hline
  1  &  1  &    1     &    1    \\
  1  &  0  &    0     &    0    \\
  0  &  1  &    1     &    0    \\
  0  &  0  &    1     &    1    \\
\hline
\end{tabular}Formatting options with PandasWith an IPython terminal or a Jupyter Notebook, it is possible to render a Pandas
DataFrame withtable.as_pandas:And this output can be modified with Pandas Styling.
More advanced modifications can be done with functions that apply styling changes.
See thestyles tutorial notebookfor examples.
See the image below for a fancy example with two lines and two columns
highlighted with yellow background and different colors for True and False.ThevaluationfunctionLet's see the how to use thevaluationfunction with a new truth table:table_val=ttg.Truths(['p','q'],['p = q','p and (~p)','(p and q) => p'])print(table_val)+-----+-----+---------+--------------+------------------+
|  p  |  q  |  p = q  |  p and (~p)  |  (p and q) => p  |
|-----+-----+---------+--------------+------------------|
|  1  |  1  |    1    |      0       |        1         |
|  1  |  0  |    0    |      0       |        1         |
|  0  |  1  |    0    |      0       |        1         |
|  0  |  0  |    1    |      0       |        1         |
+-----+-----+---------+--------------+------------------+Without arguments, thevaluationfunction classifies thelast columnas a
tautology, a contradiction or a contingency:table_val.valuation()'Tautology'If an integer is used as argument, the function classifies the correspondent
column:table_val.valuation(3)'Contingency'table_val.valuation(4)'Contradiction'CLI utilityFor those who work in the terminal there is a simple command line interface
(CLI) for printing tables. The script name isttg_cly.pyand it accepts
the following syntax according to its--help:usage: ttg_cli.py [-h] [-p PROPOSITIONS] [-i INTS] [-a ASCENDING] variables

positional arguments:
  variables             List of variables e. g. "['p', 'q']"

optional arguments:
  -h, --help            show this help message and exit
  -p PROPOSITIONS, --propositions PROPOSITIONS
                        List of propositions e. g. "['p or q', 'p and q']"
  -i INTS, --ints INTS  True for 0 and 1; False for words
  -a ASCENDING, --ascending ASCENDING
                        True for reverse output (False before True)As seen, the list of variables is mandatory. Note that the lists must be between".$ttg_cli.py"['p', 'q', 'r']"+-----+-----+-----+
|  p  |  q  |  r  |
|-----+-----+-----|
|  1  |  1  |  1  |
|  1  |  1  |  0  |
|  1  |  0  |  1  |
|  1  |  0  |  0  |
|  0  |  1  |  1  |
|  0  |  1  |  0  |
|  0  |  0  |  1  |
|  0  |  0  |  0  |
+-----+-----+-----+The CLI utility also has an option,-i, to show words instead of numbers:$ttg_cli.py"['p', 'q', 'r']"-iFalse+-------+-------+-------+
|   p   |   q   |   r   |
|-------+-------+-------|
| True  | True  | True  |
| True  | True  | False |
| True  | False | True  |
| True  | False | False |
| False | True  | True  |
| False | True  | False |
| False | False | True  |
| False | False | False |
+-------+-------+-------+A-pparameter must be before the propositions list:$ttg_cli.py"['p', 'q', 'r']"-p"['p or q', 'p and q or r']"+-----+-----+-----+----------+----------------+
|  p  |  q  |  r  |  p or q  |  p and q or r  |
|-----+-----+-----+----------+----------------|
|  1  |  1  |  1  |    1     |       1        |
|  1  |  1  |  0  |    1     |       1        |
|  1  |  0  |  1  |    1     |       1        |
|  1  |  0  |  0  |    1     |       0        |
|  0  |  1  |  1  |    1     |       1        |
|  0  |  1  |  0  |    1     |       0        |
|  0  |  0  |  1  |    0     |       1        |
|  0  |  0  |  0  |    0     |       0        |
+-----+-----+-----+----------+----------------+With words instead of numbers:$ttg_cli.py"['p', 'q', 'r']"-p"['p or q', 'p and q or r']"-iFalse+-------+-------+-------+----------+----------------+
|   p   |   q   |   r   |  p or q  |  p and q or r  |
|-------+-------+-------+----------+----------------|
| True  | True  | True  |   True   |      True      |
| True  | True  | False |   True   |      True      |
| True  | False | True  |   True   |      True      |
| True  | False | False |   True   |     False      |
| False | True  | True  |   True   |      True      |
| False | True  | False |   True   |     False      |
| False | False | True  |  False   |      True      |
| False | False | False |  False   |     False      |
+-------+-------+-------+----------+----------------+In ascending order (0/False before 1/True):$ttg_cli.py"['p', 'q', 'r']"-p"['p or q', 'p and q or r']"-iFalse-aTrue+-------+-------+-------+----------+----------------+
|   p   |   q   |   r   |  p or q  |  p and q or r  |
|-------+-------+-------+----------+----------------|
| False | False | False |  False   |     False      |
| False | False | True  |  False   |      True      |
| False | True  | False |   True   |     False      |
| False | True  | True  |   True   |      True      |
| True  | False | False |   True   |     False      |
| True  | False | True  |   True   |      True      |
| True  | True  | False |   True   |      True      |
| True  | True  | True  |   True   |      True      |
+-------+-------+-------+----------+----------------+The real look of the table depends on your terminal appearance configuration.
The green on black background screenshots from the first picture of this README
are from my terminal.ContributingAll contributions are welcome.IssuesFeel free to submit issues regarding:recommendationsmore examples for the tutorialenhancement requests and new useful featurescode bugsPull requestsbefore starting to work on your pull request, please submit an issue firstfork the repoclone the project to your own machinecommit changes to your own branchpush your work back up to your forksubmit a pull request so that your changes can be reviewedLicenseApache 2.0, seeLICENSECitingIf you usetruth-table-generatorin a scientific publication or in classes,
please consider citing asF. L. S. Bustamante,truth-table-generator- generating truth tables., 2019 -
Available at:https://github.com/chicolucio/truth-table-generatorFundingIf you enjoy this project and would like to see many more math and science
related programming projects, I would greatly appreciate any assistance. Send me
an e-mail to know how to assist. Many more projects are to come and your support
will be rewarded with more STEM coding projects :-)