Intro to
Programming
with Python

Fall 2011
course
navigation

sep 13

homework

Notes from looking over your work:
(We did an average and standard deviation program in class, which is attached down below.)
Discuss indentation and being inside or outside a block. In the average case, a loop.
Discuss Fibonnaci series.
(Also did a fibonnaci program using tuples in class; also attached below.)
Do one of the accumulator patterns such as average in class. Do we need to ask for the number of values? Can we find the variance and standard deviation too?

asides

stuff from last week

Make sure you understand the difference between this:
>>> dir() >>> from math import * >>> dir()
and this:
>>> dir() >>> import math >>> dir() >>> dir(math)

Documentation links on resources page :

going on : chapter 4 : strings

(We'll see how far we get with all this today.)

input

This fails:
# THIS WON'T WORK name = input("What is your name? ") print "You said ", name
because the input() command treats what you typed as if it were python code. (Oops).
Instead, you should do this:
name = raw_input("What is your name? ") print "You said ", name

playing with strings

ascii

ASCII : numeric values for 1 byte characters;
>>ord('a') # convert character to integer (ascii) 97 >>chr(90) # convert integer (ascii) to character 'Z'
, pg 92
, pg 93
(Aside: you can download the python programs in the textbook from Zelle's website; see the links on the resources page.

string operations summary

There are many ways to manipulate strings in python.
'this ' + 'that' # concatenate 'x' * 10 # replicate 'This is a string'[0:3] # substrings; 'Thi' len('string') # how many characters? for char in the_string: # loop over characters print char, ' is ', ord(char) # ... and do something with each # lots more import string # grab a bunch of 'em dir(string) # list all the string.yyy() functions dir("xxx") # what the the "xxx".method() things? # conversions x = float("32.3") # convert string to float y = int("101") # convert string to int z = str(anything) # convert anything to a string eval("1+2") # evaluate string as python expression; return result
Discuss the name.method(arg1, arg2) convention we just started seeing.
We'll be getting lots more of that. And remember the dir() trick to see what .method() things there are for different things in the language.
More practice with string manipulations.
import string text = "This is some text." text_list = list(text) # ['T', 'h', ...] test_list2 = string.split(text, '') # same thing back_to_text = string.join(text_list, '') hello_there = "hello" + " " + "there" number = ascii('h') letter = chr(number) word_dashes = 'one-two-three' word_list = string.split(word_dashes, '-') # ['one', 'two', 'three'] word_commas = string.join(word_list, ',') # 'one,two,three'

text formatting

Making the output look nice: text formatting : %i, %f, %d, %s within format strings, using "%" operator on strings.
print "Compare %f and %0.20f \n" % (3.14, 3.14)

Files

A file is essentially a list of lines. Once you've "opened" one, you can read from it and/or print to it.
inputfile = open("filename_here", "r") # In that last line : # 'r' for 'read' access', i.e. input, # 'w' for 'write access', i.e. output # Input: use one of these three : whole_file = inputfile.read() next_line = inputfile.readline() array_of_lines = inputfile.readlines() outputfile = open('other_filename_here', 'w') outputfile.write("this will be the first line in that file. \n") # \n is newline

summary of strings, text, files

Putting all this together you get a very typical program :
Example: write together in class a program to do file conversion for rot13
http://cs.marlboro.edu/ courses/ fall2011/python/ notes/ sep_13
last modified Wednesday September 14 2011 9:57 pm EDT

attachments [paper clip]

     name last modified size
   fib.py Sep 13 2011 11:29 am 255B    in_class_average_variance.py Sep 13 2011 10:37 am 603B