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 inputs 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 True for “Yes, they can vote” or False for “No, they can NOT vote”.
These are exercises on writing conditional expressions in Python with the Truth Table from Mathematics and the assertFalse and assertTrue methods from booleans.
The operations in these chapters are fundamental to how the computer works. All operations from the Truth Table always return False or True which can also be thought of as 1 or 0.
preview
These 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(
9 src.truth_table.logical_true()
10 )
11
12 def test_logical_false(self):
13 self.assertFalse(
14 src.truth_table.logical_false()
15 )
16
17
18class TestUnaryOperations(unittest.TestCase):
19
20 def test_logical_identity(self):
21 self.assertTrue(
22 src.truth_table.logical_identity(True)
23 )
24 self.assertFalse(
25 src.truth_table.logical_identity(False)
26 )
27
28 def test_logical_negation_aka_not(self):
29 self.assertFalse(
30 src.truth_table.logical_negation(True)
31 )
32 self.assertTrue(
33 src.truth_table.logical_negation(False)
34 )
35
36
37# Exceptions seen
38# AssertionError
39# AttributeError
40# TypeError
1import src.truth_table
2import unittest
3
4
5class TestBinaryOperations(unittest.TestCase):
6
7 def test_contradiction(self):
8 contradiction = src.truth_table.contradiction
9 self.assertFalse(contradiction(True, True))
10 self.assertFalse(contradiction(True, False))
11 self.assertFalse(contradiction(False, True))
12 self.assertFalse(contradiction(False, False))
13
14 def test_logical_conjunction(self):
15 logical_conjunction = (
16 src.truth_table.logical_conjunction
17 )
18 self.assertTrue(
19 logical_conjunction(True, True)
20 )
21 self.assertFalse(
22 logical_conjunction(True, False)
23 )
24 self.assertFalse(
25 logical_conjunction(False, True)
26 )
27 self.assertFalse(
28 logical_conjunction(False, False)
29 )
30
31 def test_project_second(self):
32 project_second = src.truth_table.project_second
33 self.assertTrue(project_second(True, True))
34 self.assertFalse(project_second(True, False))
35 self.assertTrue(project_second(False, True))
36 self.assertFalse(project_second(False, False))
37
38 def test_converse_non_implication(self):
39 converse_non_implication = (
40 src.truth_table.converse_non_implication
41 )
42 self.assertFalse(
43 converse_non_implication(True, True)
44 )
45 self.assertFalse(
46 converse_non_implication(True, False)
47 )
48 self.assertTrue(
49 converse_non_implication(False, True)
50 )
51 self.assertFalse(
52 converse_non_implication(False, False)
53 )
54
55 def test_negate_first(self):
56 negate_first = src.truth_table.negate_first
57 self.assertFalse(negate_first(True, True))
58 self.assertFalse(negate_first(True, False))
59 self.assertTrue(negate_first(False, True))
60 self.assertTrue(negate_first(False, False))
61
62 def test_logical_nand(self):
63 logical_nand = src.truth_table.logical_nand
64 self.assertFalse(logical_nand(True, True))
65 self.assertTrue(logical_nand(True, False))
66 self.assertTrue(logical_nand(False, True))
67 self.assertTrue(logical_nand(False, False))
68
69 def test_tautology(self):
70 tautology = src.truth_table.tautology
71 self.assertTrue(tautology(True, True))
72 self.assertTrue(tautology(True, False))
73 self.assertTrue(tautology(False, True))
74 self.assertTrue(tautology(False, False))
75
76 def test_logical_disjunction(self):
77 logical_disjunction = (
78 src.truth_table.logical_disjunction
79 )
80 self.assertTrue(logical_disjunction(True, True))
81 self.assertTrue(logical_disjunction(True, False))
82 self.assertTrue(logical_disjunction(False, True))
83 self.assertFalse(logical_disjunction(False, False))
84
85 def test_exclusive_disjunction(self):
86 exclusive_disjunction = (
87 src.truth_table.exclusive_disjunction
88 )
89 self.assertFalse(exclusive_disjunction(True, True))
90 self.assertTrue(exclusive_disjunction(True, False))
91 self.assertTrue(exclusive_disjunction(False, True))
92 self.assertFalse(exclusive_disjunction(False, False))
93
94 def test_material_non_implication(self):
95 material_non_implication = (
96 src.truth_table.material_non_implication
97 )
98 self.assertFalse(
99 material_non_implication(True, True)
100 )
101 self.assertTrue(
102 material_non_implication(True, False)
103 )
104 self.assertFalse(
105 material_non_implication(False, True)
106 )
107 self.assertFalse(
108 material_non_implication(False, False)
109 )
110
111 def test_project_first(self):
112 project_first = src.truth_table.project_first
113 self.assertTrue(project_first(True, True))
114 self.assertTrue(project_first(True, False))
115 self.assertFalse(project_first(False, True))
116 self.assertFalse(project_first(False, False))
117
118 def test_converse_implication(self):
119 converse_implication = (
120 src.truth_table.converse_implication
121 )
122 self.assertTrue(
123 converse_implication(True, True)
124 )
125 self.assertTrue(
126 converse_implication(True, False)
127 )
128 self.assertFalse(
129 converse_implication(False, True)
130 )
131 self.assertTrue(
132 converse_implication(False, False)
133 )
134
135 def test_negate_second(self):
136 negate_second = src.truth_table.negate_second
137 self.assertFalse(negate_second(True, True))
138 self.assertTrue(negate_second(True, False))
139 self.assertFalse(negate_second(False, True))
140 self.assertTrue(negate_second(False, False))
141
142 def test_logical_nor(self):
143 logical_nor = src.truth_table.logical_nor
144 self.assertFalse(logical_nor(True, True))
145 self.assertFalse(logical_nor(True, False))
146 self.assertFalse(logical_nor(False, True))
147 self.assertTrue(logical_nor(False, False))
148
149 def test_logical_equality(self):
150 logical_equality = (
151 src.truth_table.logical_equality
152 )
153 self.assertTrue(logical_equality(True, True))
154 self.assertFalse(logical_equality(True, False))
155 self.assertFalse(logical_equality(False, True))
156 self.assertTrue(logical_equality(False, False))
157
158 def test_material_implication(self):
159 material_implication = (
160 src.truth_table.material_implication
161 )
162 self.assertTrue(
163 material_implication(True, True)
164 )
165 self.assertFalse(
166 material_implication(True, False)
167 )
168 self.assertTrue(
169 material_implication(False, True)
170 )
171 self.assertTrue(
172 material_implication(False, False)
173 )
174
175
176# Exceptions seen
177# AttributeError
178# TypeError
179# AssertionError
180# SyntaxError
start the project
I name this project
truth_tableI open a terminal
I use uv to make a directory for the project and initialize it
uv init truth_tablethe terminal shows
Initialized project `truth_table` at `.../pumping_python/truth_table`then goes back to the command line.
I change directory to the project
cd truth_tablethe terminal shows I am in the
truth_tablefolder.../pumping_python/truth_tableI make a directory for the source code
mkdir srcthe terminal goes back to the command line.
I use the mv program to change the name of
main.pytotruth_table.pyand move it to thesrcfoldermv main.py src/truth_table.pyMove-Item main.py src/truth_table.pythe terminal goes back to the command line.
I make a directory for the tests
mkdir teststhe terminal goes back to the command line.
I make the
testsdirectory a Python packageDanger
use 2 underscores (__) before and after
initfor__init__.pynot_init_.pytouch tests/__init__.pyNew-Item tests/__init__.pythe terminal goes back to the command line.
I make a Python file for the tests in the
testsdirectorytouch tests/test_truth_table.pyNew-Item tests/test_truth_table.pythe terminal goes back to the command line.
I open
test_truth_table.pyI 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 go back to the terminal to make a requirements file for the Python packages I need
echo "pytest" > requirements.txtthe terminal goes back to the command line.
I add pytest-watcher to the requirements file
echo "pytest-watcher" >> requirements.txtthe terminal goes back to the command line.
I use uv to install pytest-watcher with the requirements file
uv add --requirement requirements.txtthe terminal shows that it installed pytest-watcher and its dependencies.
I add the new files and folder to git for tracking
git add .the terminal goes back to the command line.
I add a git commit message
git commit -am 'setup project'the terminal shows a summary of the changes then goes back to the command line.
I use pytest-watcher to run the tests automatically
uv run pytest-watcher . --nowthe terminal is my friend, and shows AssertionError
======================== 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 ====================if the terminal does not show the same error, then check
if your
tests/__init__.pyhas two underscores (__) before and afterinitfor__init__.pynot_init_.pyif you ran
echo "pytest-watcher" >> requirements.txt, to addpytest-watcherto the requirements file
fix those errors and try to run
uv run pytest-watcher . --nowagainI add AssertionError to the list of Exceptions seen in
test_truth_table.py4class TestTruthTable(unittest.TestCase): 5 6 def test_failure(self): 7 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 at the top of the file
1import src.truth_table 2import unittest