## python build a dynamic growing truth table

My question is simple: "how to build a dynamic growing truth table in python in an elegant way?"

for n=3

for p in False, True: for q in False, True: for r in False, True: print '|{0} | {1} | {2} |'.format(int(p),int(q), int(r))

for n=4

for p in False, True: for q in False, True: for r in False, True: for s in False, True: print '|{0} | {1} | {2} | {3}'.format(int(p),int(q), int(r), int(s))

I would like to have a function which takes n as a parameter and builds up the table, it is not necessary to print the table, returning a data structure representing the table is fine also.

Use `itertools.product()`

:

table = list(itertools.product([False, True], repeat=n))

Result for `n = 3`

:

[(False, False, False), (False, False, True), (False, True, False), (False, True, True), (True, False, False), (True, False, True), (True, True, False), (True, True, True)]

**logic - python build a dynamic growing truth table,** Use itertools.product() : table = list(itertools.product([False, True], repeat=n)). Result for n = 3 : [(False, False, False), (False, False, True), (False, True, False),� My question is simple: "how to build a dynamic growing truth table in python in an elegant way?" for n=3 for p in False, True: for q in False, True: for r in False, True: print '|{0} | {1} | {2} |'.format(int(p),int(q), int(r)) for n=4 for p in False, True: for q in False, True: for r in False, True: for s in False, True: print '|{0} | {1} | {2} | {3}'.format(int(p),int(q), int(r), int(s)) I

`itertools`

really is the way to go as has been pointed out by everyone. But if you really want to see the nuts and bolts of the algorithm required for this, you should look up recursive descent. Here's how it would work in your case:

def tablize(n, truths=[]): if not n: print truths else: for i in [True, False]: tablize(n-1, truths+[i])

Tested, working

Hope this helps

**python build a dynamic growing truth table?,** My question is simple: "how to build a dynamic growing truth table in python in an elegant way?" for n=3 for p in False, True: for q in False, True:� pip install truth-table-generator. Usage Importing and syntax. First, let's import the package. ttg stands for truth-table-generator. import ttg. A truth table has one column for each input variable (for example, p and q), 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:

List comprehensions are, of course, more Pythonic.

def truthtable (n): if n < 1: return [[]] subtable = truthtable(n-1) return [ row + [v] for row in subtable for v in [0,1] ]

Results, indented for clairity:

truthtable(1) [ [0], [1] ] truthtable(3) [ [0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1] ]

As a generator function with `yield`

:

def truthtable (n): if n < 1: yield [] return subtable = truthtable(n-1) for row in subtable: for v in [0,1]: yield row + [v]

Also simply changing the return from an array comprehension to a generator expression makes the return type equivalent to the `yield`

version's generator function:

def truthtable (n): if n < 1: return [[]] subtable = truthtable(n-1) return ( row + [v] for row in subtable for v in [0,1] )

**python build a dynamic growing truth table,** My question is simple: "how to build a dynamic growing truth table in python in an elegant way?" for n=3. for p in False, True: for q in False, True: for r in False,� Truth Tables. The Python interpreter doesn't think about operators the way we do when we do math. For example, when a human sees an equation like this: x = 2 + 3 It looks like the description of a situation. A person might interpret this equation as something like "The variable x has the same value as the sum of 2 and 3".

Have a look at the itertools module

In [7]: [i for i in itertools.product([0,1], repeat=3)] Out[7]: [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]

**python build a dynamic growing truth table,** My question is simple: "how to build a dynamic growing truth table in python in an elegant way?"for n=3for p in False, True: for q in False, True: for r in False,� Truth table You are encouraged to solve this task according to the task description, using any language you may know. A truth table is a display of the inputs to, and the output of a Boolean function organized as a table where each row gives one combination of input values and the corresponding value of the function.

returning a datastructure representing the table is fine

...in that case `range(2 ** n)`

is all you need. Each number in the range represents a row in the truth table. The `i`

th bit of the binary representation of the number `k`

is 1 if and only if the `i`

th variable is true in the `k`

th row of the table.

If you want an actual table you can use:

[ [ ((row >> bit_index) & 1) == 1 for bit_index in range(n)] for bit_index in range(2 ** n) ]

**truth-table-generator � PyPI,** Python API that auto generates truth tables. truth-table-generator is a tool that allows to generate a truth table. It is a fork of truths by tr3buchet. Truth Table Generator. Truth Table is a mathematical table and the base for all computing needs. It is used to find out if a propositional expression is true for all legitimate input values. Creating a Truth table involves a simple logic yet sometimes it may slow you down, especially when you are working on a last minute project.

Truth Table Generator This tool generates truth tables for propositional logic formulas. You can enter logical operators in several different formats. For example, the propositional formula p ∧ q → ¬r could be written as p /\ q -> ~r, as p and q => not r, or as p && q -> !r. The connectives ⊤ and ⊥ can be entered as T and F

5 python build a dynamic growing truth table Jun 13 '11 4 Triggering PHP from ActiveMQ May 28 '10 View all questions and answers → Badges (55)

Python is a good choice here because it has a function called eval that could be helpful. A minimal solution could assume a hard-coded proposition of 4 variables A, B, C, D. But the construction and evaluation of the truth table must be done with loops, not be hard-coded.Use simple Python coding. 4.7K views

##### Comments

- this is they way to go indeed. Thank you all for the answers. I will do it like here described, but the implementations under here are worth to look at and to vote up!
- can you give me a version which uses yield? Would be very helpful for further problems :) I like yield in scala so much ;)
- how would it look like using yield? I'm kinda new in python.
- tip: if you want yield-like functionality, just replace all brackets (except after else) with parentheses, the lambda function will then return a double generator object.