
#lec11.py

2+4

6

(2+5) * (3-5)

-14

#Float Expressions:

2.7 + 4.5

(2 + 3.5) * (2.7 -4)

(2 + 3.5) * (2.7 - 4)

#Complex Expressions: (!)

2 + 3j

-5.2 + 6j

(2 + 3j) * (-5.2 + 6j)

#String Expressions:

"this "
"this is " + "cool"





str(50)

str(92.7)

int("22")

int("abc")

int(3.9)

float(22)

float("3.14")

# assignment statements

x = 2

y = x + 4

s = "abc"

t = "this" + s * y



# sequence types
s

len(s)

t

len(t)

#we can do access the i'th element of a sequence...

s[0]

s[1]

(s[0],s[1],s[2])

t[6]

#slicing:  s[i:j] is the (sub)sequence of characters from position i through j-1.

t

t[0:4]

t[5:7]

t[3:] 

t[:4] 


#forall t. t[:i] + t[i:] is always equal to t

t[:2] + t[2:]

t[:6] + t[6:]


#Tuples:

t1 = (3,5)

t2 = ("abc",2.9,99)

t3 = (t1,t2)


(1,2) + (3.6, "a")

t4 = ("abc",10) * 4

# tuples are also sequences

t1

len(t1)

t3

len(t3)

t4

len(t4)


#Can select:

t1[0]

t2[2]

t2[1:2]

t2[1:]


#Another sequence Lists:


l1 = [9,1,1]

l2 = [1,"a",3.0,["moo","koo"]]


len(l1)

len(l2)


l1[0]

l2[1]

l2[3]

l2[3][0]


l2[1:3]


l1 + l2

l1 * 3

2 * l2

# mutation

l2

l2[0] = "zzz"

l2


x = [1,2,3,4,5]

y = x

y[0] = "a"

y

x

# slice assignment

l2[0:3] = l1 * 7

# negative wraparound

l2[-1]



#Statements:

#if

x = 10
if x > 0:
    x = abs(x)
    print "Negative, flipped", x	
elif x == 0:
    print "Zero!"
else:
    print "Positive", x


#indentation matters
if x==15: 
	pass
	print "is 15"
	if y == 10:

if x==15:
	pass
print "is 15"


if 1: print "true"

if 0: print "true"

if []: print "true"

if ["a"]: print "true"

#while - statement
a,b = 0,1
while b < 20:
	print b
	a,b = b,a+b
print b


#functions
def fib(n):
	a,b = 0,1
	while (b < n):
		print b
		a,b = b,a+b

return b




fib(20)


print fib(20)


def fac(n):
	if n == 0: 
		return 1
	else:
		return n * fac(n-1)


fac(10)


def fac(n):
	if n < 0: 
		return "Rubbish negative arg"
	elif n == 0: 
		return 1
	else:
		return n * fac(n-1)


fac(-10)

fac(10)

#"default" parameters.

def greet(i,name="Bob"):
	print "Hi "*i, name



greet(3, "Sally")


greet(3)


fac


def compose(f,g):
 	def foo(x):
	    return f(g(x))
 	return foo


def f1(x): return 3*x

def f2(x): return x[0:2]

baz = compose(f1,f2)

baz([1,2.2,"three",4,"five"]) #works for sequences

baz("thank god its friday") #works for sequences





###################################################################

def changeable(x):
        return x + y


#####################################################################


z = [1,2,3,4,5,6,7,8,9,10]
z[3:6] = ["a","b","c"]
z 
z[3:6] = ["a", "b"] * 2 
z
z[4:]=[]
z
z[:0] = ["al", "be"]
z



"a" in "cat" 
"a" in "entebbe"
"a" in ("c" , "a", "t") 
2 in [1,2,3,4,5]
2 in [1,4,"92",2.4]




for x in ["how", "many", "cannonballs"]:
  print x,len(x) 


for c in "chimichanga":
  print c*3 





s=0
z=(1,2,3,4.0,"5")    #tuple
for i in z:
  s = s + i


s=0
for i in z:
  s=s+float(i)
s







mexmenu = [("burrito",3.50),("taco",1.90),("sopa",2.50),("quesadilla",3.00),("frijole",0.75)] 

for i,p in mexmenu:
  print "One",i,"costs",p

range(10)

for i in range(10):print i

range(5,15)

range(15,5,-1)

def dup1(s,k):
   for x in s:
     print x
     s[len(s):] = x*k


def dup2(s,k):
   for x in s:
     print x
     s = s + x*k
return s

def dup3(s,k):
  for x in s[:]:
     print x
     s = s + x*k
return s





def dup(x):
  return 2*x


z = range(10)
map(dup,z)

map(dup,"chimichanga")

def even(x): return int(x)%2==0
filter(even,range(10))
filter(even,"1234096001234125")
filter(even,(1,2.0,3.2,4))

def add(x,y): x+y

reduce(add,range(10),0)

def fac(x): 
  def mul(x,y): return x*y
  return reduce(prod,range(x+1),1)

fac(5)

>>> [ x*x for x in range(10)]
[0,1,4,9,16,25,36,49,64,81]
>>> [2*x for x in "avra kedavra"]
["aa","vv","rr","aa","  ","kk","aa"]







[ x*x for x in range(10) if even(x)]
[ 2*x for x in "0123456" if even(x)]
[z[0] for z in mexmenu if z[1]<3.0]


[(x,y) for x in range(3) for y range(3)]

[(x,y) for x in range(3) for y in range(#) if x > y]



def sort(L):
  if L==[]: return L
  else:
    l=sort([x for x in s[1:] if x < s[0])
    r=sort([x for x in s[1:] if x >= s[0])
    return(l+L[0:1]]+r)


sort([1,44,12,31,34])

d={}
d=dict(mexmenu)
d["ceviche"] = 3.95
d
d["burrito"]
d.keys()

d.values()

def freq(s):
  d={}
  for c in s:
    if c in d: d[c]+=1
    else: d[c]=1
  return d

def plotfreq(s):
  d=freq(s)
  for k in d.keys():
    print k, "*"*d[k]
  return d
		
plotfreq([1,1,3.0,"A",3.0,"A","A",1,2,3.0,1,"A"])
d=plotfreq("avrakedavra")
d.keys()

