Intro to
Programming
(with Python)

Fall 2015
course
navigation

Tue Sep 16

chap 2 & 3 homework

... is due today.
The submissions I've seen didn't include what running the code does ... please do put that in for future assignments.
Generally speaking, you should plan on trying to get the homework in before Tuesday's class, since we'll typically go over it then. I will accept work after that, but since I may have posted my solutions and/or we'll have discussed the problems in class, it will likely be worth less.
I've posted my answers to the homework . (Locked until the class meets.)
Please do use that as an example of what I want:
I remind you that Sam Judson has tutorial hours over the weekend - see his emails - to answer questions and help.
Also be aware of the python docs on resources page including :
As I showed last week, you can also ask python for the names it knows.
$ python >>> dir() # list of defined names >>> dir(__builtins__) # list of built-in functions
There is also help within the python prompt.
$ python >>> help >>> help(dir) # describe the dir() built-in function >>> help() help> keywords
Questions about the homework? Parts you'd like to discuss? Any other questions about anything so far?
Homework for next Tuesday has been posted: chapter 4 stuff on strings.
That brings us to ...

chapter 4 : strings

(We'll see how far we get with all this today, and continue next class.)
At this point you should understand :
Next: working with strings.
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

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 = 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'
Aside :
There are actually three 'places' that the string functions live, which can be confusing :
>>> number = ord('h') # built-in function >>> import string; string.upper('hello') # within the string module >>> 'hello'.upper() # (!)
The last form we haven't talked about yet, but will do lots more with later. (This is actually "object-oriented" programming, in which we think of the string 'hello' as an "object" and upper as one of its "methods".)
Many of the string manipulations are available in both the string module and as methods of strings. To see the difference
>>> import string # load the string module >>> dir(string) # functions and variables in the string module >>> dir('hello') # names that can be used as 'hello'.name

text formatting

Making the output look nice.
There are several ways to do this within python: the old way (in your text), and the new way (recommended).
In both cases, markers are put within a string to indicate where values should go, and then variables are "interpolated" into the string in some given format. The details get messy.

the old way : ("x=%f" % 3)

I'll mention this and then (mostly) ignore it.
print "Compare %f and %0.20f \n" % (3.14, 3.14)
Here the "%" symbol is placed between (a) the string with its markers and (b) a sequence of the values to be interpolated.
The funky symbols within the string (%i, %f, %d, %s) give the format (integer, float, decimal, string).
See http://docs.python.org/2/library/stdtypes.html#string-formatting-operations

the new way : "x={}".format(3)

The python language has been transitioning to a new syntax for string formatting, the .format() string method and {} for interpoloation.
The new version looks like this :
print "Compare {} and {:0.20} ".format(3.14, 3.14)
This is what you should do. For the details see the docs.

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', '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
Are we having fun yet?
http://cs.marlboro.edu/ courses/ fall2015/python/ notes/ Sep_15
last modified Tuesday September 15 2015 10:59 am EDT