AND | True False
-------------------
True | true false
False | false false
OR | True False
-------------------
True | true true
False | true false
XOR | True False
-------------------
True | false true
False | true false
AND is Logical Conjunction: https://en.wikipedia.org/wiki/Truth_table#Logical_conjunction_.28AND.29
OR is Logical Disjunction: https://en.wikipedia.org/wiki/Truth_table#Logical_disjunction_.28OR.29
XOR is Exclusive Disjunction: https://en.wikipedia.org/wiki/Truth_table#Exclusive_disjunction
https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not
In [5]:
cond1 = True
def func1(): print("Hi I'm func1")
Remember the general form:
if <cond1>:
func1()
In [6]:
(cond1 and func1())
In [7]:
cond1 = False
In [8]:
(cond1 and func1())
Out[8]:
In [9]:
cond1 = True
def func1(): return True
In [10]:
(True and func1())
Out[10]:
In [11]:
x = 1
cond1 = (3 > x)
In [12]:
cond1
Out[12]:
In [13]:
(cond1 and func1())
Out[13]:
In [14]:
((3 > x) and func1())
Out[14]:
In [15]:
((3 < x) and func1())
Out[15]:
In [16]:
x = 5
cond2 = (3 > x)
In [17]:
cond2
Out[17]:
In [18]:
func1()
Out[18]:
In [19]:
((cond1) and func1())
Out[19]:
In [20]:
((cond2) and func1())
Out[20]:
In [21]:
x = 5
cond1 = (3 > x)
In [22]:
(True and func1())
Out[22]:
In [23]:
cond1 = True
cond2 = False
def func1(): print("Hi im func1")
def func2(): print("Hi im func2")
In [24]:
((cond1 and func1()) or (cond2 and func2()))
Out[24]:
In [25]:
ret = func1()
In [26]:
type(ret)
Out[26]:
In [28]:
cond1 = False
cond2 = False
def func1(): print("Hi im func1")
def func2(): print("Hi im func2")
def func3(): print("Hi im func3")
In [29]:
((cond1 and func1()) or (cond2 and func2()) or func3())
Map/filter/reduce is generally cagegorized as belonging to a functional style for the reasons we discussed in class. They pass functions around to do their work and they perform transformations on data sets.
https://en.wikipedia.org/wiki/MapReduce
In some programming environments that offer immutable data structures map/filter/reduce operate fully functionally, returning entirerly new data sets/structures from their operations. This gives rise to embarassingly parallel map/filter/reduce agorithms.
In [2]:
my_list = list(range(1000))
In [3]:
print(*my_list)
In [32]:
def multiply_by_two(x):
return x * 2
In [33]:
my_doubled_list = map(multiply_by_two, my_list)
In [34]:
my_doubled_list
Out[34]:
In [35]:
print(*my_doubled_list)
In [6]:
my_doubled_list = map(lambda x: x*2, my_list)
In [7]:
my_doubled_list = [i * 2 for i in my_list]
In [8]:
print(*my_doubled_list)
In [40]:
def my_filter(x):
return x > 900
In [41]:
my_filtered_list = filter(my_filter, my_list)
In [42]:
print(*my_filtered_list)
In [43]:
my_doubled_list = map(lambda x: x*2, filter(my_filter, my_list))
In [44]:
print(*my_doubled_list)
In [56]:
my_filtered_list = filter(my_filter, my_list)
In [57]:
type(my_filtered_list)
Out[57]:
print(*my_filtered_list)
In [58]:
my_doubled_list = map(lambda x: x*2, my_filtered_list)
In [59]:
print(*my_doubled_list)
In [60]:
from functools import reduce
In [61]:
my_list = list(range(100))
In [79]:
print(*my_list)
In [80]:
sum(my_list)
Out[80]:
In [63]:
min(my_list)
Out[63]:
In [64]:
max(my_list)
Out[64]:
In [65]:
my_sum = reduce(lambda x, y: x+y, my_list)
In [66]:
my_sum
Out[66]:
There are several multiple dispatch libraries for python. I got confused as to which I had installed into which virtualenv during class. Sorry about that.
In [23]:
import random
# https://github.com/mrocklin/multipledispatch/
from multipledispatch import dispatch
class Thing(object):
pass
class Paper(Thing):
pass
class Scissors(Thing):
pass
class Rock(Thing):
pass
In [2]:
options = [Paper, Scissors, Rock]
In [3]:
player1 = random.choice(options)
In [4]:
player2 = random.choice(options)
In [5]:
player1
Out[5]:
In [6]:
player2
Out[6]:
In [7]:
def draw(x, y):
if isinstance(x, Rock):
if isinstance(y, Rock):
return None # No winner
if isinstance(y, Paper):
return y
if isinstance(y, Scissors):
return x
else:
raise TypeError("Unknown type involved")
elif isinstance(x, Paper):
if isinstance(y, Rock):
return x
if isinstance(y, Paper):
return None # No winner
if isinstance(y, Scissors):
return y
else:
raise TypeError("Unknown type involved")
elif isinstance(x, Scissors):
""" This method left as a exercise for the reader. """
pass
In [10]:
draw(player1(), player2())
Out[10]:
In [21]:
@dispatch(Rock, Rock)
def draw(x, y):
return None
@dispatch(Paper, Scissors)
def draw(x, y):
return(y)
# This is by no means all of the combinations. Here again they're left as a exercise for the reader.
In [15]:
winner = draw(player1(), player2())
In [16]:
(isinstance(winner, player1) and print("Player1 won... bam!"))
Out[16]:
In [17]:
(isinstance(winner, player2) and print("Player2 won... bowzza!"))