Intro to
Programming
(with Python)

Fall 2015
course
navigation

Tue Sep 22

Objects and Graphics - part 1
We wrote two programs in class : ceaser.py brownian.py I've attached both of the to this page - look at the bottom of the window for them.

homework

First we'll finish talking about the chapter 4 material on strings & go over the homework.
A few of my solutions to the homework problems are in the words folder :
It is possible to do the cipher for strings with lowercase, uppercase, and punction. Even without "if" statements. But it's tricky, and not something I'd expect from you yet. For those interested, it's in the cipher folder.
Discuss any other topics from the assignment as needed.
Our story so far :
Any questions on any of this?

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 # # new python functions: # # from random import randint # randint(low, high) low <= random num <= high # # from time import sleep # sleep(2) do nothing for 2 sec # # 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): dx = randint(-5, 5) # pixels dy = randint(-5, 5) dot.move(dx, dy) sleep(0.1) # in seconds wait = window.getMouse() main()
Color names : http://www.w3schools.com/html/html_colornames.asp
http://cs.marlboro.edu/ courses/ fall2015/python/ notes/ Sep_22
last modified Monday September 19 2016 3:52 pm EDT

attachments [paper clip]

     name last modified size
   brownian.py Sep 22 2015 11:27 am 963B    ceaser.py Sep 22 2015 11:27 am 464B