Intro to
Programming
with Python

Fall 2012
course
navigation

Sep 18

Reminder: the resources page has links to the textbook website, which has downloadable versions of the code in the book and other goodies.
And remember the documentation:
Note that python currently has several different ways to format text. Last week I described the "old" way, with % signs; the python 2.7 docs mostly describe the "new" way, with the "..."format.().
Here's an example of table formatting, using the old syntax: from math import sqrt print " %10s %10s " % ("n", "sqrt(n)") print " %10s %10s " % ("-"*10, "-"*10) for n in range(20): print " %10i %10.4f " % (n, sqrt(n))
And here's the same with the new syntax: from math import sqrt print " {:10} {:10} ".format("n", "sqrt(n)") print " {:10} {:10} ".format("-"*10, "-"*10) for n in range(20): print " {:10} {:10.4f} ".format(n, sqrt(n))

homework

Discuss the homework.
Here's the basic idea of the cipher assignments : word = raw_input("What is the word to encode? (lowercase a-z) ") cipher_key = input("What is the magic number? (1-25) ") code_word = "" for char in word: char_number = ord(chard) - ord('a') # 0 to 26 code_number = (char_number + cipher_key) % 26 # wrap around at 'z' code_letter = chr(code_number + ord('a') code_word = code_word + code_letter print "The coded word is '%s'." % code_word
And here are a few more detailed solutions.
We did this in class : message = raw_input("Enter message: ") cipher_key = input("What is the cipher key? ") code_word = "" for ch in message: print " debug: char = ", ch ascii = ord(ch) print " ascii: ", ascii number = ord(ch) - ord('a') # 0 to 25 print " number: ", number new_number = (number + cipher_key) % 26 print " new number: ", new_number new_char = chr(new_number + ord('a')) print " new char = ", new_char code_word = code_word + new_char print print print "The coded word is '{}'.".format(code_word)

our story so far

where we've been :

chap 5 : exploring "objects" with a graphics API

This is usually a fun week ...

objects

Object oriented programming is a popular paradigm these days. It's not the only one, but it's an important one.
An "object" is a abstration chunk of data (a "thing") with actions (called "methods") it can perform.
A "class" is a kind of object. Example: Point, Circle, GraphWin
An "instance" is a specific object: Example: that red circle right there.
In python, creating an instance is done this way : p = Point(10, 20) # instance_variable = ClassName(argument1, arg2)
And once we have one, we can tell it do things like this: p.move(2, 3) # change position (x,y)=(10,20) to (12,23)
An "API" (Application Programmer's Interface) is a description of which sorts of objects you can create, and what methods they have.
All this is pretty abstract, so let's see a concrete example with the attached files.

graphics.py

See the resources or Zelle's book page for both (1) the file you'll need to run this stuff (put in in the same directory), and (2) the API of what you can do with it.
It's possible that your python installation doesn't yet have the right system stuff - namely tkinter - to run Zelle's graphics library. So if this doesn't work for you, talk to Sam or Jim about getting things running.
You'll need the graphics.py module (i.e. file) from Zelle. And it needs to be somewhere where your python can find it, typically in the same folder as the python file you're running. # You'll need to do this to use Zelle's graphics: from graphics import * # If that doesn't work, make sure you have his graphics.py file. # Then make sure you're running python in the folder with graphics.py. # You can see what the folder is with : import os print "The current directory is ", os.getcwd() # And you can change the folder with os.chdir(full_path_to_a_folder)
With that stuff running, we'll explore it interactively class, doing things like this :
... and so on
Tic Tac Toe, anyone ?

an example

# wiggle.py # # a demo of Zelle's graphic library # # This uses two new python functions, randint and sleep, # in addition to the graphic object API : # # from random import randint # randint(low, high) # low <= random num <= high # # from time import sleep # sleep(2) # do nothing for 2 sec # # Clicking in the window once it's stopped will quit cleanly. # Or type "control-C" at the command prompt to interrupt it. # # Jim M | Sep 2012 | MIT License from graphics import * from time import sleep from random import randint def main(): window = GraphWin("wiggle", 300, 300) dot = Circle(Point(150,150), 20) dot.setFill("red") dot.draw(window) for i in range(100): sleep(0.1) # in seconds dx = randint(-5, 5) # in pixels dy = randint(-5, 5) dot.move(dx, dy) wait = window.getClick() main()
For a list of what what color names are allowed, see the standard unix/web ones at e.g. http://www.w3schools.com/html/html_colornames.asp .
Exercise : Can you trace the path of the red circle? (One way is given in the attached wiggle2.py)

Are we having fun yet?
http://cs.marlboro.edu/ courses/ fall2012/python/ notes/ Sep_18
last modified Monday September 23 2013 9:26 pm EDT