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.

The following 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(src.truth_table.contradiction(True, True))
  9        self.assertFalse(src.truth_table.contradiction(True, False))
 10        self.assertFalse(src.truth_table.contradiction(False, True))
 11        self.assertFalse(src.truth_table.contradiction(False, False))
 12
 13    def test_logical_conjunction(self):
 14        self.assertTrue(src.truth_table.logical_conjunction(True, True))
 15        self.assertFalse(src.truth_table.logical_conjunction(True, False))
 16        self.assertFalse(src.truth_table.logical_conjunction(False, True))
 17        self.assertFalse(src.truth_table.logical_conjunction(False, False))
 18
 19    def test_project_second(self):
 20        self.assertTrue(src.truth_table.project_second(True, True))
 21        self.assertFalse(src.truth_table.project_second(True, False))
 22        self.assertTrue(src.truth_table.project_second(False, True))
 23        self.assertFalse(src.truth_table.project_second(False, False))
 24
 25    def test_converse_non_implication(self):
 26        self.assertFalse(src.truth_table.converse_non_implication(True, True))
 27        self.assertFalse(src.truth_table.converse_non_implication(True, False))
 28        self.assertTrue(src.truth_table.converse_non_implication(False, True))
 29        self.assertFalse(src.truth_table.converse_non_implication(False, False))
 30
 31    def test_negate_first(self):
 32        self.assertFalse(src.truth_table.negate_first(True, True))
 33        self.assertFalse(src.truth_table.negate_first(True, False))
 34        self.assertTrue(src.truth_table.negate_first(False, True))
 35        self.assertTrue(src.truth_table.negate_first(False, False))
 36
 37    def test_logical_nand(self):
 38        self.assertFalse(src.truth_table.logical_nand(True, True))
 39        self.assertTrue(src.truth_table.logical_nand(True, False))
 40        self.assertTrue(src.truth_table.logical_nand(False, True))
 41        self.assertTrue(src.truth_table.logical_nand(False, False))
 42
 43    def test_tautology(self):
 44        self.assertTrue(src.truth_table.tautology(True, True))
 45        self.assertTrue(src.truth_table.tautology(True, False))
 46        self.assertTrue(src.truth_table.tautology(False, True))
 47        self.assertTrue(src.truth_table.tautology(False, False))
 48
 49    def test_logical_disjunction(self):
 50        self.assertTrue(src.truth_table.logical_disjunction(True, True))
 51        self.assertTrue(src.truth_table.logical_disjunction(True, False))
 52        self.assertTrue(src.truth_table.logical_disjunction(False, True))
 53        self.assertFalse(src.truth_table.logical_disjunction(False, False))
 54
 55    def test_exclusive_disjunction(self):
 56        self.assertFalse(src.truth_table.exclusive_disjunction(True, True))
 57        self.assertTrue(src.truth_table.exclusive_disjunction(True, False))
 58        self.assertTrue(src.truth_table.exclusive_disjunction(False, True))
 59        self.assertFalse(src.truth_table.exclusive_disjunction(False, False))
 60
 61    def test_material_non_implication(self):
 62        self.assertFalse(src.truth_table.material_non_implication(True, True))
 63        self.assertTrue(src.truth_table.material_non_implication(True, False))
 64        self.assertFalse(src.truth_table.material_non_implication(False, True))
 65        self.assertFalse(src.truth_table.material_non_implication(False, False))
 66
 67    def test_project_first(self):
 68        self.assertTrue(src.truth_table.project_first(True, True))
 69        self.assertTrue(src.truth_table.project_first(True, False))
 70        self.assertFalse(src.truth_table.project_first(False, True))
 71        self.assertFalse(src.truth_table.project_first(False, False))
 72
 73    def test_converse_implication(self):
 74        self.assertTrue(src.truth_table.converse_implication(True, True))
 75        self.assertTrue(src.truth_table.converse_implication(True, False))
 76        self.assertFalse(src.truth_table.converse_implication(False, True))
 77        self.assertTrue(src.truth_table.converse_implication(False, False))
 78
 79    def test_negate_second(self):
 80        self.assertFalse(src.truth_table.negate_second(True, True))
 81        self.assertTrue(src.truth_table.negate_second(True, False))
 82        self.assertFalse(src.truth_table.negate_second(False, True))
 83        self.assertTrue(src.truth_table.negate_second(False, False))
 84
 85    def test_logical_nor(self):
 86        self.assertFalse(src.truth_table.logical_nor(True, True))
 87        self.assertFalse(src.truth_table.logical_nor(True, False))
 88        self.assertFalse(src.truth_table.logical_nor(False, True))
 89        self.assertTrue(src.truth_table.logical_nor(False, False))
 90
 91    def test_logical_equality(self):
 92        self.assertTrue(src.truth_table.logical_equality(True, True))
 93        self.assertFalse(src.truth_table.logical_equality(True, False))
 94        self.assertFalse(src.truth_table.logical_equality(False, True))
 95        self.assertTrue(src.truth_table.logical_equality(False, False))
 96
 97    def test_material_implication(self):
 98        self.assertTrue(src.truth_table.material_implication(True, True))
 99        self.assertFalse(src.truth_table.material_implication(True, False))
100        self.assertTrue(src.truth_table.material_implication(False, True))
101        self.assertTrue(src.truth_table.material_implication(False, False))
102
103
104# Exceptions seen
105# AssertionError
106# AttributeError
107# TypeError
108# 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 use the terminal to open a file in the Integrated Development Environment (IDE) by typing the name of the program and the name of the file. That means when 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 I gave 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