booleans: truth table


preview

Sometimes I want programs to make decisions based on inputs or conditions, and can make this happen with if statement. 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?

I can add these to a program so that when it gets information about the person, it can make a decision or return output of True for “Yes, they can vote” or False for “No, they can not vote”

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

All operations from the Truth Table always result in True or False

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

  1import unittest
  2import src.truth_table
  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
 25class TestBinaryOperations(unittest.TestCase):
 26
 27    def test_contradiction(self):
 28        self.assertFalse(src.truth_table.contradiction(True, True))
 29        self.assertFalse(src.truth_table.contradiction(True, False))
 30        self.assertFalse(src.truth_table.contradiction(False, True))
 31        self.assertFalse(src.truth_table.contradiction(False, False))
 32
 33    def test_logical_conjunction(self):
 34        self.assertTrue(src.truth_table.logical_conjunction(True, True))
 35        self.assertFalse(src.truth_table.logical_conjunction(True, False))
 36        self.assertFalse(src.truth_table.logical_conjunction(False, True))
 37        self.assertFalse(src.truth_table.logical_conjunction(False, False))
 38
 39    def test_project_second(self):
 40        self.assertTrue(src.truth_table.project_second(True, True))
 41        self.assertFalse(src.truth_table.project_second(True, False))
 42        self.assertTrue(src.truth_table.project_second(False, True))
 43        self.assertFalse(src.truth_table.project_second(False, False))
 44
 45    def test_converse_non_implication(self):
 46        self.assertFalse(src.truth_table.converse_non_implication(True, True))
 47        self.assertFalse(src.truth_table.converse_non_implication(True, False))
 48        self.assertTrue(src.truth_table.converse_non_implication(False, True))
 49        self.assertFalse(src.truth_table.converse_non_implication(False, False))
 50
 51    def test_negate_first(self):
 52        self.assertFalse(src.truth_table.negate_first(True, True))
 53        self.assertFalse(src.truth_table.negate_first(True, False))
 54        self.assertTrue(src.truth_table.negate_first(False, True))
 55        self.assertTrue(src.truth_table.negate_first(False, False))
 56
 57    def test_logical_nand(self):
 58        self.assertFalse(src.truth_table.logical_nand(True, True))
 59        self.assertTrue(src.truth_table.logical_nand(True, False))
 60        self.assertTrue(src.truth_table.logical_nand(False, True))
 61        self.assertTrue(src.truth_table.logical_nand(False, False))
 62
 63    def test_tautology(self):
 64        self.assertTrue(src.truth_table.tautology(True, True))
 65        self.assertTrue(src.truth_table.tautology(True, False))
 66        self.assertTrue(src.truth_table.tautology(False, True))
 67        self.assertTrue(src.truth_table.tautology(False, False))
 68
 69    def test_logical_disjunction(self):
 70        self.assertTrue(src.truth_table.logical_disjunction(True, True))
 71        self.assertTrue(src.truth_table.logical_disjunction(True, False))
 72        self.assertTrue(src.truth_table.logical_disjunction(False, True))
 73        self.assertFalse(src.truth_table.logical_disjunction(False, False))
 74
 75    def test_exclusive_disjunction(self):
 76        self.assertFalse(src.truth_table.exclusive_disjunction(True, True))
 77        self.assertTrue(src.truth_table.exclusive_disjunction(True, False))
 78        self.assertTrue(src.truth_table.exclusive_disjunction(False, True))
 79        self.assertFalse(src.truth_table.exclusive_disjunction(False, False))
 80
 81    def test_material_non_implication(self):
 82        self.assertFalse(src.truth_table.material_non_implication(True, True))
 83        self.assertTrue(src.truth_table.material_non_implication(True, False))
 84        self.assertFalse(src.truth_table.material_non_implication(False, True))
 85        self.assertFalse(src.truth_table.material_non_implication(False, False))
 86
 87    def test_project_first(self):
 88        self.assertTrue(src.truth_table.project_first(True, True))
 89        self.assertTrue(src.truth_table.project_first(True, False))
 90        self.assertFalse(src.truth_table.project_first(False, True))
 91        self.assertFalse(src.truth_table.project_first(False, False))
 92
 93    def test_converse_implication(self):
 94        self.assertTrue(src.truth_table.converse_implication(True, True))
 95        self.assertTrue(src.truth_table.converse_implication(True, False))
 96        self.assertFalse(src.truth_table.converse_implication(False, True))
 97        self.assertTrue(src.truth_table.converse_implication(False, False))
 98
 99    def test_negate_second(self):
