Sunday, August 26, 2012

Exercises 22-26: Consolidation

Exercises 22-24 basically just go over everything we've already done. I couldn't see anything new in there so I just skim read through them... Unless you are feeling very confident with everything we have done so far, I would recommend actually doing them.

ex25.py:
1:    
2:    
3:    
4:  # if it weren't for emacs' tab auto-indent I would have forgotten the colon  
5:  # on every one of these defs  
6:    
7:  def line2words(line):  
8:    words = line.split(' ')  
9:    return words  
10:    
11:  # sorted() takes an array and sorts  
12:    
13:  def print_head(words):  
14:    print words.pop(0)  
15:    
16:  def print_arse(words):  
17:    print words.pop(-1)  
18:    
19:  def sort_line(line):  
20:    return sorted(line2words(line))  
21:    
22:  def print_first_n_last(line):  
23:    ws = line2words(line)  
24:    print_head(ws)  
25:    print_arse(ws)  
26:    
27:  def print_top_n_bot(line):  
28:    ws = sort_line(line)  
29:    print_head(ws)  
30:    print_arse(ws)  
31:    

Testing ex25 with the Python interpreter:
1:  slithgow@holly:~/python$python  
2:  Python 2.6.4 (r264:75706, Mar 24 2010, 10:35:01)  
3:  [GCC 4.0.2] on sunos5  
4:  Type "help", "copyright", "credits" or "license" for more information.  
5:  >>> import ex25  
6:  >>> l1 = "All good things come to those who wait."  
7:  >>> words = ex25.line2words(sentence)  
8:  Traceback (most recent call last):  
9:   File "<stdin>", line 1, in <module>  
10:  NameError: name 'sentence' is not defined  
11:  >>> words = ex25.line2words(l1)  
12:  >>> ex25.print_head(words)  
13:  All  
14:  >>> ex25.print_top_n_bot(l1)  
15:  All  
16:  who  
17:  >>> ex25.print_first_n_last(l1)  
18:  All  
19:  wait.  
20:  >>>  

