truth table

Sometimes I want programs to choose what to do based on inputs or conditions, and can make this happen with if statements. For example, If I want to know if a person can vote, the conditions could be

  • Is the person alive?

  • Is the person old enough?

  • Is the person a citizen?

I can add these to a program so that when it gets information about the person it returns output of True for “Yes, they can vote” or False for “No, they can not vote” after it looks at the options.

These are exercises on writing conditional expressions in Python with the Truth Table from Mathematics and the assertFalse and assertTrue methods from AssertionError, None and booleans.

These are the underlying operations that make the computer do what it does. All operations from the Truth Table always return True or False.


preview

Here are the tests I have at the end of the chapters

 1import src.truth_table
 2import unittest
 3
 4
 5class TestNullaryOperations(unittest.TestCase):
 6
 7    def test_logical_true(self):
 8        self.assertTrue(src.truth_table.logical_true())
 9
10    def test_logical_false(self):
11        self.assertFalse(src.truth_table.logical_false())
12
13
14class TestUnaryOperations(unittest.TestCase):
15
16    def test_logical_identity(self):
17        self.assertTrue(src.truth_table.logical_identity(True))
18        self.assertFalse(src.truth_table.logical_identity(False))
19
20    def test_logical_negation_aka_not(self):
21        self.assertFalse(src.truth_table.logical_negation(True))
22        self.assertTrue(src.truth_table.logical_negation(False))
23
24
25# Exceptions seen
26# AssertionError
27# AttributeError
28# TypeError
  1import src.truth_table
  2import unittest
  3
  4
  5class TestBinaryOperations(unittest.TestCase):
  6
  7    def test_contradiction(self):
  8        self.assertFalse(
  9            src.truth_table.contradiction(True, True)
 10        )
 11        self.assertFalse(
 12            src.truth_table.contradiction(True, False)
 13        )
 14        self.assertFalse(
 15            src.truth_table.contradiction(False, True)
 16        )
 17        self.assertFalse(
 18            src.truth_table.contradiction(False, False)
 19        )
 20
 21    def test_logical_conjunction(self):
 22        self.assertTrue(
 23            src.truth_table.logical_conjunction(True, True)
 24        )
 25        self.assertFalse(
 26            src.truth_table.logical_conjunction(True, False)
 27        )
 28        self.assertFalse(
 29            src.truth_table.logical_conjunction(False, True)
 30        )
 31        self.assertFalse(
 32            src.truth_table.logical_conjunction(False, False)
 33        )
 34
 35    def test_project_second(self):
 36        self.assertTrue(
 37            src.truth_table.project_second(True, True)
 38        )
 39        self.assertFalse(
 40            src.truth_table.project_second(True, False)
 41        )
 42        self.assertTrue(
 43            src.truth_table.project_second(False, True)
 44        )
 45        self.assertFalse(
 46            src.truth_table.project_second(False, False)
 47        )
 48
 49    def test_converse_non_implication(self):
 50        self.assertFalse(
 51            src.truth_table.converse_non_implication(True, True)
 52        )
 53        self.assertFalse(
 54            src.truth_table.converse_non_implication(True, False)
 55        )
 56        self.assertTrue(
 57            src.truth_table.converse_non_implication(False, True)
 58        )
 59        self.assertFalse(
 60            src.truth_table.converse_non_implication(False, False)
 61        )
 62
 63    def test_negate_first(self):
 64        self.assertFalse(src.truth_table.negate_first(True, True))
 65        self.assertFalse(src.truth_table.negate_first(True, False))
 66        self.assertTrue(src.truth_table.negate_first(False, True))
 67        self.assertTrue(src.truth_table.negate_first(False, False))
 68
 69    def test_logical_nand(self):
 70        self.assertFalse(src.truth_table.logical_nand(True, True))
 71        self.assertTrue(src.truth_table.logical_nand(True, False))
 72        self.assertTrue(src.truth_table.logical_nand(False, True))
 73        self.assertTrue(src.truth_table.logical_nand(False, False))
 74
 75    def test_tautology(self):
 76        self.assertTrue(src.truth_table.tautology(True, True))
 77        self.assertTrue(src.truth_table.tautology(True, False))
 78        self.assertTrue(src.truth_table.tautology(False, True))
 79        self.assertTrue(src.truth_table.tautology(False, False))
 80
 81    def test_logical_disjunction(self):
 82        self.assertTrue(
 83            src.truth_table.logical_disjunction(True, True)
 84        )
 85        self.assertTrue(
 86            src.truth_table.logical_disjunction(True, False)
 87        )
 88        self.assertTrue(
 89            src.truth_table.logical_disjunction(False, True)
 90        )
 91        self.assertFalse(
 92            src.truth_table.logical_disjunction(False, False)
 93        )
 94
 95    def test_exclusive_disjunction(self):
 96        self.assertFalse(src.truth_table.exclusive_disjunction(True, True))
 97        self.assertTrue(src.truth_table.exclusive_disjunction(True, False))
 98        self.assertTrue(src.truth_table.exclusive_disjunction(False, True))
 99        self.assertFalse(src.truth_table.exclusive_disjunction(False, False))
