truth table: Nullary and Unary Operations
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(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
questions about Nullary and Unary Operations
Questions to think about as I go through the chapter
requirements
Nullary Operations
There are 2 Nullary operations - Logical True and Logical False. They do not take input and always return True or False
test_logical_true
RED: make it fail
I open the Explorer to change the name of
test_truth_table.pyI right click on
test_truth_table.pyselectRenamethen change it totest_nullary_unary.pyI change the names of the class and method in
test_nullary_unary.py1import 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 11# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_true'because I have not added a definition for logical_true to
truth_table.pyin thesrcfolder
GREEN: make it pass
I add AttributeError to the list of Exceptions seen in
test_nullary_unary.py11# Exceptions seen 12# AssertionError 13# AttributeErrorI use the Explorer to open
truth_table.pyfrom thesrcfolder in the editor of my Integrated Development Environment (IDE)I add a function to
truth_table.py1def logical_true(): 2 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: None is not trueI change None to True in the return statement
1def logical_true(): 2 return Truethe test passes
logical_true does not take any input and always returns True
test_logical_false
RED: make it fail
I add another test to test_nullary_unary.py
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
14# Exceptions seen
the terminal is my friend, and shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_false'. Did you mean: 'logical_true'?
because I have not added a definition for logical_false to truth_table.py, I only added one for logical_true
GREEN: make it pass
I add a function to
truth_table.py1def logical_true(): 2 return True 3 4 5def logical_false(): 6 return Truethe terminal is my friend, and shows AssertionError
AssertionError: True is not falsebecause the function returns True and the test expects False
I change True to False in the return statement
5def logical_false(): 6 return Falsethe test passes
Note
logical_true always returns True
logical_false always returns False
both Nullary Operations do not take input
return |
operation |
|---|---|
True |
|
False |
Unary Operations
There are 2 unary operations: Logical Identity and Logical Negation, they each take one input and return True or False
test_logical_identity
RED: make it fail
I add a new TestCase for Unary Operations with a test for logical_identity when it gets True as input
input |
output |
|---|---|
True |
True |
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 reality = src.truth_table.logical_identity(True)
18 self.assertTrue(reality)
19
20
21# Exceptions seen
the terminal is my friend, and shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_identity'
because I need to add a definition for logical_identity to truth_table.py
GREEN: make it pass
I add the function to
truth_table.py5def logical_false(): 6 return False 7 8 9def logical_identity(): 10 return Falsethe terminal is my friend, and shows TypeError
TypeError: logical_identity() takes 0 positional arguments but 1 was givenbecause the test called the logical_identity function with 1 argument and this definition only takes calls with 0 arguments
I add TypeError to the list of Exceptions seen in
test_nullary_unary.py21# Exceptions seen 22# AssertionError 23# AttributeError 24# TypeErrorI add a name in parentheses for logical_identity to take calls with 1 input, in
truth_table.py9def logical_identity(the_input): 10 return Falsethe terminal is my friend, and shows AssertionError
AssertionError: False is not truebecause the function returns False and the test expects True
I change the return statement to give the test what it wants
9def logical_identity(the_input): 10 return Truethe test passes
REFACTOR: make it better
I add another assertion to test_logical_identity in
test_nullary_unary.pyfor the case when it gets False as inputinput
output
False
False
16 def test_logical_identity(self): 17 reality = src.truth_table.logical_identity(True) 18 self.assertTrue(reality) 19 reality = src.truth_table.logical_identity(False) 20 self.assertFalse(reality) 21 22 23# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: True is not falsebecause the function returns True and the test expects False
I change the return statement of logical_identity in
truth_table.py9def logical_identity(the_input): 10 return Falsethe terminal is my friend, and shows AssertionError
AssertionError: False is not truebecause the assertion that was passing before, expects True and the function now returns False
I change the return statement of the function
9def logical_identity(the_input): 10 return the_inputthe test passes.
Tip
the_inputis a name, I can use any name.This is what happens when the logical_identity function is called
it returns True if the input is True
it returns False, if the input is False
it returns the input as output
input
output
True
True
False
False
test_logical_negation
RED: make it fail
I add a test for logical_negation with an assertion for when it gets True as input, to test_nullary_unary.py
input |
output |
|---|---|
True |
False |
16 def test_logical_identity(self):
17 reality = src.truth_table.logical_identity(True)
18 self.assertTrue(reality)
19 reality = src.truth_table.logical_identity(False)
20 self.assertFalse(reality)
21
22 def test_logical_negation(self):
23 reality = src.truth_table.logical_negation(True)
24 self.assertFalse(reality)
25
26
27# Exceptions seen
the terminal is my friend, and shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_negation'
there is no definition for logical_negation in truth_table.py
GREEN: make it pass
I add the function to
truth_table.py9def logical_identity(the_input): 10 return the_input 11 12 13def logical_negation(the_input): 14 return the_inputthe terminal is my friend, and shows AssertionError
AssertionError: True is not falsebecause the function returned True and the test expects False
I change the return statement to give the test what it wants
13def logical_negation(the_input): 14 return Falsethe test passes
REFACTOR: make it better
I add another assertion to test_logical_negation for when logical_negation gets False as input, in
test_nullary_unary.pyinput
output
False
True
22 def test_logical_negation(self): 23 reality = src.truth_table.logical_negation(True) 24 self.assertFalse(reality) 25 reality = src.truth_table.logical_negation(False) 26 self.assertTrue(reality) 27 28 29# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: False is not truebecause the function returns False and the new assertion expects True
I change the return statement of the logical_negation function in
truth_table.py13def logical_negation(the_input): 14 return Truethe terminal is my friend, and shows AssertionError
AssertionError: True is not falsethe test fails for the assertion that passed before, because the function now returns True and that assertion expects False
I make the function return its input again
13def logical_negation(the_input): 14 return the_inputthe terminal is my friend, and shows AssertionError
AssertionError: True is not falsethis means the expectation of the test is that the logical_negation function
how to return the opposite of a boolean
I can use the “not” keyword to return the opposite of the boolean after it. I add it to the return statement
13def logical_negation(the_input): 14 return not the_inputthe test passes. This is what happens when the logical_identity function is called
I add to the name of the test
22 def test_logical_identity(self): 23 reality = src.truth_table.logical_identity(True) 24 self.assertTrue(reality) 25 reality = src.truth_table.logical_identity(False) 26 self.assertFalse(reality) 27 28 def test_logical_negation_aka_not(self): 29 reality = src.truth_table.logical_negation(True) 30 self.assertFalse(reality) 31 reality = src.truth_table.logical_negation(False) 32 self.assertTrue(reality) 33 34 35# Exceptions seen
logical_negation also known as not returns the opposite of its input
input |
output |
|---|---|
True |
False |
False |
True |
REFACTOR: make it better
I can call the logical_negation function directly in the test, I do not need the
realityvariables because they are only used once for each assertion22 def test_logical_negation_aka_not(self): 23 reality = src.truth_table.logical_negation(True) 24 # self.assertFalse(reality) 25 self.assertFalse(src.truth_table.logical_negation(True)) 26 reality = src.truth_table.logical_negation(False) 27 # self.assertTrue(reality) 28 self.assertTrue(src.truth_table.logical_negation(False)) 29 30 31# Exceptions seenthe test is still green
I remove the commented lines and unused variables
22 def test_logical_negation_aka_not(self): 23 self.assertFalse(src.truth_table.logical_negation(True)) 24 self.assertTrue(src.truth_table.logical_negation(False)) 25 26 27# Exceptions seenI call the logical_identity function directly, in its test
16 def test_logical_identity(self): 17 reality = src.truth_table.logical_identity(True) 18 # self.assertTrue(reality) 19 self.assertTrue(src.truth_table.logical_identity(True)) 20 reality = src.truth_table.logical_identity(False) 21 # self.assertFalse(reality) 22 self.assertFalse(src.truth_table.logical_identity(False)) 23 24 def test_logical_negation_aka_not(self):still green
I remove the commented lines and unused variables from test_logical_identity
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):
close the project
I close
test_nullary_unary.pyandtruth_table.pyin the editorI click in the terminal, then use q on the keyboard to leave the tests. The terminal goes back to the command line
I change directory to the parent of
truth_tablecd ..the terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory
review
I ran these tests for Nullary Operations which take no input
and for Unary operations which take 1 input
the truth table for Unary Operations is
return |
True |
False |
operation |
|---|---|---|---|
the_input |
True |
False |
|
not the_input |
False |
True |
How many questions can you answer after going through this chapter?
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 binary operations? they take 2 inputs
rate pumping python
If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too