DAY 4 (just the end of week 2! Slow down) ==today, we are going to practice with stuff you already saw operator precedence swapping assigning to variables and replacing values calling functions [Go over the final sunset.py] Python Shell # remember operator precedence # 1. ** # 2. +x -x # * / % # + - 10 % (2 * 3) 10 % 3 - 4 / 2 5 * 2 / 5 * 2 5 * 2 / (5 * 2) 5 * 2 ** 3 5 * (2 ** 3) (5 * 2) ** 3 -2 ** 4 (-2) ** 4 ! On the board, and in the software [code trace1.py] Motivation for functions? When you do something more than once. [code song.py] - to be silly for a second :-) Let's swap the values of two variables: [trace this on the board] i = 5 j = 15 i = j #oops! We lost i's old value i = 5 j = 15 temp = i # save i's value i = j # now it is OK to clobber i's old value j = temp Let's calculate future value of an investment, given APR, current value, and number of years into the future. *** I did this very slowly. Repeated myself alot. Except for the top people, the pace seems needed. There are direct formulas to use. We'll do it with a loop. how do we solve this? value = current_investment Each year - the value increases by apr * last year's value e.g. - apr = .06 value = 1000 after year 1: 1000 + .06 * 1000 = 1060.00 after year 2: 1060 + .06 * 1060 = 1123.60 after year 3: 1123.60 + .06 * 1123.60 = 1191.016 and so on OK? NOTE: the above a good test case, so let's save the values. 1000 + .06 * 1000 = 1000 * (1 + .06) So, we'll use this simpler version *** Note: we are doing this in the editor so that we can put it in a file and save the definitions. def future_value(current_value,apr,years): new_value = current_value # start things off!! 'Initilization' how many calculations? exactly 'years' remember the for loop? range(years) - see what that does in the python shell. years = 10 # suppose years has this value range(years) this is a "list" for i in range(years): *** 'i' is a variable whose name we made up. *** the for loop will step through the list items one by one. just like the for-loops on the lab. year = 1 year = 2 year = 3 and so on OK, now we have the initilization and the loop set up. How do we write the calculation? everyone write something down in their notes new_value = new_value * (1 + apr) assignment: evaluate the RHS - get's contents of new_value and apr and performs the arithmetic. THEN: places that value back in new_value, clobbering the value it used to have. That's fine, we only need the new value. [leave off the return new_value as a bug] Let's test it. We have a test case above if __name__ == '__main__': future_value(1000,.06,3) run the program. nothing happens. We called future_value, but didn't save the value, or print it, etc. print "The future value of a 1000 investment with APR .06 after three years is",future_value(1000,.06,3) Still doesn't do anything Need the return value! Test it with our test case - it worked! We also should test with special cases. How about 0 years? 1 year? APR of 0? DEBUGGING: you test it on a general/typical case, like the one above, and then test on all the special cases. While you are writing your program you want to design test cases. *If you are going to use the program for something, you want to be sure it is correct! Wouldn't want to make investment decisions on a buggy program! special cases. How about 0 years? 1 year? APR of 0? also, let's make our job easier by writing a print function def print_future_value(current_value,apr,years,future_value): print "The future value of a", current_value, "investment with APR", apr, "after three years is",future_value # should be 1191.016 test1 = future_value(1000,.06,3) print_future_value(1000,.06,3,test1) OK, we could call this in a more convenient way print_future_value(1000,.06,3,future_value(1000,.06,3)) How will this execute? python will start to execute print_future_value. Before it can execute, it has to have all of its arguments. It will get to future value, and then go execute that. Yeah, it works! Ok, now let's call it on two different CDs and then total up the two values. Suppose this will be a longer program, and we need to save the total value in variable 'total' Let's try: future_value(1000,.06,3) future_value(1500,.03,3) total = # oops - the above just call the function, and throw the value away #assuming we need to save the cd values for later in the program: cd_1_val = future_value(1000,.06,3) cd_2_val = future_value(1500,.03,3) total = cd_1_val + cd_2_val #Suze Orman example - 45 year old woman with $74K in retirement to # buy a $6400 pool? # Denied! if she invests the money now at 4% and retires at 65, # she will have this much more money print_future_value(6400,.04,20,future_value(1000,0,3)) She would be giving up 8000 (or more, if she gets a higher rate!) This is a very simple program, but still useful! Ok, so that gave us practice with solving a problem, function definitions, function calling, *testing*, and for-loops. Now, let's move on: == Giving help: docstring == - You've used python help to find out about built-in functions - When you write your own function (or module), you can provide a comment that will be displayed when someone asks help. - It's called a docstring - Notation: ''' - write a docstring for each of our functions - now try help on it! (Must run the program first, so Python knows about the docstring) [sunset_nicely.py] NOTE: it does not print out the comments prefaced by # -- those are for someone who is reading the code to understand. Only the 'docstring', which is between ''' and ''' == Input and output Let's have a closer look at input and output (we'll do more later in the course, too) [into the shell] raw_input('Enter your name:') name = raw_input('Enter your name: ') name type(name) # Our first string variable! print 'hello' print 'hello my name is', name # "," is for printing lists of items, # separated by blanks. print 'hello my name is' + name # "+" smucks 2 strings together. print 'hello my name is ' + name # We can add a blank if we want. - "raw" as in untreated (like sewage). Just gives you exactly what is entered, up to newline, as a string. There are other functions for input; they all do something to the input, eg clean it up in some way. END DAY 4