





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A lecture plan for a Python course focused on artificial intelligence. It covers topics such as list comprehensions, iterators, generators, imports, functions, classes, inheritance, magic methods, and profiling. It also includes code examples and explanations for each topic.
What you will learn
Typology: Study notes
1 / 37
This page cannot be seen from the preview
Don't miss anything!
[x for x in lst1 if x > 2 for y in lst2 for z in lst if x + y + z < 8] res = [] # translation for x in lst1: if x > 2: for y in lst2: for z in lst3: if x + y + z > 8: res.append(x)
k = [1,2,3] i = iter(k) # could also use k.iter() i.next() 1 i.next() 2 i.next() 3 i.next() StopIteration
import math math.sqrt(9)
from math import * sqrt(9) # unclear where function defined #hw1.py def concatenate(seqs): return [seq for seq in seqs] # This is wrong
with hw1.py
import hw assert hw1.concatenate([[1, 2], [3, 4 ]]) == [1, 2, 3, 4 ] #AssertionError reload(hw1) #after fixing hw
def myfun(b, c=3, d=“hello”): return b + c myfun(5,3,”bob”) 8 myfun(5,3) 8 myfun(5) 8
def print_everything(*args):
for count, thing in enumerate(args): print '{0}. {1}'.format(count, thing)
lst = ['a', ’b', 'c’] print_everything(*lst)
a
b
c
print_everything('a', ’b', 'c')
def print_keyword_args(**kwargs):
for key, value in kwargs.iteritems(): #.items() is list print "%s = %s" % (key, value)
kwargs = {'first_name': 'Bobby', 'last_name': 'Smith'} print_keyword_args(**kwargs) first_name = John last_name = Doe print_keyword_args(first_name="John”, last_name="Doe")
foo(arg1, *args, **kwargs) # one mandatory argument
def fib(n, fibs={}): if n in fibs: return fibs[n] if n <= 1: fibs[n] = n else: fibs[n] = fib(n-1) + fib(n-2) return fibs[n]
**def compose(f, g, x): return f(g(x))
compose(str, sum, [1,2,3]) '6'**
from operator import itemgetter def calc_ngram(inputstring, nlen): ngram_list = [inputstring[x:x+nlen] for x in
xrange(len(inputstring)-nlen+1)] ngram_freq = {} # dict for storing results for n in ngram_list: # collect the distinct n-grams and count if n in ngram_freq: ngram_freq[n] += 1 else: ngram_freq[n] = 1 # human counting numbers start at 1
(ascending/descending) return sorted(ngram_freq.iteritems(),
key=itemgetter(1), reverse=True) http://times.jayliew.com/2010/05/20/a simple n gram calculator pyngram/