truth table: Binary Operations 3
requirements
preview
Here are the tests I have at the end of this chapter
1import src.truth_table
2import unittest
3
4
5class TestBinaryOperations(unittest.TestCase):
6
7 def test_contradiction(self):
8 self.assertFalse(
9 src.truth_table.contradiction(True, True)
10 )
11 self.assertFalse(
12 src.truth_table.contradiction(True, False)
13 )
14 self.assertFalse(
15 src.truth_table.contradiction(False, True)
16 )
17 self.assertFalse(
18 src.truth_table.contradiction(False, False)
19 )
20
21 def test_logical_conjunction(self):
22 self.assertTrue(
23 src.truth_table.logical_conjunction(True, True)
24 )
25 self.assertFalse(
26 src.truth_table.logical_conjunction(True, False)
27 )
28 self.assertFalse(
29 src.truth_table.logical_conjunction(False, True)
30 )
31 self.assertFalse(
32 src.truth_table.logical_conjunction(False, False)
33 )
34
35 def test_project_second(self):
36 self.assertTrue(
37 src.truth_table.project_second(True, True)
38 )
39 self.assertFalse(
40 src.truth_table.project_second(True, False)
41 )
42 self.assertTrue(
43 src.truth_table.project_second(False, True)
44 )
45 self.assertFalse(
46 src.truth_table.project_second(False, False)
47 )
48
49 def test_converse_non_implication(self):
50 self.assertFalse(
51 src.truth_table.converse_non_implication(True, True)
52 )
53 self.assertFalse(
54 src.truth_table.converse_non_implication(True, False)
55 )
56 self.assertTrue(
57 src.truth_table.converse_non_implication(False, True)
58 )
59 self.assertFalse(
60 src.truth_table.converse_non_implication(False, False)
61 )
62
63 def test_negate_first(self):
64 self.assertFalse(src.truth_table.negate_first(True, True))
65 self.assertFalse(src.truth_table.negate_first(True, False))
66 self.assertTrue(src.truth_table.negate_first(False, True))
67 self.assertTrue(src.truth_table.negate_first(False, False))
68
69 def test_logical_nand(self):
70 self.assertFalse(src.truth_table.logical_nand(True, True))
71 self.assertTrue(src.truth_table.logical_nand(True, False))
72 self.assertTrue(src.truth_table.logical_nand(False, True))
73 self.assertTrue(src.truth_table.logical_nand(False, False))
74
75 def test_tautology(self):
76 self.assertTrue(src.truth_table.tautology(True, True))
77 self.assertTrue(src.truth_table.tautology(True, False))
78 self.assertTrue(src.truth_table.tautology(False, True))
79 self.assertTrue(src.truth_table.tautology(False, False))
80
81 def test_logical_disjunction(self):
82 self.assertTrue(
83 src.truth_table.logical_disjunction(True, True)
84 )
85 self.assertTrue(
86 src.truth_table.logical_disjunction(True, False)
87 )
88 self.assertTrue(
89 src.truth_table.logical_disjunction(False, True)
90 )
91 self.assertFalse(
92 src.truth_table.logical_disjunction(False, False)
93 )
94
95 def test_exclusive_disjunction(self):
96 self.assertFalse(src.truth_table.exclusive_disjunction(True, True))
97 self.assertTrue(src.truth_table.exclusive_disjunction(True, False))
98 self.assertTrue(src.truth_table.exclusive_disjunction(False, True))
99 self.assertFalse(src.truth_table.exclusive_disjunction(False, False))
100
101 def test_material_non_implication(self):
102 self.assertFalse(
103 src.truth_table.material_non_implication(True, True)
104 )
105 self.assertTrue(
106 src.truth_table.material_non_implication(True, False)
107 )
108 self.assertFalse(
109 src.truth_table.material_non_implication(False, True)
110 )
111 self.assertFalse(
112 src.truth_table.material_non_implication(False, False)
113 )
114
115 def test_project_first(self):
116 self.assertTrue(src.truth_table.project_first(True, True))
117 self.assertTrue(src.truth_table.project_first(True, False))
118 self.assertFalse(src.truth_table.project_first(False, True))
119 self.assertFalse(src.truth_table.project_first(False, False))
120
121 def test_converse_implication(self):
122 self.assertTrue(src.truth_table.converse_implication(True, True))
123 self.assertTrue(src.truth_table.converse_implication(True, False))
124 self.assertFalse(src.truth_table.converse_implication(False, True))
125 self.assertTrue(src.truth_table.converse_implication(False, False))
126
127
128# Exceptions seen
129# AttributeError
130# TypeError
131# AssertionError
132# SyntaxError
continue the project
Make sure you are in the
pumping_pythonfolder with pwd in the terminalpwdif the terminal shows anything other than
.../pumping_pythonchange directory to the
pumping_pythonfolderOnce in
pumping_python, change directory to the projectcd truth_tablethe terminal shows
.../pumping_python/truth_tableI run the tests with pytest-watcher
uv run pytest-watcher . --nowthe terminal shows
rootdir: .../pumping_python/truth_table configfile: pyproject.toml collected 12 items tests/test_binary.py ............ [ 66%] tests/test_nullary_unary.py .... [100%] ======================== 12 passed in G.HIs ========================I hold ctrl (Windows) or option (MacOS) on the keyboard, then click on
tests/test_binary.pywith the mouse to open it in the editor
test_exclusive_disjunction
RED: make it fail
I add a new test to test_binary.py
82 def test_logical_disjunction(self):
83 self.assertTrue(
84 src.truth_table.logical_disjunction(True, True)
85 )
86 self.assertTrue(
87 src.truth_table.logical_disjunction(True, False)
88 )
89 self.assertTrue(
90 src.truth_table.logical_disjunction(False, True)
91 )
92 self.assertFalse(
93 src.truth_table.logical_disjunction(False, False)
94 )
95
96 def test_exclusive_disjunction(self):
97 self.assertFalse(src.truth_table.exclusive_disjunction(True, True))
98
99
100# Exceptions seen
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'exclusive_disjunction'
GREEN: make it pass
I add exclusive_disjunction to truth_table.py
47def logical_disjunction(first_input, second_input):
48 return first_input or second_input
49
50
51def exclusive_disjunction(first_input, second_input):
52 return False
the test passes. exclusive_disjunction returns False, if the first input is True and the second input is True
REFACTOR: make it better
I add the next case - where the first input is True and the second input is False, to test_exclusive_disjunction in
test_binary.py95 def test_exclusive_disjunction(self): 96 self.assertFalse(src.truth_table.exclusive_disjunction(True, True)) 97 self.assertTrue(src.truth_table.exclusive_disjunction(True, False))the terminal shows AssertionError
AssertionError: False is not trueI add if statements to exclusive_disjunction in
truth_table.py51def exclusive_disjunction(first_input, second_input): 52 if first_input == True: 53 if second_input == False: 54 return True 55 return Falsethe test passes. exclusive_disjunction returns
I add the third case - where the first input is False and the second input is True, to
test_binary.py95 def test_exclusive_disjunction(self): 96 self.assertFalse(src.truth_table.exclusive_disjunction(True, True)) 97 self.assertTrue(src.truth_table.exclusive_disjunction(True, False)) 98 self.assertTrue(src.truth_table.exclusive_disjunction(False, True))the terminal shows AssertionError
AssertionError: False is not trueI add if statements to exclusive_disjunction in
truth_table.py51def exclusive_disjunction(first_input, second_input): 52 if first_input == False: 53 if second_input == True: 54 return True 55 if first_input == True: 56 if second_input == False: 57 return True 58 return Falsethe test passes. exclusive_disjunction returns
I add the last case - when the first input is False and the second input is True to test_exclusive_disjunction, in
test_binary.py95 def test_exclusive_disjunction(self): 96 self.assertFalse(src.truth_table.exclusive_disjunction(True, True)) 97 self.assertTrue(src.truth_table.exclusive_disjunction(True, False)) 98 self.assertTrue(src.truth_table.exclusive_disjunction(False, True)) 99 self.assertFalse(src.truth_table.exclusive_disjunction(False, False)) 100 101 102# Exceptions seenthe test is still green. exclusive_disjunction returns
I put the first two if statements together to make them simpler in exclusive_disjunction in
truth_table.py51def exclusive_disjunction(first_input, second_input): 52 # if first_input == False: 53 # if second_input == True: 54 if first_input == False and second_input == True: 55 return True 56 if first_input == True: 57 if second_input == False: 58 return True 59 return Falsethe test is still green
I do the same thing with the next two if statements
51def exclusive_disjunction(first_input, second_input): 52 if first_input == False and second_input == True: 53 return True 54 # if first_input == True: 55 # if second_input == False: 56 if first_input == True and second_input == False: 57 return True 58 return Falsestill green
I add not to the first if statement
51def exclusive_disjunction(first_input, second_input): 52 # if first_input == False and second_input == True: 53 if not first_input == True and second_input == True: 54 return True 55 if first_input == True and second_input == False: 56 return True 57 return Falsegreen
I do the same thing to the second if statement
51def exclusive_disjunction(first_input, second_input): 52 if not first_input == True and second_input == True: 53 return True 54 # if first_input == True and second_input == False: 55 if first_input == True and not second_input == True: 56 return True 57 return Falsestill green
I add bool
51def exclusive_disjunction(first_input, second_input): 52 # if not first_input == True and second_input == True: 53 if not bool(first_input) == True and bool(second_input) == True: 54 return True 55 # if first_input == True and not second_input == True: 56 if bool(first_input) == True and not bool(second_input) == True: 57 return True 58 return Falsegreen
I remove
== True51def exclusive_disjunction(first_input, second_input): 52 # if not bool(first_input) == True and bool(second_input) == True: 53 if not bool(first_input) and bool(second_input): 54 return True 55 # if bool(first_input) == True and not bool(second_input) == True: 56 if bool(first_input) and not bool(second_input): 57 return True 58 return Falsestill green
I remove bool
51def exclusive_disjunction(first_input, second_input): 52 # if not bool(first_input) and bool(second_input): 53 if not first_input and second_input: 54 return True 55 # if bool(first_input) and not bool(second_input): 56 if first_input and not second_input: 57 return True 58 return Falsethe test is still green
I use Logical Disjunction to put the two if statements together because they both return True
Tip
I can put two if statements together with Logical Disjunction (or) when they are at the same indentation level and return the same thing.
For example
if something: return this if something_else: return thiscan also be written as
if something or something_else: return this51def exclusive_disjunction(first_input, second_input): 52 # if not first_input and second_input: 53 # return True 54 # if first_input and not second_input: 55 # return True 56 if ( 57 (not first_input and second_input) 58 or 59 (first_input and not second_input) 60 ): 61 return True 62 return Falsestill green
I use a conditional expression
51def exclusive_disjunction(first_input, second_input): 52 return True if ( 53 (not first_input and second_input) 54 or 55 (first_input and not second_input) 56 ) else False 57 if ( 58 (not first_input and second_input) 59 or 60 (first_input and not second_input) 61 ): 62 return True 63 return Falsegreen
I remove the other statements in the function and make the ternary operator simpler
51def exclusive_disjunction(first_input, second_input): 52 return ( 53 (not first_input and second_input) 54 or 55 (first_input and not second_input) 56 ) 57 return True if ( 58 (not first_input and second_input) 59 or 60 (first_input and not second_input) 61 ) else Falsewhy is the grass greener on the other side?
I remove the second return statement
51def exclusive_disjunction(first_input, second_input): 52 return ( 53 (not first_input and second_input) 54 or 55 (first_input and not second_input) 56 )still green
exclusive_disjunction returns False when
first_inputandsecond_inputare the same and returns True, if they are NOT. I add an if statement to show this with the equality symbol (2 equal signs together =+=)51def exclusive_disjunction(first_input, second_input): 52 if first_input == second_input: 53 return False 54 else: 55 return True 56 return ( 57 (not first_input and second_input) 58 or 59 (first_input and not second_input) 60 )the test is still green
I change the new if statement to a simple return statement
51def exclusive_disjunction(first_input, second_input): 52 return not (first_input == second_input) 53 if first_input == second_input: 54 return False 55 else: 56 return True 57 return ( 58 (not first_input and second_input) 59 or 60 (first_input and not second_input) 61 )still green
I remove the commented lines, then use an even simpler return statement with the NOT equal symbol (
!=) (exclamation mark and equal symbol !+= on the keyboard)51def exclusive_disjunction(first_input, second_input): 52 return first_input != second_input 53 return not (first_input == second_input) 54 return ( 55 (not first_input and second_input) 56 or 57 (first_input and not second_input) 58 )
Exclusive Disjunction returns
True, if the first input is NOT EQUAL to the second input
False, if the first input is EQUAL to the second input
first_input != second_input- which reads as first input is NOT equal to second inputnot (first_input == second_input)- which reads as the Logical Negation of the Logical Equality of the first input and the second input(not first_input and second_input) or (first_input and not second_input)which is the Logical Disjunction of the Logical Conjunction of the Logical Negation of the first input, and the second input, and the Logical Conjunction of the first input and the Logical Negation of the second input. Wow! That’s a lot.
All of the above statements mean the same thing. Exclusive Disjunction is also known as Exclusive OR or XOR. Would “Logical Inequality” be a better name?
test_material_non_implication
RED: make it fail
I add another test to test_binary.py
95 def test_exclusive_disjunction(self):
96 self.assertFalse(src.truth_table.exclusive_disjunction(True, True))
97 self.assertTrue(src.truth_table.exclusive_disjunction(True, False))
98 self.assertTrue(src.truth_table.exclusive_disjunction(False, True))
99 self.assertFalse(src.truth_table.exclusive_disjunction(False, False))
100
101 def test_material_non_implication(self):
102 self.assertFalse(
103 src.truth_table.material_non_implication(True, True)
104 )
105
106
107# Exceptions seen
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'material_non_implication'. Did you mean: 'converse_non_implication'?
GREEN: make it pass
I add material_non_implication to truth_table.py
51def exclusive_disjunction(first_input, second_input):
52 return first_input != second_input
53 return not (first_input == second_input)
54 return (
55 (not first_input and second_input)
56 or
57 (first_input and not second_input)
58 )
59
60
61def material_non_implication(first_input, second_input):
62 return False
the test passes. material_non_implication returns False, if the first input is True and the second input is True
REFACTOR: make it better
I add the second case - when the first input is True and the second input is False to test_material_non_implication in
test_binary.py101 def test_material_non_implication(self): 102 self.assertFalse( 103 src.truth_table.material_non_implication(True, True) 104 ) 105 self.assertTrue( 106 src.truth_table.material_non_implication(True, False) 107 )the terminal shows AssertionError
AssertionError: False is not trueI add an if statement to material_non_implication in
truth_table.py61def material_non_implication(first_input, second_input): 62 if first_input == True: 63 if second_input == False: 64 return True 65 return Falsethe test passes. material_non_implication returns
I add the third case, where the first input is False and the second input is True in
test_binary.py101 def test_material_non_implication(self): 102 self.assertFalse( 103 src.truth_table.material_non_implication(True, True) 104 ) 105 self.assertTrue( 106 src.truth_table.material_non_implication(True, False) 107 ) 108 self.assertFalse( 109 src.truth_table.material_non_implication(False, True) 110 )the test is still green. material_non_implication returns
I add the fourth case, where the first input is False and the second input is False
101 def test_material_non_implication(self): 102 self.assertFalse( 103 src.truth_table.material_non_implication(True, True) 104 ) 105 self.assertTrue( 106 src.truth_table.material_non_implication(True, False) 107 ) 108 self.assertFalse( 109 src.truth_table.material_non_implication(False, True) 110 ) 111 self.assertFalse( 112 src.truth_table.material_non_implication(False, False) 113 ) 114 115 116# Exceptions seenstill green. material_non_implication returns
False, if the first input is False and the second input is False
False, if the first input is False and the second input is True
True, if the first input is True and the second input is False - this is the only case where it returns True
False, if the first input is True and the second input is True
I change the two if statements to one in
truth_table.py61def material_non_implication(first_input, second_input): 62 # if first_input == True: 63 # if second_input == False: 64 if first_input == True and second_input == False: 65 return True 66 return Falsegreen
I add not
61def material_non_implication(first_input, second_input): 62 # if first_input == True and second_input == False: 63 if first_input == True and not second_input == True: 64 return True 65 return Falsestill green
I use bool
61def material_non_implication(first_input, second_input): 62 # if first_input == True and not second_input == True: 63 if bool(first_input) == True and not bool(second_input) == True: 64 return True 65 return Falsethe test is still green
I remove
== True61def material_non_implication(first_input, second_input): 62 # if bool(first_input) == True and not bool(second_input) == True: 63 if bool(first_input) and not bool(second_input): 64 return True 65 return Falsestill green
I remove bool
61def material_non_implication(first_input, second_input): 62 # if bool(first_input) and not bool(second_input): 63 if first_input and not second_input: 64 return True 65 return Falsegreen
I add a conditional expression
61def material_non_implication(first_input, second_input): 62 return True if first_input and not second_input else False 63 if first_input and not second_input: 64 return True 65 return Falsestill green
I make the statement simpler
61def material_non_implication(first_input, second_input): 62 return first_input and not second_input 63 return True if first_input and not second_input else Falsethe test is still green
I remove the other statement in the function
61def material_non_implication(first_input, second_input): 62 return first_input and not second_input
Material NonImplication returns
first_input and not second_inputwhich is the Logical Conjunction of the first input and the Logical Negation of the second inputTrue, only if the first input is True and the second input is False
it is the Logical Negation of Material/Logical Implication which returns False only if the first input is True and the second input is False
Note
return first_input and not second_input returns the Logical Conjunction of the first input and the Logical Negation of the second input. This means that in the 4 cases
if the first input is True and the second input is True, material_non_implication returns
first_input and not second_input True and not True True and False # logical_conjunction(True, False) False # logical_conjunction(True, False)if the first input is True and the second input is False, material_non_implication returns
first_input and not second_input True and not False True and True # logical_conjunction(True, True) True # logical_conjunction(True, True)if the first input is False and the second input is True, material_non_implication returns
first_input and not second_input False and not True False and False # logical_conjunction(False, False) False # logical_conjunction(False, False)if the first input is False and the second input is False, material_non_implication returns
first_input and not second_input False and not False False and True # logical_conjunction(False, True) False # logical_conjunction(False, True)
test_project_first
RED: make it fail
I add a new test in test_binary.py
101 def test_material_non_implication(self):
102 self.assertFalse(
103 src.truth_table.material_non_implication(True, True)
104 )
105 self.assertTrue(
106 src.truth_table.material_non_implication(True, False)
107 )
108 self.assertFalse(
109 src.truth_table.material_non_implication(False, True)
110 )
111 self.assertFalse(
112 src.truth_table.material_non_implication(False, False)
113 )
114
115 def test_project_first(self):
116 self.assertTrue(src.truth_table.project_first(True, True))
117
118
119# Exceptions seen
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'project_first'
GREEN: make it pass
I add a function for project_first in truth_table.py
61def material_non_implication(first_input, second_input):
62 return first_input and not second_input
63
64
65def project_first(first_input, second_input):
66 return True
the test passes. project_first returns True, if the first input is True and the second input is True
REFACTOR: make it better
I add the second case to test_project_first in
test_binary.py115 def test_project_first(self): 116 self.assertTrue(src.truth_table.project_first(True, True)) 117 self.assertTrue(src.truth_table.project_first(True, False))the test is still green. project_first returns
on to the next case
115 def test_project_first(self): 116 self.assertTrue(src.truth_table.project_first(True, True)) 117 self.assertTrue(src.truth_table.project_first(True, False)) 118 self.assertFalse(src.truth_table.project_first(False, True))the terminal shows AssertionError
AssertionError: True is not falseI add if statements for this case to project_first in
truth_table.py65def project_first(first_input, second_input): 66 if first_input == False: 67 if second_input == True: 68 return False 69 return Truethe test passes. project_first returns
I add the last case to test_project_first in
test_binary.py115 def test_project_first(self): 116 self.assertTrue(src.truth_table.project_first(True, True)) 117 self.assertTrue(src.truth_table.project_first(True, False)) 118 self.assertFalse(src.truth_table.project_first(False, True)) 119 self.assertFalse(src.truth_table.project_first(False, False)) 120 121 122# Exceptions seenthe terminal shows AssertionError
AssertionError: True is not falseI add more if statements to project_first in
truth_table.py65def project_first(first_input, second_input): 66 if first_input == False: 67 if second_input == False: 68 return False 69 if second_input == True: 70 return False 71 return Truethe test passes. project_first returns
I add a return statement
65def project_first(first_input, second_input): 66 return first_input 67 if first_input == False: 68 if second_input == False: 69 return False 70 if second_input == True: 71 return False 72 return Truethe test is still green
I remove the other statements
65def project_first(first_input, second_input): 66 return first_input
Project First always returns the first input, like Project Second which always returns the second input
test_converse_implication
RED: make it fail
I add a new test to test_binary.py
115 def test_project_first(self):
116 self.assertTrue(src.truth_table.project_first(True, True))
117 self.assertTrue(src.truth_table.project_first(True, False))
118 self.assertFalse(src.truth_table.project_first(False, True))
119 self.assertFalse(src.truth_table.project_first(False, False))
120
121 def test_converse_implication(self):
122 self.assertTrue(src.truth_table.converse_implication(True, True))
123
124
125# Exceptions seen
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'converse_implication'. Did you mean: 'converse_non_implication'?
GREEN: make it pass
I add a function for converse_implication to truth_table.py
65def project_first(first_input, second_input):
66 return first_input
67
68
69def converse_implication(first_input, second_input):
70 return True
the test passes. converse_implication returns True, if the first input is True and the second input is True
REFACTOR: make it better
I add the second case to test_converse_implication in
test_binary.py121 def test_converse_implication(self): 122 self.assertTrue(src.truth_table.converse_implication(True, True)) 123 self.assertTrue(src.truth_table.converse_implication(True, False))the test is still green. converse_implication returns
time for the next case
121 def test_converse_implication(self): 122 self.assertTrue(src.truth_table.converse_implication(True, True)) 123 self.assertTrue(src.truth_table.converse_implication(True, False)) 124 self.assertFalse(src.truth_table.converse_implication(False, True))the terminal shows AssertionError
AssertionError: True is not falseI add if statements to converse_implication in
truth_table.py69def converse_implication(first_input, second_input): 70 if first_input == False: 71 if second_input == True: 72 return False 73 return Truethe test passes. converse_implication returns
I add the last case to test_converse_implication in
test_binary.py121 def test_converse_implication(self): 122 self.assertTrue(src.truth_table.converse_implication(True, True)) 123 self.assertTrue(src.truth_table.converse_implication(True, False)) 124 self.assertFalse(src.truth_table.converse_implication(False, True)) 125 self.assertTrue(src.truth_table.converse_implication(False, False)) 126 127 128# Exceptions seenthe test is still green. converse_implication returns
I change the two if statements to one in
truth_table.py69def converse_implication(first_input, second_input): 70 # if first_input == False: 71 # if second_input == True: 72 if first_input == False and second_input == True: 73 return False 74 return Truestill green
I use not to write the if statement with True
69def converse_implication(first_input, second_input): 70 # if first_input == False and second_input == True: 71 if not first_input == True and second_input == True: 72 return False 73 return Truegreen
I use bool
69def converse_implication(first_input, second_input): 70 # if not first_input == True and second_input == True: 71 if not bool(first_input) == True and bool(second_input) == True: 72 return False 73 return Truestill green
I remove
== True69def converse_implication(first_input, second_input): 70 # if not bool(first_input) == True and bool(second_input) == True: 71 if not bool(first_input) and bool(second_input): 72 return False 73 return Truethe test is still green
I remove bool
69def converse_implication(first_input, second_input): 70 # if not bool(first_input) and bool(second_input): 71 if not first_input and second_input: 72 return False 73 return Truedo you like green eggs and ham?
I add the opposite of the if statement for the second return statement
69def converse_implication(first_input, second_input): 70 if not first_input and second_input: 71 return False 72 if not (not first_input and second_input): 73 return Truestill green
I move the new if statement to the top
69def converse_implication(first_input, second_input): 70 if not (not first_input and second_input): 71 return True 72 if not first_input and second_input: 73 return Falsegreen
I add a conditional expression
69def converse_implication(first_input, second_input): 70 return True if not (not first_input and second_input) else False 71 if not (not first_input and second_input): 72 return True 73 if not first_input and second_input: 74 return Falsestill green
I remove the other statements and write a simpler conditional expression
69def converse_implication(first_input, second_input): 70 return not (not first_input and second_input) 71 return True if not (not first_input and second_input) else Falsethe test is still green
I remove the commented lines and “multiply not” by the symbols in the parentheses
69def converse_implication(first_input, second_input): 70 return (not not first_input) (not and) (not second_input) 71 return not (not first_input and second_input)the terminal shows SyntaxError
SyntaxError: invalid syntaxI change “not and” to “or” to be correct
69def converse_implication(first_input, second_input): 70 return (not not first_input) or (not second_input) 71 return not (not first_input and second_input)back to green
I remove the other return statement then remove
not notbecause they cancel out - the negation of the negation of something is something69def converse_implication(first_input, second_input): 70 return first_input or not second_input 71 return (not not first_input) or (not second_input)the test is still green
I remove the other return statement
69def converse_implication(first_input, second_input): 70 return first_input or not second_input
Converse Implication returns
first_input or not second_inputFalse, only if the first input is False and the second input is True
the Logical Disjunction of the first input and the Logical Negation of the second input
It is the opposite of Converse NonImplication which always returns
not first_input and second_inputor True, only if the first input is False and the second input is True
more about Exclusive Disjunction
return (not first_input and second_input) or (first_input and not second_input) can be thought of as
return x or y
if
xisnot first_input and second_inputif
yisfirst_input and not second_input
Exclusive Disjunction can also be thought of as
return (
converse_non_implication(first_input, second_input)
or
material_non_implication(first_input, second_input)
)
because
converse_non_implication returns
not first_input and second_inputmaterial_non_implication returns
first_input and not second_input
This means that in the 4 cases
if the first input is True and the second input is True, exclusive_disjunction returns
(not first_input and second_input) or (first_input and not second_input) (not True and True ) or (True and not True ) (False and True ) or (True and False ) False or False False # logical_disjunction(False, False)if the first input is True and the second input is False, exclusive_disjunction returns
(not first_input and second_input) or (first_input and not second_input) (not True and False ) or (True and not False ) (False and False ) or (True and True ) False or True True # logical_disjunction(False, True)if the first input is False and the second input is True, exclusive_disjunction returns
(not first_input and second_input) or (first_input and not second_input) (not False and True ) or (False and not True ) (True and True ) or (False and False ) True or False True # logical_disjunction(True, False)if the first input is False and the second input is False, exclusive_disjunction returns
(not first_input and second_input) or (first_input and not second_input) (not False and False ) or (False and not False ) (True and False ) or (False and True ) False or False False # logical_disjunction(False, False)
close the project
I close
test_binary.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
Binary Operations take 2 inputs, each input can be True or False, if I name the first input first_input and the second one second_input, the tests show that
-
returns
first_input or not second_inputreturns False if
first_inputis False andsecond_inputis Trueis the opposite (Logical Negation) of Converse NonImplication which returns True only if
first_inputis False andsecond_inputis True
-
always returns
first_inputis the opposite (Logical Negation) of Negate First which returns True only if
first_inputis False
-
returns
first_input and not second_inputreturns True only if
first_inputis True andsecond_inputis Falseis the opposite (Logical Negation) of Material/Logical Implication which returns False only if
first_inputis True andsecond_inputis False
-
returns
first_input != second_inputreturns True only if
first_inputandsecond_inputare NOT equalis the opposite (Logical Negation) of Logical Equality which returns True only if
first_inputandsecond_inputare equal
-
returns
first_input or second_inputreturns False only if
first_inputis False andsecond_inputis Falseis the opposite (Logical Negation) of Logical NOR which returns True only if
first_inputis False andsecond_inputis False
-
always returns True
never returns False
is the opposite (Logical Negation) of contradiction which always returns False
-
returns
not (first_input and second_input)returns False only if
first_inputis True andsecond_inputis Trueis the opposite (Logical Negation) (not) of Logical Conjunction (and) which returns True only if
first_inputis True andsecond_inputis True
-
always returns
not first_inputis the opposite (Logical Negation) of Project First which returns True only if
first_inputis True
-
returns
not first_input and second_inputreturns True only if
first_inputis False andsecond_inputis Trueis the opposite (Logical Negation) of Converse Implication which returns False if
first_inputis False andsecond_inputis True
-
always returns
second_inputis the opposite (Logical Negation) of Negate Second which returns True only if
second_inputis False
Logical Conjunction returns
returns
first_input and second_inputreturns True only if
first_inputis True andsecond_inputis Trueis the opposite (Logical Negation) of Logical NAND which returns False only if
first_inputis True andsecond_inputis True
-
always returns False
never returns True
is the opposite (Logical Negation) of Tautology which always returns True
and
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?
what is next?
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