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
What are the possible combinations of two inputs?
start the project
I name this project
truth_tableI open a terminal
then I make a directory for the project
mkdir truth_tablethe terminal goes back to the command line
.../pumping_pythonI change directory to the project
cd truth_tablethe terminal shows I am now in the
truth_tablefolder.../pumping_python/truth_tableI make a folder for the source code
mkdir srcthe terminal goes back to the command line
.../pumping_python/truth_tableI use touch to make an empty file for the program in the
srcfoldertouch src/truth_table.pyon Windows without Windows Subsystem for Linux use
New-Item src/truth_table.pyinstead oftouch src/truth_table.pyNew-Item src/truth_table.pythe terminal goes back to the command line
.../pumping_python/truth_tableI make a directory for the tests
mkdir teststhe terminal goes back to the command line
I use touch to make an empty file in the
testsfolder to tell Python that it is a Python packageAttention
use 2 underscores (__) before and after
initfor__init__.pynot_init_.pytouch tests/__init__.pyon Windows without Windows Subsystem for Linux use
New-Item tests/__init__.pyinstead oftouch tests/__init__.pyNew-Item tests/__init__.pythe terminal goes back to the command line
I make an empty file for the actual test
touch tests/test_truth_table.pyon Windows without Windows Subsystem for Linux use
New-Item tests/test_truth_table.pyinstead oftouch tests/test_truth_table.pyNew-Item tests/test_truth_table.pythe terminal goes back to the command line
I open
test_truth_table.pyin the editor of the Integrated Development Environment (IDE)Tip
I can open a file from the terminal in Visual Studio Code by typing
codeand the name of the file withcode tests/test_truth_table.pytest_truth_table.pyopens up in the editorI add the first failing test to
test_truth_table.py1import 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 .venvon Windows without Windows Subsystem for Linux use
python3 -m venv .venvinstead ofpython3 -m venv .venvpython -m venv .venvthe terminal takes some time then goes back to the command line
I activate the virtual environment
source .venv/bin/activateon Windows without Windows Subsystem for Linux use
.venv/bin/activate.ps1instead ofsource .venv/bin/activate.venv/scripts/activate.ps1the terminal shows
(.venv) .../pumping_python/truth_tableI upgrade the Python package manager (pip) to the latest version
python3 -m pip install --upgrade pipthe terminal shows pip being uninstalled then installs the latest version or shows that it is already the latest version
I make a
requirements.txtfile for the Python programs my project needsecho "pytest-watch" > requirements.txtthe terminal goes back to the command line
I use pip to use the requirements file to install
pytest-watchpython3 -m pip install --requirement requirements.txton Windows without Windows Subsystem for Linux use
python -m pip install --requirement requirements.txtinstead ofpython3 -m pip install --requirement requirements.txtpython -m pip install --requirement requirements.txtthe terminal shows pip downloads and installs the Python programs that pytest-watch needs to run
I use pytest-watch to run the test
pytest-watchthe 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 ontests/test_truth_table.py:7to open it in the editorI add AssertionError to the list of Exceptions seen in
test_truth_table.py7 self.assertFalse(True) 8 9 10# Exceptions seen 11# AssertionErrorthen 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_tableI 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?