Intro to
Programming
(with Python)

Fall 2015
course
navigation

Nov 5

I have very few homework submissions for the last few weeks. Please do turn in something each time, even if it's only part of the assignment.
Questions about anything?

in class : word_count.py

Write in class a program to count the words in the file, as described at the end of Tuesday's notes.

implementing a "bank" with a dictionary

Here's another example of a dictionary.
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.

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 :
A version with a couple of doctests is attached.
http://cs.marlboro.edu/ courses/ fall2015/python/ notes/ Nov_5
last modified Wednesday November 2 2016 11:20 pm EDT

attachments [paper clip]

     name last modified size
   count_words.py Nov 5 2015 11:39 am 1.40kB