oct 9
- I've added documentation links to the software page.
- continue discussion about logic and loops
Here are some logic operations :
a and b True only when both are True
a or b False only when both are False
not a Flip True/False value of 'a'
a => b implies
a <=> b if and only if
a xor b exclusive or
And here's a 'truth table' that shows explicitly what they all do.
a b (a and b) (a or b) (a => b) (a <=> b) (a xor b)
..............................................................
T T T T T T F
T F F T F F T
F T F T T F T
F F F F T T F
These identities can all be verified by checking all the
possibilities explicitly with a truth table.
( a => b ) <=> (not a or b)
( a and b ) <=> not (not a or not b)
( a or b ) <=> not (not a and not b)
( not not a ) <=> a
( a or not a ) <=> True
a and (b and c) <=> (a and b) and c
a or (b or c) <=> (a or b) or c
a and (b or c) <=> (a and b) or (a and c)
a or (b and c) <=> (a or b) and (a or c)
a => b <=> not b => not a
There are also statements called 'syllogisms', which are always True.
For example,
((a => b) and (b => c)) => (a => c)
which in English says
If a implies b, and b implies c, then a implies c.
OK, so let's do a lewis carroll logic puzzle this way.
(1) Every one who is sane can do Logic;
(2) No lunatics are fit to serve on a jury;
(3) None of your sons can do Logic.
What conclusion can be drawn?
Use a = able to do Logic
b = fit to serve on a jury
c = sane
d = your sons
Express each line as a boolean expression.
Use the rules of boolean algebra to draw a short conclusion.
Then we have
c => a sane => does logic
not c => not b not sane => not fit to be on jury
d => not a your son => can't do logic
The first is eqivalent to ( not a => not c ).
Then stringing them together,
d => not a => not c => not b
which in English says
Your sons are not fit to serve on a jury.
'if', 'while', and all that
So what does this do ?
for x in range(20):
if (x < 10) and (x > 5):
print "x is between 5 and 10"
else:
print "."
How about this ?
numbers = [1,2,10,20,50,80]
while numbers:
x = numbers.pop(0)
print x