Intro to
Programming
with Python

Fall 2010
course
navigation

sep 30

Homework comments :
Next assignment (chapter 6) is up.

discuss functions

Back to encode / decode, this time with functions .
Class exercise :
Ideas :
Look at futval_func_example.py. For each function:

default values

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/ fall2010/python/ notes/ sep_30
last modified Thursday September 30 2010 3:16 pm EDT

attachments [paper clip]

     name last modified size
   BBARS.py Sep 30 2010 2:14 am 1.85kB    cipher.py Sep 30 2010 3:16 pm 676B    cipher_func.py Sep 30 2010 3:16 pm 582B    futval_func_example.py Sep 30 2010 2:23 am 1.36kB    laser.py Sep 30 2010 2:06 am 3.77kB