Intro to
Programming
with Python

Fall 2012
course
navigation

Nov 8

Assignment for next week has been posted - look at it and discuss briefly.

dictionaries and counting

Say you have some things, like this :
things = ['cat', 'dog', 'cat', 'cat', 'dog', 'skunk'] # How would you count how many of each you have, in python? # Well, how about a dictionary? counts = {} # one way for thing in things: if thing in counts: counts[thing] += 1 else: counts[thing] = 1 # a slightly better way for thing in things: counts[thing] = counts.get(thing, 0) + 1 print counts
Then look at this take on word counts in moby dick .

randomly generated papers

We looked at these in passing:

list functions and randoms

It's a good time to look again at Python's list and random methods, since they're particularly helpful in the sorts of programming we're doing now.
Check out in particular
random.shuffle(list) # modify list in place list.sort() # put list in order, in place (uses cmp()) x = list.pop(i) # remove list[i] and return it

cards

Yes, let's look at this one more time, since many of you have been distracted by this and that lately. And the practice can't hurt.
First, let's look at simple cards.py.
And then there's my way-over-the-top poker.py ... just to illustrate what can be done. That one doesn't use anything that you haven't seen, but does use data structures in some math-ish tricky ways.

If time remains, practice together on a different problem, say, tic tac toe.
http://cs.marlboro.edu/ courses/ fall2012/python/ notes/ Nov_8
last modified Thursday November 8 2012 10:48 am EST