Oooh.... ex26 is a debugging task/test.... excellent. This really is a very good tutorial. Possibly approaching LYAHFGG (http://learnyouahaskell.com/) in quality. Which is pretty much the highest compliment I can give to any other tutorial.

The best way to do the test is to just copy-paste the file (http://learnpythonthehardway.org/exercise26.txt) and try to run it... we'll get errors - errors that have been deliberately selected as ones we're likely to make (even once we know what we're doing).

1:File "ex26.py", line 18  
2:    print word  
3:      ^  
4:  SyntaxError: invalid syntax
Note that not only does it give you the line - it also tells you where the error is. If the python interpreter is like most programming languages then it is extremely valuable to learn when it does this accurately and when it does not (hint: this is one of those times). Becoming familiar with a compiler/interpreter is key to debugging. And debugging is almost certainly what we'll be spending most of our time on.

First pass through the file, you'll get all the syntax errors. It can't even parse the file if for example, brackets don't match up. Then it will try to run the file, and there should be more errors - the kind it runs into when its running aka runtime errors. The first load will be ones that make little sense - they were basically typos that didnt break it enough to stop it from parsring. Then finally, you should encounter logic errors. These are the ones that you can't blame on a tiny laptop keyboard. I've never met anyone who doesn't make them sometimes (and I've met some freaking geniuses) so chill about them but they do tend to indicate a lack of understanding of what you want to be doing. Each time you get an error, you could search through the code for other instances of this (via the editor's search feature, never manually); sometimes this is wise (like a typo in a variable name that you think you might have made a few times), but usually its a good idea to fix one error, then try to recompile.

This is what I ended up with (let me know if I missed any!):
1:  def break_words(stuff):  
2:    """This function will break up words for us."""  
3:    words = stuff.split(' ')  
4:    return words  
5:    
6:  def sort_words(words):  
7:    """Sorts the words."""  
8:    return sorted(words)  
9:    
10:  def print_first_word(words):  
11:    """Prints the first word after popping it off."""  
12:    word = words.pop(0)  
13:    print word  
14:    
15:  def print_last_word(words):  
16:    """Prints the last word after popping it off."""  
17:    word = words.pop(-1)  
18:    print word  
19:    
20:  def sort_sentence(sentence):  
21:    """Takes in a full sentence and returns the sorted words."""  
22:    words = break_words(sentence)  
23:    return sort_words(words)  
24:    
25:  def print_first_and_last(sentence):  
26:    """Prints the first and last words of the sentence."""  
27:    words = break_words(sentence)  
28:    print_first_word(words)  
29:    print_last_word(words)  
30:    
31:  def print_first_and_last_sorted(sentence):  
32:    """Sorts the words then prints the first and last one."""  
33:    words = sort_sentence(sentence)  
34:    print_first_word(words)  
35:    print_last_word(words)  
36:    
37:    
38:  print "Let's practice everything."  
39:  print 'You\'d need to know \'bout \\s that do \n \\ns and \t \\ts.'  
40:    
41:  poem = """  
42:  \tThe lovely world  
43:  with logic so firmly planted  
44:  cannot discern \n the needs of love  
45:  nor comprehend passion from intuition  
46:  and requires an explantion  
47:  \n\t\twhere there is none.  
48:  """  
49:    
50:    
51:  print "--------------"  
52:  print poem  
53:  print "--------------"  
54:    
55:  five = 10 - 2 + 3 - 5 - 1  
56:  print "This should be five: %s" % five  
57:    
58:  def secret_formula(started):  
59:    jelly_beans = started * 500  
60:    jars = jelly_beans / 1000  
61:    crates = jars / 100  
62:    return jelly_beans, jars, crates  
63:    
64:    
65:  start_point = 10000  
66:  beans, jars, crates = secret_formula(start_point)  
67:    
68:  print "With a starting point of: %d" % start_point  
69:  print "We'd have %d jellybeans, %d jars, & %d crates." % (beans, jars, crates)  
70:    
71:  start_point = start_point / 10  
72:    
73:  print "We can also do that this way:"  
74:  print "We'd have %d beans, %d jars, & %d crates" % secret_formula(start_point)  
75:    
76:  sentence = "All good\tthings come to those who wait."  
77:  # finding typos in strings is sometimes important to change, sometimes not.  
78:  # I cbf checking the poem for example, everywhere else I'll check  
79:    
80:  words = break_words(sentence)  
81:  sorted_words = sort_words(words)  
82:    
83:  print_first_word(words)  
84:  print_last_word(words)  
85:  print_first_word(sorted_words)  
86:  print_last_word(sorted_words)  
87:  sorted_words = sort_sentence(sentence)  
88:  print "%s" % ' '.join(sorted_words) # <- this arguably isnt an error.  
89:    # usually though, keep internal representations  
90:    # away from end users, they'll typically only hurt themselves.  
91:    
92:  print_first_and_last(sentence)  
93:  print_first_and_last_sorted(sentence)  
94:    



Exercises 14-21: Files and subroutines

ex14.py:
1:    
2:  from sys import argv  
3:    
4:  script, uname, pword = argv  
5:  prompt = "My answer is "  
6:    
7:  if pword == "password":  
8:    print "%s@%s" % (uname, script)  
9:    print "Do you like me %s?" % uname  
10:    likes = raw_input(prompt)  
11:    
12:    print "Where you live %s?" % uname  
13:    lives = raw_input(prompt)  
14:    
15:    # copy-pasting the above down here, I left uname in -  
16:    # caused a runtime error - python ran until the statement itself, it  
17:    # didn't do any pre-checking  
18:    print "What kinda comp you got?" #% uname  
19:    comp = raw_input(prompt)  
20:    
21:    print """  
22:    Alright, so you said %r about liking me.  
23:    You live in %r. Not sure where that is.  
24:    And you have a %r computer. Nice.  
25:    """ % (likes, lives, comp)  
26:    
27:  else:  
28:    print "Incorrect password!"  
29:    
ex15.py:
1:    
2:    
3:  from sys import argv  
4:    
5:  #script, filename = argv  
6:    
7:  # opens a file and stores it as a file object  
8:  #txt = open(filename)  
9:    
10:  # read is a method of the file object which returns a string containg the file  
11:  #print "H %r:" % filename  
12:  #print txt.read()  
13:    
14:  # collecting input and making a new file object called txt2  
15:  fn2 = raw_input("Next filename: ")  
16:  txt2 = open(fn2)  
17:    
18:  # read is performed on the file object again  
19:  print "T %r:" % fn2  
20:  #print txt2.readlines()  
21:  for line in txt2:  
22:    print line  
23:  txt2.close()  
24:    
25:  # It is better to do raw_input because it can collect spaces (while spaces in  
26:  # in the argument vector simply increase its length.  
27:  # On the other hand, argument input supports globbing natively.  
28:    
ex16.py:
1:    
2:    
3:  from sys import argv  
4:    
5:  script, filename = argv  
6:    
7:  print "We're going to erase %r." % filename  
8:  print "If you don't want that, hit CTRL-C (^C)."  
9:  print "If you do want that, hit RETURN."  
10:    
11:  raw_input("?")  
12:  t = open(filename, 'w')  
13:  #t.truncate()  # seems unnecessaryu for this stricpt  
14:    
15:  l1 = raw_input("1:")  
16:  l2 = raw_input("2:")  
17:  l3 = raw_input("3:")  
18:    
19:  t.write("%s\n%s\n%s\n" % (l1,l2,l3))  
20:    
21:  t.close()  
22:    
23:  # check conbtents  
24:  t = open(filename)  
25:  print t.read()  
26:  t.close()  
27:    
28:  print "something"  
29:    
ex17.py:
1:    
2:    
3:  from sys import argv  
4:  from os.path import exists  
5:    
6:  script, ffile, tfile = argv  
7:    
8:    
9:  ifile = open(ffile)  
10:  indata = ifile.read()  
11:  print "Preparing to copy %d bytes from %s to %s" % (len(indata),ffile,tfile)  
12:    
13:  if not exists(tfile):  
14:    ofile = open(tfile,'w')  
15:    ofile.write(indata)  
16:    print "DONE!"  
17:    ofile.close() # its possible without this line that the new file would not  
18:           # be written to disk.  
19:  ifile.close()  
20:  print "something"  
21:    
22:    
23:  # options regarding importing modules:  
24:  ##> import X - creates a ref in current namespace... X.method() works  
25:  ##  X = __import__('x') - imports module defined by string to programmer  
26:  ##          defined variable X in cur namespace  
27:  ##  from X import * - creates refs for all public objects  
28:  ##  from X import Y, Z - creates refs for listed objects  
29:  # the first one is generally ideal.  
30:  # avoid circular imports - two files which import each other  
31:    
32:    
33:  # Below is some fucking about...  
34:  import string  
35:    
36:  print string.ascii_letters #a-zA-Z or maybe the other way around  
37:  print string.digits    # 0-9  
38:  print string.printable   # everything  
39:    
40:    
41:  # os.path is for whatever the current os is...  
42:  # can specify (why would you want to): ntpath, posixpath, macpath  
43:    
44:  abspath ="/home/stude2/s/slithgow/python/data2"  
45:    
46:  # print os.path.basename(abspath)  
47:  #       .exists / lexists <- second is True for broken symlinks  
48:  #                  (first is not)  
49:  #       .splitext <- returns tuple (root,ext) putting shit  
50:  #               in root if theres no ext  
51:    
52:  # s=set([iterable]) eg. s=set('abc')  
53:  # len(s)  
54:  # x in s / x not in s  
55:  # s.isdisjoint(s2)  
56:  # >,>=,<,<= - elementwise compare  
57:  # .issubset(s2) / .issuperset(s2)  
58:  # .union(s2) / .intersection(s2)  
59:    
60:    
ex18.py:
1:    
2:    
3:  #from sys import argv  
4:  #script, filename = argv  
5:    
6:              # Zed says:  
7:  def print_two(*args):  #  *args hactually means all remaining args  
8:    a1,a2 = args    #  so print_two(1,2,3,34,45,5,6,6,7,78) is valid  
9:    print "%r %r" % (a1,a2) # but it will fuck up in the above pattern match  
10:    
11:  def beacorn (*args):  
12:    print "arg1: %r, arg2: %r" % args  
13:    
14:  def print_2(a1,a2,*a3):  
15:    print "%r %r" % (a1,a2)  
16:    
17:  def print_one(a1):  
18:    print "%r" % a1  
19:    
20:  def print_none():  
21:    print "THERE CAN BE ONLY NONE!"  
22:    
23:    
24:  print_two("1.1","1.2") # adding extra args here doesnt seem to work.. ,"1.3")  
25:  print_2("2.1","2.2")  
26:  print_one("3.1")  
27:  print_none()  
28:  #beacorn("one","two","three","hahahaha you suck!")  
29:    
30:  print "something"  
31:    
ex19.py:
1:    
2:  def c(cheese_count, boxes_of_crackers):  
3:    print "You have %d cheeses!" % cheese_count  
4:    print "You have %d boxes of crackers!" % boxes_of_crackers  
5:    print "Man that's enough for a party!"  
6:    print "Get a blanket.\n"  
7:    
8:    
9:  #print "something"  
10:  c(20,30)  
11:    
12:  #print "OR, we can use variables from our script:"  
13:  amount_of_cheese = 10  
14:  amount_of_crackers = 50  
15:  c(amount_of_cheese, amount_of_crackers)  
16:    
17:  #print "We can even do math inside too:"  
18:  c(10 + 20, 5 + 6)  
19:    
20:  #print "And we can combine the two, variables and math:"  
21:  c(amount_of_cheese + 100, amount_of_crackers + 1000)  
22:    
23:    
24:  # one more func of own design  
25:  def newfunc(arg):  
26:    print "%r" % arg  
27:    
28:  # run it 10 different ways:  
29:  #1 iteratively  
30:  for x in range(10):  
31:    newfunc('  '+("%d"%x))  
32:    
33:  #2 hardcoded strings  
34:  newfunc("string")  
35:    
36:  #3 function pointers  
37:  newfunc(c)  
38:  newfunc(newfunc)  
39:    
40:  #4 predefined object  
41:  s = set('abcde')  
42:  newfunc(s)  
43:    
44:  #5 disposable object  
45:  newfunc(set('1234'))  
46:    
47:  #6 nested methods  
48:  newfunc(s.union(set('defgh')))  
49:    
50:  #7 inline hiding  
51:  print "%s" % "hey" and newfunc("hello")  
52:    
53:  #8 from user input  
54:  newfunc(raw_input("Anything:"))  
55:    
56:  #9 printing the reference to imported class chosen at runtime  
57:  newfunc(__import__(raw_input("A Python class:")))  
58:    
59:  #10 regex patten  
60:  import re  
61:  newfunc(re.compile('^((\S*)//)*(\S*)\.(\S*)'))  
62:    
ex20.py:
1:    
2:    
3:  from sys import argv  
4:    
5:  script, filename = argv  
6:    
7:  def print_all(f):  
8:    print f.read()  
9:  def rewind(f):  
10:    f.seek(0)  
11:  def print_a_line(l,f):  
12:    print l, f.readline()  
13:    
14:  peter_file = open(filename)  
15:  print_all(peter_file)  
16:  rewind(peter_file)  
17:  c = 1  
18:  while c<4:  
19:    print_a_line(c,peter_file)  
20:    c+=1  
21:    
22:    
23:  print "something"  
24:    
ex21.py:
1:    
2:  def add(a, b):  
3:    print "ADDING %d + %d" % (a, b)  
4:    return a + b  
5:    
6:  def subtract(a, b):  
7:    print "SUBTRACTING %d - %d" % (a, b)  
8:    return a - b  
9:    
10:  def multiply(a, b):  
11:    print "MULTIPLYING %d * %d" % (a, b)  
12:    return a * b  
13:    
14:  def divide(a, b):  
15:    print "DIVIDING %d / %d" % (a, b)  
16:    return a / b  
17:    
18:    
19:  print "Let's do some math with just functions!"  
20:    
21:  age = add(30, 5)  
22:  height = subtract(78, 4)  
23:  weight = multiply(90, 2)  
24:  iq = divide(100, 2)  
25:  print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)  
26:    
27:    
28:  # A puzzle for the extra credit, type it in anyway.  
29:  print "Here is a puzzle."  
30:    
31:  what = add(age, subtract(height, multiply(weight, divide(iq, 2))))  
32:    
33:  # first time through, i read subtract's shit backwards.  
34:  # for non-commutative functions, best to pay careful attention to  
35:  # order of arguments...  
36:  yes = height - iq /2 *weight+age  
37:  # not sure if that was what was expected for extra credit #2..  
38:    
39:  print "That becomes: ", what, "Can you do it by hand?", yes  
40:    
41:    
42:  # ugh... im not doing any of the other ones without infix support...  
43:  #  found this: http://dev-tricks.net/pipe-infix-syntax-for-python  
44:  #  looks perfect, but when I  
45:  #  from pipe import *  <- tried using this... it became apparent that  
46:  #  it requires the functions to be rewritten using 'yield' which we havent  
47:  #  covered yet... baby steps, baby steps.  
48:    

Tuesday, August 21, 2012

Exercises 6-13: Variables and basic input/output

ex6.py:
1:  x = "There are %d types of people" % 10  
2:  binary = 'binary'  
3:  do_not = "don't"  
4:  # I suppose this line shows that you can assign formatted strings to  
5:  # variables as well as printing them straight to stdout  
6:  y = "Those who know %s and those who %s" % (binary, do_not) # 1 (maybe 2)  
7:  print x  
8:  print y  
9:  # This is to show its the value of the string that affects the output  
10:  # rather than the name of the variable  
11:  hilarious = False  
12:  joke_eval = "Isnt that joke so funny? %r"  
13:  print joke_eval % hilarious  # 2 (3)  
14:  # Shows how + concatenates strings  
15:  w="1 2 3..."  
16:  e="4 5 6."  
17:  print w+e  # depending on interpreation = 2/3/4  
18:  # at least 2 strings are put in side other strings  
19:  # depending on interpretation, up 4-5 strings are put in other strings  
20:  # In order for + to be a valid operation, its arguments  
21:  # must be ordered and have a distance between.  
22:  # So, you can add integers, reals, complex numbers, vectors...  
23:  # But strings aren't either of these. So what to do when you call + on a  
24:  # pair of strings? throw an exception? Whoever made python evidently  
25:  # decided the most logical answer to that question is to concatenate the  
26:  # strings. imho, at least as good of an answer as any other.  
ex7.py:
1:  print "Mary had a little lamb."  
2:  print "Its fleece was white as %s." % 'snow'  
3:  print "And everywhere that Mary went."  
4:  print "." * 10 # what'd that do?  
5:  end1 = unichr(67).encode('utf-8')  
6:  end2 = "h"  
7:  end3 = "e"  
8:  end4 = "e"  
9:  end5 = "s"  
10:  end6 = "e"  
11:  end7 = "B"  
12:  end8 = "u"  
13:  end9 = "r"  
14:  end10 = "g"  
15:  end11 = "e"  
16:  end12 = "r"  
17:  # watch that comma at the end. try removing it to see what happens  
18:  print end1 + end2 + end3 + end4 + end5 + end6,  
19:  print end7 + end8 + end9 + end10 + end11 + end12  
20:  # comma seems to squelch the implicit newline character  
21:  # and replaces it with a space.  
ex8.py:
1:  formatter = "%r %r %r %r"  
2:  print formatter % (1,2,3,4)  
3:  print formatter % ('one','two','tree','four')  
4:  print formatter % (True, False, False, True)  
5:  print formatter % (formatter,formatter,formatter,formatter)  
6:  print formatter % (  
7:    "I had this thing",  
8:    "That you couldd type right up",  
9:    "But it didnt sing",  
10:    "So I said gooednight")  
11:  #print formatter % (1,2,3,4)  
12:  # because %r is used instead of %s, its not immediately assuming  
13:  # that variable/constant is a string, so when it gets strings it prints  
14:  # that it is a string but surrounding it in apostrophes.  
ex9.py:
1:  days = "M T W T F S S"  
2:  months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"  
3:  print "dyas:",days  
4:  print "months:",months  
5:  print """  
6:  Theres something goign on here  
7:  with the three dobules 1quoes  
8:  well be able to totype as much as we like  
9:  even 4 lines i fwe want or 56 or 6  
10:  """  
11:  days = "M T W T F S S"  
12:  months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"  
13:  print "dyas:",days  
14:  print "months:",months  
15:  print """  
16:  Theres something goign on here  
17:  with the three dobules 1quoes  
18:  well be able to totype as much as we like  
19:  even 4 lines i fwe want or 56 or 6  
20:  """  
ex10.py:
1:  print "%r" % "I am 6'2\" tall." # escape double-quote inside string  
2:  print "%r" % 'I am 6\'2" tall.' # escape single-quote inside string  
3:  # Huh... %r automatically changes them to single quotes, parsing escaped  
4:  # chars and re-escaping apostrophes. Pretty rocking. I would guess then that  
5:  # \ts and \ns etc. are also not printed for %r prints  
6:  tabby_cat = "\tI'm tabbed in."  
7:  persian_cat = "I'm split\non a line."  
8:  backslash_cat = "I'm \\ a \\ cat."  
9:  fat_cat = """  
10:  I'll do a list:  
11:  \t* Cat food  
12:  \t* Fishies  
13:  \t* Catnip\n\t* Grass  
14:  """  
15:  print tabby_cat  
16:  print persian_cat  
17:  print backslash_cat  
18:  print fat_cat  
19:  for j in range(10000):  # while True:  
20:    for i in ["/","-","|","\\","|"]:  
21:      print "%s\r" % i,  
22:  # CTRL-C in linux/osx, CTRL-Z? perhaps D in windows to break infinite loops  
23:  # somehow \r makes this print on top of the last character printed. very cool.  
24:  # I did not realise this was even possible in shell... :O!  
25:  fat_cat = '''  
26:  I'll do a list:  
27:  \t* Cat food  
28:  \t* Fishies  
29:  \t* Catnip\n\t* Grass  
30:  '''  
31:  print fat_cat  
32:  # seems to do the exact same thing. presumably the choice is made by which  
33:  # chars you want to escape.  
ex11.py:
1:  print "How old are you?",  
2:  age = raw_input()  
3:  print "How tall are you?",  
4:  height = raw_input()  
5:  print "How much do you weigh?",  
6:  weight = raw_input()  
7:  # raw_input() appears to take stdin until a newline  
8:  # and automatically throw away that newline  
9:  print "So, you're %r old, %r tall and %r heavy." % (  
10:    age, height, weight)  
11:  # ah the benefit (besides diagnostic prints) of %r becomes clear here :D  
12:  print "How much are you liking Python now?",  
13:  leadingq1 = raw_input()  
14:  print "How much do you expect to love Python in the future?",  
15:  leadingq2 = raw_input()  
16:  print """  
17:  So, you like Python %r now, and will love Python %r in the future.  
18:  Meaning the rate at which your opinion of Python is changing is  
19:  %r per time unit.  
20:  """ % ( leadingq1, leadingq2, (int(leadingq2)-int(leadingq1)))  
ex12.py:
1:  age = raw_input("How old are you? ")  
2:  height = raw_input("How tall are you? ")  
3:  weight = raw_input("How much do you weigh? ")  
4:  print "So, you're %r old, %r tall and %r heavy." % (  age, height, weight)  
5:  #print sys.argv  
6:  # ^ this didnt work  
ex13.py:
1:  from sys import argv  
2:  # ^ ah right, that solves that then  
3:  #script, first second, third = argv  
4:  #       ^  MY FIRST COMPILE ERROR!  
5:  # Even though Zed keeps telling me to write these down so they dont happen  
6:  # again, in the case of typos, I think I will skip his sage advice.  
7:  script, first, second, third = argv  
8:  print "The script is called:", script  
9:  print "Your first variable is:", first  
10:  print "Your second variable is:", second  
11:  print "Your third variable is:", third  

Exercises 1-5: Starting Zed's tutorial and the basics of Python

I decided the learn Python as I had the perfect application for it. I did a quick Google for something like 'Learn You a Haskell for Great Good' (one of the best tutorials I've ever done) but for Python. Instead, Zed Shaw's Python tutorial came up and after reading the introduction, I thought this would be perfect. I'm a fan of tutorials that make minimal assumptions about how much you know. I tried to learn Fortran by following a tutorial something like 'Fortran for C/Perl programmers' and found that although there was a lot less I could glide through, there was also some things that I didn't end up understanding properly because they were presumably assumed knowledge and as a result I never really stuck with Fortran.

http://learnpythonthehardway.org/book/

Free online! If I make my way through it I'll probably buy the book - US$15 is fairly inexpensive price to pay to learn a new language.

In any case, here are my source files for the first five chapters (after having completed the exercise, so they are more like answers). In some of the first few I did exactly as he told me not to and copy-pasted the lines but meh - I think I learned all the lessons. I suppose we shall see.

ex1.py:
1:  #print "Hello World!"  
2:  #print "Hello Again"  
3:  #print "I like typing this."  
4:  #print "This is fun."  
5:  #print 'Yay! Printing.'  
6:  #print "I'd much rather you 'not'."  
7:  #print 'I "said" do not touch this.'  
8:  print 'another line'  

ex2.py:
1:  # A comment, this is so you can read your program later.  
2:  # Anything after the # is ignored by python.  
3:  print "I could have code like this." # and the comment after is ignored  
4:  # You can also use a comment to "disable" or comment out a piece of code:  
5:  # print "This won't run."  
6:  print "This will run."  

ex3.py:
1:  print "Hens", 25+30/6 # order of ops, and commas result in a single space char  
2:  print "Cocks", 100-25*3%4 # same, but with mult & mod (mult first)  
3:  print "Eggs:"  
4:  print 3+2+1-5+4%2-1/4.0+6 # order of ops, this time showing mod before +  
5:              # and that 1/4 is integer div  
6:  print "3+2<5-7 ?"  
7:  print 3+2<5-7  
8:  print "What is 3 + 2?", 3 + 2  
9:  print "What is 5 - 7?", 5 - 7  
10:  print "Oh, that's why it's False."  
11:  print "Is it greater?", 5 > -2  
12:  print "Is it greater or equal?", 5 >= -2  
13:  print "Is it less or equal?", 5 <= -2  

ex4.py:
1:  cars = 100  
2:  space_in_car = 4  
3:  drivers = 30  
4:  passengers =90  
5:  cars_not_driven = cars - drivers  
6:  cars_driven = drivers  
7:  passenger_cap = cars_driven * space_in_car  
8:  mean_passengers_per_car = passengers / cars_driven  
9:  print "There are", cars, "cars available."  
10:  print "There are only", drivers, "drivers available."  
11:  print "There will be", cars_not_driven, "empty cars today."  
12:  print "We can transport", passenger_cap, "people today."  
13:  print "We have", passengers, "to carpool today."  
14:  print "We need to put about", mean_passengers_per_car, "in each car."  

ex5.py:
1:  my_name = 'poop'  
2:  my_age = 25 # not a lie  
3:  my_height = 180 # cm  
4:  my_weight = 90 # kg  
5:  my_eyes = 'Blue'  
6:  my_teeth = 'White'  
7:  my_hair = 'Brown'  
8:  print "Let's talk about %s." % my_name  
9:  print "He's %d cm tall." % my_height  
10:  print "He's %r kg heavy." % my_weight  
11:  print "Actually that's not too heavy."  
12:  print "He's got %r eyes and %s hair." % (my_eyes, my_hair)  
13:  print "His teeth are usually %s depending on the coffee." % my_teeth  
14:  # this line is tricky, try to get it exactly right  
15:  print "If I multiply %d, %d, and %d I get %d (kg.cm.years)." % (  
16:    my_age, my_height, my_weight, my_age * my_height * my_weight)