Intro to
Programming
(with Python)

Fall 2016
course
navigation

Nov 3

Tuesday's homework has been graded - generally looks pretty good. The assignment for next week (Poker) has been posted.
Questions about anything?
Start by looking over the end of Tuesday's notes: debugging.
Then on to applications of dictionaries, looking at the example from Tuesday's notes :
Write in class a program to count the words in the file.
The code we wrote is attached.

implementing a "bank" with a dictionary

Here's another example of what a dictionary could be used for.
bank = { 'checking' : 100.23, 'saving' : 1200.00 } print "My checking account has ", bank['checking'] print "My saving account has ", bank['saving'] # Look at dir(bank) to see all the methods like # bank.keys() # bank.items() # bank.has_key(key) # bank.get(key, value_otherwise)

implementing a "bank" with a class.

Or we could do the same sort of thing with a class. Both classes and dicts let you put data inside them, with names attached.
(In fact, the Javascript programming language merges python's notion of classes and dictionaries.)
--- file bank.py --- class Bank(object): def __init__(self, checking=0.0, saving=0.0): self.checking = checking self.saving = saving def __str__(self): """ Return string for Bank with same syntax it could be created. """ return 'Bank(checking={}, saving={})'.format(self.checking, self.saving) def deposit(self, amount): """ deposit amount into checking """ self.checking = self.checking + amount def transfer(self, amount): """ transfer amount from checking to saving """ self.checking = self.checking - amount self.saving = self.saving + amount ---- end file ----
$ python >>> from bank import Bank >>> b = Bank() # create a bank (__init__ is called) >>> b.deposit(100) >>> b.transfer(25) >>> print "My bank is", b # converts to string (__str__ is called)
Are there any class variables here? Instance variables?
What are the advantages of a class over a dictionary?
What are the advantages of a dictionary over a class?

review lists

... and things that we can do with them.
from random import shuffle, choice, randrange things = ["apple", "carrot", "cucumber"] dir(things) # list of methods for lists shuffle(things) # random rearrangement x = choice(things) # pick one ... leave it in list y = things.pop() # get rightmost, and remove it
Practice with this a bit in class.

Another Card example

The homework assignment is posted :
Chapter 11 includes among other things a discussion of lists in classes and lists of instances of a class. (Can you give an example of each?)
I've posted code for (Card, Deck, Hand) that you can use this as a starting point for the homework, or as an example. You'll want to adapt it, adding in other stuff like suits, find a way to compare poker hands, and write an appropriate main() routine.
We'll walk through this code, see what it defines, how to use it, and perhaps make some modifications :
http://cs.marlboro.edu/ courses/ fall2016/python/ notes/ Nov_3
last modified Thursday November 3 2016 10:47 am EDT

attachments [paper clip]

     name last modified size
   countwords.py Nov 3 2016 10:47 am 1.14kB [TXT]words.txt Nov 3 2016 10:47 am 57B