Intro to
Programming
(with Python)

Fall 2015
course
navigation

Nov 3

Grades and comments are posted for your midterm projects. As I said last week, nice work all around.
So: questions about anything so far?

recent new material

Where we are :
This week we're practicing using classes and seeing how they can be used, and introducing a new data structure.
I haven't been talking a lot in class about exactly what's in the textbook, but I hope you've been reading along - I think the material we're discussing is covered well there.

homework for this week

How did it go?
Here's my version of part of the homework :

new syntax

__cmp__

A special class method that lets you compare two class instances with less-than and greater-then.
first_hand = Pair() second_hand = Pair() if first_hand > second_hand: print "First player wins!"
The __cmp__ (compare) method lets us add this behavior to our classes. First, though we should look at the cmp(a,b) built-in function, which always returns either -1, 0, or 1 depending on whether a is more, equal, or less than b. The __cmp__ is method supposed to do the same thing.
>>> cmp(2,1) # returns 1 since 2 > 1 >>> cmp(2,2) # returns 0 since they're == >>> cmp(1,2) # returns -1 since 1 < 2

dictionaries

Python "dictionary" data structure. Like an array, but with non-numeric "keys". The pairs of things in 'em are called (key, value) pairs. And there are lots of tricky methods built in;
jim = { "name" : "Jim", "age": 53 } print "My name is ", jim['name'] print "My age is ", jim['age'] # Look at dir(jim) to see all the methods like # jim.keys() # jim.items() # jim.has_key(key) # jim.get(key, value_otherwise)
Dictionaries are very useful data structures. In fact, the symbol table of a running program is essentially just a dictionary, with each variable's name and value. Objects are very like dictionaries, with "slots" for both data and methods. Most of the other CS data structures are things with specific uses built from lists and dictionaries.
Coming: the many uses of dictionaries, including counting things.

aside : method chaining

Sometimes it's useful to have object methods that return the object itself. That lets you do one thing after another to the same thing, with a syntax like this :
thing.double().increment().double()
See for example chaining.py.

revisiting card.py

With the tricks that we just discussed, can we make a nicer version of the my Card class?
Here is blackjack_pair.py : both a Card object and a Pair object.
Next week's assignment will continue this example, looking at creating objects that can hold data and implement behavior of card games.

debugging

At some point we should also talk some more about debugging techniques.
This is something all programmers do, and learning how is part of the game. Several of you are getting stuck in the homework, and aren't sure how to get unstuck. A few ideas :

in class

If there's time, I'd like to work on another class programming exercise.
The task is to write a program which will read in a file which has one word per line, and then to report how many times each word was seen.
A sample words.txt file is attached.
Here's the start of the program :
""" count_words.py Read words.txt which has one word per line. Count how many times each word appears and print a summary. $ python count_words.py -- count_words -- 12 cat 2 dog 2 duck 1 elephant 3 mouse """ def main(): print '-- count_words --' if __name__ == '__main__': import doctest doctest.testmod() main()
And here's the corresponding words.txt file like.
cat dog elephant cat cat mouse mouse duck mouse duck dog
http://cs.marlboro.edu/ courses/ fall2015/python/ notes/ Nov_3
last modified Thursday November 5 2015 10:03 am EST

attachments [paper clip]

     name last modified size
[TXT]words.txt Nov 3 2015 12:53 am 58B