1Defining variables
Run this cell. You can swap in your own English sentences for P, Q and S: the code stays the same, because the connectives don’t care what the sentences say.
Logical connectives as code: evaluate sentences, build truth tables, and test two expressions for logical equivalence.
Prototype for CS50. A Python version of the CS350 Lesson 3 workbook “Truth in R”.
By the end of this workbook you should be able to write any sentence from the reading in Python, and say which connectives Python hands you and which ones you have to build yourself.
The names in brackets are the everyday words for each connective: disjunction is OR, conjunction is AND, negation is NOT, and the conditional is IF–THEN.
In Python, = assigns a value and == compares two values. Keep them apart.
Run every cell. Where a cell asks you to predict first, write the prediction down before you run it.
Run this cell. You can swap in your own English sentences for P, Q and S: the code stays the same, because the connectives don’t care what the sentences say.
How do you form a disjunction, an OR (∨), in Python? Write the disjunction of P and Q, then run it.
or.How do you form a conjunction, an AND (∧), in Python? Write the conjunction of P and Q, then run it.
and.How do you negate a sentence, NOT (¬), in Python? Negate Q, then run it.
not, written in front of the sentence.Precedence is the order in which Python applies the operators in an expression when there are no parentheses to say. Parentheses override it: whatever is inside them is worked out first.
Before you run anything: with P = True and Q = False, what do you expect not P or Q to be? What about not (P or Q)? Write both predictions down, then run the cell.
Both False? That’s a coincidence of these values, and it hides the point.
Add Q = True as the first line of this cell and run it again: now the two answers differ.
Then run cell 1 again to put Q back to False.
If you predicted the same value for both, you’ve just found a mistake that would otherwise surface later, in a sentence where it changes the answer.
Did you put Q back to False? Cell 5 set Q = True. Run cell 1 again before going on, or the answers from here on will differ from your classmates’.
Python gives you AND (and), OR (or) and NOT (not), but there is no operator for the conditional, the IF–THEN sentence (→). Build it. Recall when P → Q is false, and write an expression using only the connectives you have.
Watch out: P <= Q appears to work, because Python treats True and False as 1 and 0. That’s arithmetic, not logic, and it will mislead anyone who adopts it. Use only and, or and not.
So far every cell has evaluated one row of a truth table: the row where P, Q and S happen to have the values you assigned. A truth table shows every row.
The code below is a worked example. It builds the full truth table for ¬(P ∨ Q), written not (P or Q) in Python. Run it first, and make sure you understand what each line does. (Inside the table, lowercase p and q stand for each row’s values, so the P and Q from cell 1 stay as they are.)
Then build your own, below the example, for the three-variable sentence ¬(P ∨ Q) ∧ S, written not (P or Q) and S. Use the example as your model: make a list called tt3 whose rows also include s, add the sentence as the last value in each row, and print it. Keep the example’s lines as they are: cell 8 uses tt.
With the values from cell 1 the single-row answer is False, but the table tells you why, and in which of the eight rows it is ever True.
repeat=3 in product, a third loop variable s, and the sentence not (p or q) and s as the last value in each row.== actually does predict firstTwo expressions, every possible input. Are they logically equivalent?
Run these.
P != Q is exclusive or: Python has no xor word, but two True/False values differ exactly when one of them is True.
Everyday English “or” is ambiguous between inclusive (one, the other, or both) and exclusive (one or the other, not both).
or is always inclusive.