truth table: Binary Operations part 4
requirements
how to get back to the automated tests
If your tests stopped after the previous chapter, heres’s how to get back to the tests
Make sure you are in the
pumping_pythonfolder with pwd in the terminalpwdif the terminal shows anything other than
.../pumping_pythonyou need to change directory to the
pumping_pythonfolderOnce in the
pumping_pythondirectory, change directory to the projectcd truth_tablethe terminal shows
.../pumping_python/truth_tableactivate the Virtual Environment
source .venv/bin/activateon Windows without Windows Subsystem for Linux use
.venv/scripts/activate.ps1instead ofsource .venv/bin/activate.venv/scripts/activate.ps1when the Virtual Environment is activated, the terminal shows
(.venv) .../pumping_python/truth_tablerun the tests
pytest-watch
test_negate_second
RED: make it fail
I add a new test for another Binary Operation to test_truth_table.py with the first case where both first_input and second_input are 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
102# Exceptions seen
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'negate_second'
there is no definition for it yet
GREEN: make it pass
I add a function definition for negate_second to truth_table.py
63def converse_implication(first_input, second_input):
64 return first_input or not second_input
65
66
67def negate_second(first_input, second_input):
68 return False
the test passes. negate_second returns False when the two inputs are True
REFACTOR: make it better
I add the next case - when
first_inputis True andsecond_inputis False, totest_negate_secondintest_truth_table.py99 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))the terminal shows AssertionError
AssertionError: False is not trueI add an if statement to
negate_secondintruth_table.py67def negate_second(first_input, second_input): 68 if first_input == True and second_input == False: 69 return True 70 return Falsethe test passes.
negate_secondreturnsI add the third case - when the first input is False and the second input is True, to
test_negate_secondintest_truth_table.py99 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))the test is still passing.
negate_secondreturnsI add the last case - when the two inputs are False
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))the terminal shows AssertionError
AssertionError: False is not trueI add an if statement for the case to
negate_secondintruth_table.py67def negate_second(first_input, second_input): 68 if first_input == False and second_input == False: 69 return True 70 if first_input == True and second_input == False: 71 return True 72 return Falsethe test passes.
negate_secondreturnsI add a return statement to show that
negate_secondalways returns the opposite of the second input67def negate_second(first_input, second_input): 68 return not second_input 69 if first_input == False and second_input == False: 70 return True 71 if first_input == True and second_input == False: 72 return True 73 return Falsethe test is still green
I remove the other statements
67def negate_second(first_input, second_input): 68 return not second_input
Negate Second always returns
not second_inputthe Logical Negation of the second input
it is the opposite or Logical Negation of Project Second which always returns the second input
test_logical_nor
RED: make it fail
I add a test for logical_nor in test_truth_table.py
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
108
109# Exceptions seen
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_nor'. Did you mean: 'logical_nand'?
GREEN: make it pass
I add the function to truth_table.py
67def negate_second(first_input, second_input):
68 return not second_input
69
70
71def logical_nor(first_input, second_input):
72 return False
the test passes. logical_nor returns False when the first and second inputs are both True
REFACTOR: make it better
I add the next case to
test_logical_norintest_truth_table.py- whenfirst_inputis True andsecond_inputis False105 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))the terminal still shows green.
logical_norreturnson to the next case - when the first input is False and the second input is True
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))the test is still green.
logical_norreturnsI add the last case - when the two inputs are False
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 112# Exceptions seenthe terminal shows AssertionError
AssertionError: False is not truethis is the only case where
logical_norreturns TrueI add a conditional expression for it in
truth_table.py71def logical_nor(first_input, second_input): 72 return True if (first_input == False and second_input == False) else False 73 return Falsethe test passes.
logical_norreturnsI remove the second return statement then use the simpler form of the conditional expression
71def logical_nor(first_input, second_input): 72 return first_input == False and second_input == False 73 return True if (first_input == False and second_input == False) else Falsethe test is still green
I remove the second return statement and write the first one in terms of True
71def logical_nor(first_input, second_input): 72 return not first_input == True and not second_input == True 73 return first_input == False and second_input == Falsestill green
I remove the second return statement and make the first one simpler with bool
71def logical_nor(first_input, second_input): 72 return not bool(first_input) and not bool(second_input) 73 return not first_input == True and not second_input == Truethe terminal still shows green
I make the statement simpler again
71def logical_nor(first_input, second_input): 72 return not first_input and not second_input 73 return not bool(first_input) and not bool(second_input)green
I remove the second return statement then factor out “not” since it happens 2 times in the first statement
71def logical_nor(first_input, second_input): 72 return (not first_input) (not or) (not second_input) 73 return not first_input and not second_inputthe terminal shows SyntaxError
SyntaxError: invalid syntaxI comment out the line then add the correct statement
71def logical_nor(first_input, second_input): 72 return not (first_input or second_input) 73 # return (not first_input) (not or) (not second_input) 74 return not first_input and not second_inputgreen, green, green again
I remove the other statements
71def logical_nor(first_input, second_input): 72 return not (first_input or second_input)
Logical NOR returns
not (first_input or second_input)the Logical Negation of the Logical Disjunction (or) of the first input and second input
test_logical_equality
RED: make it fail
I add a new test for the next Binary Operation in test_truth_table.py
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
114
115# Exceptions seen
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_equality'. Did you mean: 'logical_identity'?
GREEN: make it pass
I add a function definition for it in truth_table.py
71def logical_nor(first_input, second_input):
72 return not (first_input or second_input)
73
74
75def logical_equality(first_input, second_input):
76 return True
the test passes. logical_equality returns True when the two inputs are True
REFACTOR: make it better
I add the next case to
test_logical_equalityintest_truth_table.py111 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))the terminal shows AssertionError
AssertionError: True is not falseI add an if statement to
logical_equalityintruth_table.py75def logical_equality(first_input, second_input): 76 if first_input and not second_input: 77 return False 78 return Truethe test passes.
logical_equalityreturnsI add the next case to
test_logical_equalityintest_truth_table.py111 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))the terminal shows AssertionError
AssertionError: True is not falseI add another if statement to
logical_equalityintruth_table.py75def logical_equality(first_input, second_input): 76 if not first_input and second_input: 77 return False 78 if first_input and not second_input: 79 return False 80 return Truethe test passes.
logical_equalityreturnsI add the last case to
test_logical_equalityintest_truth_table.py111 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 118# Exceptions seenthe test is still green.
logical_equalityreturnsI use “or” to put the 2 statements that return False together, since they are at the same level in
logical_equalityintruth_table.py75def logical_equality(first_input, second_input): 76 if (not first_input and second_input) or (first_input and not second_input): 77 return False 78 # if not first_input and second_input: 79 # return False 80 # if first_input and not second_input: 81 # return False 82 return Truestill green
I remove the commented lines
75def logical_equality(first_input, second_input): 76 if (not first_input and second_input) or (first_input and not second_input): 77 return False 78 return Truethe test is still passing
I write a new return statement with not to replace the if statement
75def logical_equality(first_input, second_input): 76 return not ((not first_input and second_input) or (first_input and not second_input)) 77 if (not first_input and second_input) or (first_input and not second_input): 78 return False 79 return Truethe test is still green
I remove the other statements then “multiply not” by every symbol in the parentheses
75def logical_equality(first_input, second_input): 76 return ( 77 (not (not first_input and second_input)) 78 (not or) 79 (not (first_input and not second_input)) 80 ) 81 return not ((not first_input and second_input) or (first_input and not second_input))the terminal shows SyntaxError
SyntaxError: invalid syntax75def logical_equality(first_input, second_input): 76 return ( 77 (not (not first_input and second_input)) 78 and 79 (not (first_input and not second_input)) 80 ) 81 return not ((not first_input and second_input) or (first_input and not second_input))the test is green again
I remove the other return statement then “multiply” “ not in the first parentheses
75def logical_equality(first_input, second_input): 76 return ( 77 (not not first_input not and not second_input) 78 # (not (not first_input and second_input)) 79 and 80 (not (first_input and not second_input)) 81 )the terminal shows SyntaxError
SyntaxError: invalid syntax75def logical_equality(first_input, second_input): 76 return ( 77 (not not first_input or not second_input) 78 # (not (not first_input and second_input)) 79 and 80 (not (first_input and not second_input)) 81 )green again
I remove the commented line and “multiply” not in the next statement
75def logical_equality(first_input, second_input): 76 return ( 77 (not not first_input or not second_input) 78 and 79 (not first_input not and not not second_input) 80 # (not (first_input and not second_input)) 81 )the terminal shows SyntaxError
SyntaxError: invalid syntax-
75def logical_equality(first_input, second_input): 76 return ( 77 (not not first_input or not second_input) 78 and 79 (not first_input or not not second_input) 80 # (not (first_input and not second_input)) 81 )the test is green again
I remove the commented line and the “not not” from both parentheses since they cancel out
75def logical_equality(first_input, second_input): 76 return ( 77 (first_input or not second_input) 78 and 79 (not first_input or second_input) 80 )still green
The 2 cases where
logical_equalityreturns True are whenfirst_inputandsecond_inputare the same, which means I can write a much simpler return statement thanks to the equality symbol (2 equal signs together =+=)75def logical_equality(first_input, second_input): 76 return first_input == second_input 77 return ( 78 (first_input or not second_input) 79 and 80 (not first_input or second_input) 81 )the test is still green
Logical Equality returns
first_input == second_inputTrue when the two inputs are the same
False when the two inputs are NOT the same
the Logical Conjunction of the result of the Logical Disjunction of the first input and the Logical Negation of the second input, and the result of the Logical Disjunction of the Logical Negation of the first input and the second input
is the opposite of Exclusive Disjunction which returns False when the two inputs are the same
test_material_implication
RED: make it fail
I add a new test for one more Binary Operation in test_truth_table.py
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
120
121# Exceptions seen
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'material_implication'
GREEN: make it pass
I add a method for material_implication in truth_table.py
75def logical_equality(first_input, second_input):
76 return first_input == second_input
77 return (
78 (first_input or not second_input)
79 and
80 (not first_input or second_input)
81 )
82
83
84def material_implication(first_input, second_input):
85 return True
the test passes. material_implication returns True when the two inputs are True
REFACTOR: make it better
I add the next case to
test_material_implicationintest_truth_table.py117 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))the terminal shows AssertionError
AssertionError: True is not falseI add an if statement to
material_implicationintruth_table.py84def material_implication(first_input, second_input): 85 if first_input and not second_input: 86 return False 87 return Truethe test passes.
material_implicationreturnsI add the next case to
test_material_implicationintest_truth_table.py117 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))the test is still green.
material_implicationreturnsI add the fourth case
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# Exceptions seenthe test is still passing.
material_implicationreturnsthere is only one case where
material_implicationreturns False, I add a return statement for it with Logical Negation since the if statement returns False intruth_table.py84def material_implication(first_input, second_input): 85 return not (first_input and not second_input) 86 if first_input and not second_input: 87 return False 88 return Truethe test is still green
I remove the other statements then “multiply not” by the symbols in the parentheses
84def material_implication(first_input, second_input): 85 return (not first_input) (not and) (not not second_input) 86 return not (first_input and not second_input)the terminal shows SyntaxError
SyntaxError: invalid syntax-
84def material_implication(first_input, second_input): 85 return (not first_input) or (not not second_input) 86 return not (first_input and not second_input)the test is green again
-
84def material_implication(first_input, second_input): 85 return not first_input or second_input 86 return (not first_input) or (not not second_input)the test is still passing
I remove the other statement
84def material_implication(first_input, second_input): 85 return not first_input or second_input
Material Implication aka Logical Implication returns
not first_input or second_inputthe Logical Disjunction of the Logical Negation of the first input, and the second input
False only when the first input is True and the second input is False
it is the opposite or Logical Negation of Material NonImplication which only returns True when the first input is True and the second input is False
review
Binary Operations take 2 inputs, each input can be True or False, if we name the first input first_input and the second one second_input, the tests show that
-
returns
not first_input or second_inputreturns False when
first_inputis True andsecond_inputis Falseis the opposite or Logical Negation of Material NonImplication which returns True only when
first_inputis True andsecond_inputis False
-
returns
first_input == second_inputreturns True when
first_inputandsecond_inputare equalis the opposite or Logical Negation of Exclusive Disjunction (Exclusive OR) which only returns True when the two inputs are NOT equal
-
returns
not (first_input or second_input)returns True when
first_inputandsecond_inputare both Falseis the opposite or Logical Negation of Logical Disjunction which only returns False when the two inputs are False
-
returns
not second_inputis the opposite or Logical Negation of Project Second which only returns True when
second_inputis True
-
returns
first_input or not second_inputreturns False when
first_inputis False andsecond_inputis Trueis the opposite or Logical Negation of Converse NonImplication which only returns True when
first_inputis False andsecond_inputis True
-
returns
first_inputis the opposite or Logical Negation of Negate First which only returns True when
first_inputis False
-
returns
first_input and not second_inputreturns True when
first_inputis False andsecond_inputis Trueis the opposite or Logical Negation of Material or Logical Implication which only returns False when
first_inputis True andsecond_inputis False
-
returns
first_input != second_inputreturns True when
first_inputandsecond_inputare NOT equalis the opposite or Logical Negation of Logical Equality which only returns True when the two inputs are equal
-
returns
first_input or second_inputreturns False when
first_inputandsecond_inputare both Falseis the opposite or Logical Negation of Logical NOR which only returns True when the two inputs are False
-
always returns True
is the opposite or Logical Negation of Contradiction which always returns False
-
returns
not (first_input and second_input)returns False when
first_inputandsecond_inputare both Trueis the opposite or Logical Negation (not) of Logical Conjunction (and) which only returns True when the two inputs are True
-
returns
not first_inputis the opposite or Logical Negation of Project First which only returns True when
first_inputis True
-
returns
not first_input and second_inputreturns True when
first_inputis False andsecond_inputis Trueis the opposite or Logical Negation of Converse Implication which only returns False when
first_inputis False andsecond_inputis True
-
returns
second_inputis the opposite or Logical Negation of Negate Second which only returns True when
second_inputis False
Logical Conjunction returns
returns
first_input and second_inputis the opposite or Logical Negation of Logical NAND which only returns False when the two inputs are True
and
Logical Disjunction is “or”
Logical Conjunction is “and”
Logical Negation is “not”
All the logic statements or conditions have been written with some or all of the above 3.
code from the chapter
Do you want to see all the CODE I typed for the Truth Table?