Sep 20
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 :
- Creating *.py files with an editor.
- Running python from the command line.
- Finding online documentation.
- text input and output : interactive and files
- variables (x, y, this_thing) contain values (1, 2.3, "hello")
- lists [2, "hello"] and strings, and ways to manipulate them
- operations that combine types (i.e. '-'*10)
- built-in functions like len(that_string)
- modules with more functions like import math; math.sqrt(5)
- invoking "methods" with a period, i.e. name.lower()
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 Dylan 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 :
- create a window
- with given size
- with given background
- create a circle
- give it a color
- draw it
- move it
- animate it
- "from time import sleep"
- "import random; r = Random(); r.randint(low,high)"
- get a mouse click
- draw an image from a .jpeg file
... 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()
aside : color
Discuss the model of color that computers use :
- additive (light) not pigment (paint)
- why 3 primary colors?
- corners of color cube
- 1 byte per color, i.e. a number (0 to 255) in base 10 or (00 to FF) in hex.
- go through the names for each corner of the "color cube"
practice in class
If there's time :
- Modify the wiggle.py to show brownian motion, jiggling a point and drawing a line behind it.
- Draw rectangles showing ascii colors
- Draw a spiral
- Draw a math function like y = x**2