Sep 11
chap 2 & 3 homework
... is due today.
How did it go?
Work a few similar examples in class:
- Write a program to calculate the mean of N numbers input by the user.
- (First ask for N, the ask for the numbers one at a time.)
- Also find the standard deviation.
- Now input the numbers as a python list (i.e. [2, 4, 6]).
- Write a program to find the N'th Fibonnacci number.
- Second version: use a tuple.
All of these files are attached at the bottom of this page.
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.)
At this point you should understand :
- creating / editing programs (i.e. XXX.py files)
- running python programs
- integers and floats (decimals), and some operations on 'em
- basic input and output from the command line
- basic loops and "accumulating" something during the loop
This week: working with strings (chapter 4).
So, part one of this week's assignment: read chapter 4, and bring questions to class.
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 = ord('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