sep 13
homework
Notes from looking over your work:
- Each python program should be put in a *.py file. From the IDLE GUI (if that's what you're using), choose "New File", then "Run Module" to do it. Typing whole programs at the >>> will get old really fast; you want to be able to make small changes and see what happens.
- Spaces between lines are fine where they add to readability ... but don't overdo.
- Choose variable names wisely. (It becomes built in documentation.)
(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
- a string s an array of characters.
- discuss subscript conventions: a[i], a[i:j], a[i:], a[:i], a[-1]
ascii
ASCII : numeric values for 1 byte characters;
- wikipedia: ascii
- printable are 32 (space) through 176 (~).
- In python: ord() and chr() convert between characters and ASCII.
>>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 :
- open a file
- read stuff in (typically text or numbers)
- do something to it (put it into a secret code, for example)
- write it back out
Example: write together in class a program to do file conversion for rot13
- see wikipedia:rot13
- Ask for input file name
- create file with same name, but 'rot13_' on front.
- Read input to a string.
- In a loop, convert (and store) the thing to its rot13 version
- Output new string to new file