100        self.assertFalse(src.truth_table.negate_second(True, True))
101        self.assertTrue(src.truth_table.negate_second(True, False))
102        self.assertFalse(src.truth_table.negate_second(False, True))
103        self.assertTrue(src.truth_table.negate_second(False, False))
104
105    def test_logical_nor(self):
106        self.assertFalse(src.truth_table.logical_nor(True, True))
107        self.assertFalse(src.truth_table.logical_nor(True, False))
108        self.assertFalse(src.truth_table.logical_nor(False, True))
109        self.assertTrue(src.truth_table.logical_nor(False, False))
110
111    def test_logical_equality(self):
112        self.assertTrue(src.truth_table.logical_equality(True, True))
113        self.assertFalse(src.truth_table.logical_equality(True, False))
114        self.assertFalse(src.truth_table.logical_equality(False, True))
115        self.assertTrue(src.truth_table.logical_equality(False, False))
116
117    def test_material_implication(self):
118        self.assertTrue(src.truth_table.material_implication(True, True))
119        self.assertFalse(src.truth_table.material_implication(True, False))
120        self.assertTrue(src.truth_table.material_implication(False, True))
121        self.assertTrue(src.truth_table.material_implication(False, False))
122
123
124# Exceptions seen
125# AssertionError
126# AttributeError
127# TypeError
128# SyntaxError

questions about The Truth Table


start the project

  • I name this project truth_table

  • I open a terminal

  • then I make a directory for the project

    mkdir truth_table
    

    the terminal goes back to the command line

    .../pumping_python
    
  • I change directory to the project

    cd truth_table
    

    the terminal shows I am now in the truth_table folder

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

    mkdir src
    

    the terminal goes back to the command line

    .../pumping_python/truth_table
    
  • I use touch to make an empty file for the program in the src folder

    touch src/truth_table.py
    

    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

    .../pumping_python/truth_table
    
  • I make a directory for the tests

    mkdir tests
    

    the terminal goes back to the command line

  • I use touch to make an empty file in the tests folder to tell Python that it is a Python package

    Attention

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

    touch tests/__init__.py
    

    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 an empty file for the actual test

    touch tests/test_truth_table.py
    

    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 Visual Studio Code by typing code and the name of the file with

    code tests/test_truth_table.py
    

    test_truth_table.py opens up 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 virtual environment in the terminal

    python3 -m venv .venv
    

    on Windows without Windows Subsystem for Linux use python3 -m venv .venv instead of python3 -m venv .venv

    python -m venv .venv
    

    the terminal takes some time then goes back to the command line

  • I activate the virtual environment

    source .venv/bin/activate
    

    on Windows without Windows Subsystem for Linux use .venv/bin/activate.ps1 instead of source .venv/bin/activate

    .venv/scripts/activate.ps1
    

    the terminal shows

    (.venv) .../pumping_python/truth_table
    
  • I upgrade the Python package manager (pip) to the latest version

    python3 -m pip install --upgrade pip
    

    the terminal shows pip being uninstalled then installs the latest version or shows that it is already the latest version

  • I make a requirements.txt file for the Python programs my project needs

    echo "pytest-watch" > requirements.txt
    

    the terminal goes back to the command line

  • I use pip to use the requirements file to install pytest-watch

    python3 -m pip install --requirement requirements.txt
    

    on Windows without Windows Subsystem for Linux use python -m pip install --requirement requirements.txt instead of python3 -m pip install --requirement requirements.txt

    python -m pip install --requirement requirements.txt
    

    the terminal shows pip downloads and installs the Python programs that pytest-watch needs to run

  • I use pytest-watch to run the test

    pytest-watch
    

    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 hold ctrl (Windows/Linux) or option or command (MacOS) on the keyboard and use the mouse to click on tests/test_truth_table.py:7 to open it in the editor

  • I add AssertionError to the list of Exceptions seen in test_truth_table.py

     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 unittest
    2import src.truth_table
    
  • I CLICK HERE to continue to Truth Table: Nullary and Unary Operations


truth table operations


code from the chapter

Do you want to see all the CODE I typed for the Truth Table?


what is next?

Would you like to test Nullary and Unary Operations?