Intro to
Programming
with Python

Fall 2011
course
navigation

sep 29

Homework comments : Please do test & run your code. (I'm seeing syntax errors that suggest some were typed in but not actually executed.)
Next assignment (chapter 6) is up.

functions

doctests

Discuss how you would make sure a function was doing what you expect
Function with docs and tests example ; see the doctest documentation.
Suppose you're writing a program that has something to do with palindromes. One thing you'd need is to have strings turned around backwards. So what would a function look like to do that? Turns out (google "python reverse") that you can reverse lists easily. And we've seen before that you can turn strings into lists and vice-versa. So you play around at the command line and figure this out:
>>> word = "hello" >>> chars = list(word) >>> chars ['h', 'e', 'l', 'l', 'o'] >>> chars.reverse() ['o', 'l', 'l', 'e', 'h'] >>> import string >>> string.join(chars, '') 'olleh'
This is a small specific piece of the larger problem that you can tuck into a function. Together with its docs and a test it would look like this:
def reverse_string(word): """ Reverse a string. input: a string, e.g. "hello" output: the string backwards, e.g. "olleh" >>> reverse_string("Can you hear me?") '?em raeh uoy naC' """ import string chars = list(word) chars.reverse() return string.join(chars, '') if __name__ == "__main__": # This runs the doctests; import doctest # just put these 3 lines at the doctest.testmod() # end if you do this sort of testing.
Put this in "test.py", run it with "python test.py -v".
Also put the wrong thing in the test, and run it with "python test.py".

encode / decode example

An example : encode / decode from before, this time with functions .
The original is here : cipher.py
Ideas :
Class exercise :
Here's a start of a top-down approach to the cipher problem :
""" cipher_functions.py """ def get_user_input(): # fill in docs & test return ('wwww', 'kkkk') # FIXME def translate(word, key): # fill in docs & test return '????' # FIXME def main(): print "-- cipher --\n" (word, key) = get_user_input() encoded = translate(word, key) print "'%s' encrypted is '%s'" % (word, encoded) # TODO: put stuff to run doctest here main()

book example

Look at futval_func_example.py. For each function:

default values in function arguments

def do_something(size, color="white"): # code goes here print "size, color = %i, %s" % (size, color) do_something(3, "blue") # this is OK do_something(4) # also OK; color defaults to "white" do_something() # *not* OK; 0 args gives error
Args with defaults may only go on the *end* of the argument list ... otherwise the later ones (required) imply that the earlier ones are also required.
Note that this is a python-specific syntax.

multiple return values

def get_stuff(): return 1, "hey" # And now use it a, b = get_stuff() # "Tuples" in python

other bells and whistles

Less used, and can be safely ignored on a first pass through python. But for those who like the tricky corners... def two_or_more_args(a, b, *c): print "a = '" + str(a) + "'" print "b = '" + str(b) + "'" print "c = '" + str(c) + "'" # and now call it. two_or_more_args(3, "hey") # a=3, b="hey", c=[] two_or_more_args(3, "hey", 4, "there") # a=3, b="hey", c=[4, "there"]

depending on time

... start "conditionals" (chapters 7 and 8): value = input("What is the number? (1 to 10) ") if value < 1 or value > 10 : print "Oops: your number was too big or too small." print "Your number is %i" % number
Discuss flowcharts.
Discuss comparison operators.
Coming: boolean variables.
http://cs.marlboro.edu/ courses/ fall2011/python/ notes/ sep_29
last modified Wednesday September 28 2011 11:16 pm EDT

attachments [paper clip]

     name last modified size
   cipher.py Sep 28 2011 9:56 pm 676B    futval_func_example.py Sep 28 2011 10:49 pm 1.35kB