Intro to
Programming
with Python

Fall 2012
course
navigation

Nov 1

aside

A couple more interesting python resources :

apologies

I'm (still) not finished grading the midterm projects. They will be done "real soon", hopefully by Fri afternoon.

old homework

Discuss the homework, looking at several of your submitted pieces.
Volunteers welcome.
We went over a "method chaining" and described how to write objects that allowed syntax like
thing.double().increment().double)
I've uploaded the chaining.py code we wrote in class.

new business

Continue discussion of classes.
Some examples, including the notion of "inheritance" and "class variables" are in the code/objects directory .
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.
My example of a Card class is blackjack pair.py .
It includes use of one more special class method, __cmp__ . cmp(x,y) is a built-in Python function that compares things.
And there are many more "special method names" in classes, all with names like __*__ , for adding, comparing, and doing things like making classes behave like functions or collections (i.e. dictionaries and lists). Thus you can invent your own, new python data structures, and add 'em in.
See http://docs.python.org/2/reference/datamodel.html#specialnames for the details.

new homework

... has been posted.
I want you to use a "template" top down approach to working on your code : first write the form of the class and methods, with "fake" return values (no "print" allowed for this assignment within classes), along with the main() method that uses the classes. The point is to make sure that you know what methods and classes you want *before* you try to implement the specific methods.

other business

My interactive fiction with finite state machine code, as a follow-up to some of your midterm work.
We should talk some more about debugging techniques. This is something all programmars 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 :

http://cs.marlboro.edu/ courses/ fall2012/python/ notes/ Nov_1
last modified Thursday November 1 2012 12:30 pm EDT