Finding a Boolean propositional formula from a truth table

The logic module in SymPy lets us manipulate complex Boolean expressions, also known as propositional formulas.

This recipe will show an example where this module can be useful. Let's suppose that, in a program, we need to write a complex if statement depending on three Boolean variables. We can think about each of the eight possible cases (true, true and false, and so on) and evaluate what the outcome should be. SymPy offers a function to generate a compact logic expression that satisfies our truth table.

How to do it...

  1. Let's import SymPy:
    In [1]: from sympy import *
            init_printing()
  2. Let's define a few symbols:
    In [2]: var('x y z')
  3. We can define propositional formulas with symbols and a few ...

Get IPython Interactive Computing and Visualization Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.