Pages

Sunday, June 21, 2009

Programming in Python

I've been learning Python stuff. I get a similar "soulcraft" feeling from doing programming that people describe getting from mechanical work. I think the two fields are quite similar on an abstract level; both involve identifying problems, isolating them, forming hypotheses about them testing the hypotheses, fixing, and iterating. A significant difference, though, is that with programming you are in a position to do more original creation, whereas that part is already done with a mechanical object like a motorcycle.

I'm working my way through this free book on programming called "How to Think Like a Computer Scientist; Think Python." It's a nice gentle introduction, but even the "gentle" exercises provide fertile ground for creativity and problem solving. Here's a couple of functions I created for the exercises from chapter four and their graphical results:

def flower(t,n,r):
turnAngle = 360/(n)
angle=(360/(n/2))
for i in range(n):
lt(t,turnAngle)
arc(t,r,angle)
lt(t)
arc(t,r,angle)

def polypie(r,s):
theta=360.0/s
alpha=(180.0-theta)/2
edge=sqrt(r**2+r**2-2*r*r*cos(theta*pi/180))
for i in range (s):
fd(bob,r)
rt(bob,180-alpha)
fd(bob,edge)
rt(bob,180-alpha)
fd(bob,r)
rt(bob,180)


(that one's done four times, moving the turtle to the edge and turning it each time. "bob" is the name of the turtle, btw.)

Not mind-blowing, I realize, but nonetheless the product of quite a bit of thought. It will seem more mind-blowing if you try to come up with the functions from scratch; quite a bit of ingenuity (and (basic) math) is required.

I remember computer classes from when I was a kid that used some predecessor to this little grapical program (TurtleWorld). I remember mostly being irritated with it... having to type commands and so on. I wish someone had taught me control structures, back in the day.

No comments: