# Conditionals and Biconditionals

### From Devipedia

p => q can be expressed in Python as:

#!/usr/bin/python # a => b or said in English, "a implies b" def implies( a,b): if a: return b else: return True

(Source of implies function and good reading for students of both Python and discrete mathematics: http://www.sm.luth.se/~tomas/MAM200-Python1.pdf )

The function "implies", along with or, not and and allows us to express seven important pairs of equivalent propositional forms in Python.

The expressions tuple in the code below are my port of theorem 1.2 from the book "A Transition to Advanced Mathematics" (sixth edition) by Douglas Smith, Maurice Eggen, and Richard St. Andre.

#!/usr/bin/python def implies( a,b): if a: return b else: return True expressions = ( "implies(p,q) == ((not p) or q)", "(p == q) == (implies(p,q) and implies(q,p))", "not(p and q) == ((~p) or (~q))", "(not(p or q)) == ((not p) and (not q))", "(not(implies(p,q))) == (p and (not q))", "not(p and q) == implies(p, not q)", "(p and (q or r)) == ((p and q) or (p and r))", "(p or (q and r)) == ((p or q) and (p or r))" ) def test_forms(p,q,r): print "\nAre forms equal for p=%s, q=%s, r=%s?" % (p,q,r) for exp in expressions: print "%s: %s" % (eval(exp), exp) test_forms(True, True, True) test_forms(False, True, True) test_forms(True, False, True) test_forms(False, False, True) test_forms(True, True, False) test_forms(False, True, False) test_forms(True, False, False) test_forms(False, False, False)

Output from the program:

$ python implies.py Are forms equal for p=True, q=True, r=True? True: implies(p,q) == ((not p) or q) True: (p == q) == (implies(p,q) and implies(q,p)) True: not(p and q) == ((~p) or (~q)) True: (not(p or q)) == ((not p) and (not q)) True: (not(implies(p,q))) == (p and (not q)) True: not(p and q) == implies(p, not q) True: (p and (q or r)) == ((p and q) or (p and r)) True: (p or (q and r)) == ((p or q) and (p or r)) Are forms equal for p=False, q=True, r=True? True: implies(p,q) == ((not p) or q) True: (p == q) == (implies(p,q) and implies(q,p)) True: not(p and q) == ((~p) or (~q)) True: (not(p or q)) == ((not p) and (not q)) True: (not(implies(p,q))) == (p and (not q)) True: not(p and q) == implies(p, not q) True: (p and (q or r)) == ((p and q) or (p and r)) True: (p or (q and r)) == ((p or q) and (p or r)) Are forms equal for p=True, q=False, r=True? True: implies(p,q) == ((not p) or q) True: (p == q) == (implies(p,q) and implies(q,p)) True: not(p and q) == ((~p) or (~q)) True: (not(p or q)) == ((not p) and (not q)) True: (not(implies(p,q))) == (p and (not q)) True: not(p and q) == implies(p, not q) True: (p and (q or r)) == ((p and q) or (p and r)) True: (p or (q and r)) == ((p or q) and (p or r)) Are forms equal for p=False, q=False, r=True? True: implies(p,q) == ((not p) or q) True: (p == q) == (implies(p,q) and implies(q,p)) True: not(p and q) == ((~p) or (~q)) True: (not(p or q)) == ((not p) and (not q)) True: (not(implies(p,q))) == (p and (not q)) True: not(p and q) == implies(p, not q) True: (p and (q or r)) == ((p and q) or (p and r)) True: (p or (q and r)) == ((p or q) and (p or r)) Are forms equal for p=True, q=True, r=False? True: implies(p,q) == ((not p) or q) True: (p == q) == (implies(p,q) and implies(q,p)) True: not(p and q) == ((~p) or (~q)) True: (not(p or q)) == ((not p) and (not q)) True: (not(implies(p,q))) == (p and (not q)) True: not(p and q) == implies(p, not q) True: (p and (q or r)) == ((p and q) or (p and r)) True: (p or (q and r)) == ((p or q) and (p or r)) Are forms equal for p=False, q=True, r=False? True: implies(p,q) == ((not p) or q) True: (p == q) == (implies(p,q) and implies(q,p)) True: not(p and q) == ((~p) or (~q)) True: (not(p or q)) == ((not p) and (not q)) True: (not(implies(p,q))) == (p and (not q)) True: not(p and q) == implies(p, not q) True: (p and (q or r)) == ((p and q) or (p and r)) True: (p or (q and r)) == ((p or q) and (p or r)) Are forms equal for p=True, q=False, r=False? True: implies(p,q) == ((not p) or q) True: (p == q) == (implies(p,q) and implies(q,p)) True: not(p and q) == ((~p) or (~q)) True: (not(p or q)) == ((not p) and (not q)) True: (not(implies(p,q))) == (p and (not q)) True: not(p and q) == implies(p, not q) True: (p and (q or r)) == ((p and q) or (p and r)) True: (p or (q and r)) == ((p or q) and (p or r)) Are forms equal for p=False, q=False, r=False? True: implies(p,q) == ((not p) or q) True: (p == q) == (implies(p,q) and implies(q,p)) True: not(p and q) == ((~p) or (~q)) True: (not(p or q)) == ((not p) and (not q)) True: (not(implies(p,q))) == (p and (not q)) True: not(p and q) == implies(p, not q) True: (p and (q or r)) == ((p and q) or (p and r)) True: (p or (q and r)) == ((p or q) and (p or r))