100
101    def test_material_non_implication(self):
102        self.assertFalse(
103            src.truth_table.material_non_implication(True, True)
104        )
105        self.assertTrue(
106            src.truth_table.material_non_implication(True, False)
107        )
108        self.assertFalse(
109            src.truth_table.material_non_implication(False, True)
110        )
111        self.assertFalse(
112            src.truth_table.material_non_implication(False, False)
113        )
114
115    def test_project_first(self):
116        self.assertTrue(src.truth_table.project_first(True, True))
117        self.assertTrue(src.truth_table.project_first(True, False))
118        self.assertFalse(src.truth_table.project_first(False, True))
119        self.assertFalse(src.truth_table.project_first(False, False))
120
121    def test_converse_implication(self):
122        self.assertTrue(src.truth_table.converse_implication(True, True))
123        self.assertTrue(src.truth_table.converse_implication(True, False))
124        self.assertFalse(src.truth_table.converse_implication(False, True))
125        self.assertTrue(src.truth_table.converse_implication(False, False))
126
127    def test_negate_second(self):
128        self.assertFalse(src.truth_table.negate_second(True, True))
129        self.assertTrue(src.truth_table.negate_second(True, False))
130        self.assertFalse(src.truth_table.negate_second(False, True))
131        self.assertTrue(src.truth_table.negate_second(False, False))
132
133    def test_logical_nor(self):
134        self.assertFalse(src.truth_table.logical_nor(True, True))
135        self.assertFalse(src.truth_table.logical_nor(True, False))
136        self.assertFalse(src.truth_table.logical_nor(False, True))
137        self.assertTrue(src.truth_table.logical_nor(False, False))
138
139    def test_logical_equality(self):
140        self.assertTrue(src.truth_table.logical_equality(True, True))
141        self.assertFalse(src.truth_table.logical_equality(True, False))
142        self.assertFalse(src.truth_table.logical_equality(False, True))
143        self.assertTrue(src.truth_table.logical_equality(False, False))
144
145    def test_material_implication(self):
146        self.assertTrue(src.truth_table.material_implication(True, True))
147        self.assertFalse(src.truth_table.material_implication(True, False))
148        self.assertTrue(src.truth_table.material_implication(False, True))
149        self.assertTrue(src.truth_table.material_implication(False, False))
150
151
152# Exceptions seen
153# AttributeError
154# TypeError
155# AssertionError
156# SyntaxError

questions about The Truth Table


start the project

  • I name this project truth_table

  • I open a terminal

  • I make a directory for the project

    mkdir truth_table
    

    the terminal goes back to the command line

  • I change directory to the project

    cd truth_table
    

    the terminal shows I am in the truth_table folder

    .../pumping_python/truth_table
    
  • I make a directory for the source code

    mkdir src
    

    the terminal goes back to the command line

  • I make a Python file to hold the source code in the src directory

    touch src/truth_table.py
    

    Note

    on Windows without Windows Subsystem for Linux use New-Item src/truth_table.py instead of touch src/truth_table.py

    New-Item src/truth_table.py
    

    the terminal goes back to the command line

  • I make a directory for the tests

    mkdir tests
    

    the terminal goes back to the command line

  • I make the tests directory a Python package

    Danger

    use 2 underscores (__) before and after init for __init__.py not _init_.py

    touch tests/__init__.py
    

    Note

    on Windows without Windows Subsystem for Linux use New-Item tests/__init__.py instead of touch tests/__init__.py

    New-Item tests/__init__.py
    

    the terminal goes back to the command line

  • I make a Python file for the tests in the tests directory

    touch tests/test_truth_table.py
    

    Note

    on Windows without Windows Subsystem for Linux use New-Item tests/test_truth_table.py instead of touch tests/test_truth_table.py

    New-Item tests/test_truth_table.py
    

    the terminal goes back to the command line

  • I open test_truth_table.py in the editor of the Integrated Development Environment (IDE)

    Tip

    I can open a file from the terminal in the Integrated Development Environment (IDE) with the name of the program and the name of the file. That means if I type this in the terminal

    code tests/test_truth_table.py
    

    Visual Studio Code opens test_truth_table.py in the editor

  • I add the first failing test to test_truth_table.py

    1import unittest
    2
    3
    4class TestTruthTable(unittest.TestCase):
    5
    6    def test_failure(self):
    7        self.assertFalse(True)
    
  • I make a requirements file for the Python packages I need, in the terminal

    echo "pytest" > requirements.txt
    

    the terminal goes back to the command line

  • I add pytest-watcher to the file

    echo "pytest-watcher" >> requirements.txt
    

    the terminal goes back to the command line

  • I setup the project with uv

    uv init
    

    the terminal shows

    Initialized project `truth-table`
    

    then goes back to the command line

  • I remove main.py from the project because I do not use it

    rm main.py
    

    the terminal goes back to the command line

  • I install the Python packages that I wrote in the requirements file

    uv add --requirement requirements.txt
    

    the terminal shows it installed the Python packages

  • I use pytest-watcher to run the tests automatically

    uv run pytest-watcher . --now
    

    the terminal shows

    ================================ FAILURES ================================
    _____________________ TestTruthTable.test_failure ________________________
    
    self = <tests.test_truth_table.TestTruthTable testMethod=test_failure>
    
        def test_failure(self):
    >       self.assertFalse(True)
    E       AssertionError: True is not false
    
    tests/test_truth_table.py:7: AssertionError
    ======================== short test summary info =========================
    FAILED tests/test_truth_table.py::TestTruthTable::test_failure - AssertionError: True is not false
    =========================== 1 failed in X.YZs ============================
    
  • I add AssertionError to the list of Exceptions seen in test_truth_table.py

     4class TestTruthTable(unittest.TestCase):
     5
     6    def test_failure(self):
     7        self.assertFalse(True)
     8
     9
    10# Exceptions seen
    11# AssertionError
    
  • then I change True to False in the assertion

    7        self.assertFalse(False)
    

    the test passes

  • I add an import statement

    1import src.truth_table
    2import unittest
    
  • Click Here to continue to Nullary and Unary Operations


truth table operations


what is next?

Nullary and Unary Operations