Intro to
Programming
(with Python)

Fall 2015
course
navigation

Oct 1 - more about functions

last Tuesday's homework

... generally looked good.
The assignment for next Tuesday, on chapter 6, is posted. (We'll be doing both chapter 7 and 8 the following week, so if you want to get a headstart on that, read ahead.
What I was looking for from the questions on object-oriented programming concepts :
Here is some python code using the Zelle's graphics module.
red_dot = Circle(Point(50, 40), 4) red_dot.setFill("red")
Then we have :
Circle is a class red_dot is an instance of class Circle setFill is a method of class Circle "red" is an argument passed to setFill
Anything else you'd like to talk about ?

functions

Following up on that and continuing discussion of functions from Tuesday :

doctests

How do you know that a function is doing what you expect? Tests.
We started talking about doctests last time; here are more examples.
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".

program development

Ideas :
Often as you get more experience, you not to implement every function right away - instead you set up their interfaces and see how they fit together to solve the whole problem.
Here's an example to complete in class :
""" factorial.py Looking at functions, doctests, and test-driven deveopment. Jim M | Oct 2013 """ def factorial(n): """ Return the factorial of n, i.e. n!, which is n * (n-1) * ... 2 * 1 >>> factorial(2) 2 >>> factorial(5) 120 """ return 0 # This is the wrong result. Tests will fail. def main(): print "-- factorial --" n = input("What is n? ") print "{}! is {}.".format(n, factorial(n)) if __name__ == "__main__": import doctest doctest.testmod() main()
In class modify this so that factorial() passes its tests.
A slightly larger example : the cipher program we wrote two weeks ago.
My implementation is at cipher_functions.py
With this model of writing code, you can play around with pieces from the command prompt without running the main() function every time, like this :
$ python >>> from cipher_functions import * >>> encode_char('a', 3) >>> encode_word('hello', 18)
When it's imported this way, the package __name__ is not "__main__", and so the doctests and main() are not run.
However, after you make changes to the source file (cipher_functions.py in this case) you should quit the interactive shell and import it again.
(ipython has some tricks to get around this; google "ipython run" and "ipython relaod" to learn more.)

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/ fall2015/python/ notes/ Oct_1
last modified Thursday October 1 2015 11:22 am EDT

attachments [paper clip]

     name last modified size
   factorial.py Oct 1 2015 11:21 am 974B