deflyrics_to_frequencies(lyrics):
myDict ={}for words in lyrics:if words in myDict:
myDict[words]+=1else:
myDict[words]=1return myDict
# Find the biggest value numbers in a dictionary
animals ={'a':'aaa','b':'bbb','c':'ccc','d':['ddd','dddd']}
result =None
biggestvalue =0for i in animals:iflen(animals[i])>= biggestvalue ## here! the expression
result = i
return result
Fibonacci with a dictionary
deffib_efficient(n,d):if n in d:return d[n]else:
ans = fib_efficient(n-1, d)+fib_efficient(n-2, d)
d[n]= ans
return ans
# do a lookup first in case already calculated the value# modify dictionary as progress through function calls
fib_efficient(20,{1:1,2:2})
Out[7]:10946
Global Variables
deffib(n);global numFibCalls # Here is something that I # can access out of the scope