"circle-journal" is a (edited) transcript of an immediate mode Python session to illustrate what we mean by saying that Definition: A circle is a plane curve of constant curvature. First, check that your computer has a working Python installation. Open a command shell and enter the phrase following the dollar sign. The output follows that line. >>> is the prompt from Python to enter another line. My comments begin at the left without symbols. Invoke Python $python Python 2.4.3 (#1, Jan 10 2007, 12:05:23) [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import turtle There is no response by Python to this command. You have told Python to use the "turtle" libraries (in Pythonspeak, this is called the "turtle module". If do get a reply from Python here, then Python cannot find this module. And you cannot continue this lesson, until you "install" this module. This is another story. To see the vocabulary of "turtle" entger this. The phrase "dir" stands for "directory". >>> dir(turtle) ['Error', 'Pen', 'RawPen', 'Tkinter', '__builtins__', '__doc__', '__file__', '__name__', '_canvas', '_getpen', '_pen', '_root', 'acos', 'asin', 'atan', 'atan2', 'backward', 'ceil', 'circle', 'clear', 'color', 'cos', 'cosh', 'degrees', 'demo', 'down', 'e', 'exp', 'fabs', 'fill', 'floor', 'fmod', 'forward', 'frexp', 'goto', 'heading', 'hypot', 'ldexp', 'left', 'log', 'log10', 'modf', 'pi', 'position', 'pow', 'radians', 'reset', 'right', 'setheading', 'setx', 'sety', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tracer', 'up', 'width', 'window_height', 'window_width', 'write'] It is neither necessary nor advisable to understand all of these. You can, of course, experiment with any of the words which don't begin with a double underscore. The ones we're interested in are "forward, right, reset" for this lesson. >>> turtle.forward(50) The turtle module now opens a screen on your computer. That is, it invokes another module called "Tkinter" which "knows" how to draw in the plane. Moreover, we want to abbreviate the command to something simpler. We can write an alias for the words, but not for the parentheses. The latter indicate the the words refer to a function, and the input to the function, in this case, was 50. >>> fd = turtle.forward >>> fd(50) >>> turtle.right(90) >>> fd(50) >>> rt=turtle.right >>> rt(90) >>> fd(50) >>> rt(90) >>> fd(50) Well, wasn't that fun? But tedious. Let's check the directory for a nother useful function, one that puts everything back to the beginning. >>> dir(turtle) Same output as before. >>> turtle.reset() Aha, that's what we want. Let's call it "zap" for short/ >>> zap=turtle.reset We next "define" our own function to draw a square. We'll call it "square" and we insert a placeholder for the side-length of the square. The word "dis" can be anything you like, but best not to confuse Python by using one of the words already in its vocabulary. When you end the line with a colon, Python presents you with another prompt, the tribple dots. There you MUST indent a number of spaces. Don't use (TAB), though you can. Here I used my usual two spaces. One space is too short to see, more spaces is a waste of effort. To tell Python you're done writing, enter a blank line. Python will return to the standard >>> prompt. >>> def square(dis): ... fd(dis) ... rt(ang) ... fd(dis) ... rt(ang) ... fd(dis) ... rt(ang) ... fd(dis) ... rt(ang) ... >>> square(96) Always check that something you've just defined does what you expect it to do. We now speed up. Here is a loop. Note that loop counter, a dummy variable here called "i" because it's easy to type, takes on five values: 0,1,2,3,4. We do not use it for anything. We just want five loops. Clearly 4 would have sufficed. >>> for i in range(5): ... fd(dist) ... rt(ang) ... Now we are ready to do some serious programming. >>> zap() A polygon has three attributes, number of sides, lenght of side, and angle. >>> def poly(nn, dis, ang): ... for i in range(nn): ... fd(dis) ... rt(ang) ... Can you write out in words what this program does? It's easier to see it do it, isn't it? >>> poly(4,96,90) >>> poly(8,43,45) You are welcome to double the number of sides, and halve the side lenght and turning angle. But you'll get a cramp typing. Let's do this: >>> num=4 >>> dist=96 >>> angl=90 >>> zap() >>> poly(num,dist,angl) >>> poly(2*num,dist/2,angl/2) >>> poly(5*num,dist/5,angl/5) What's going on here? Write a short essay into your journal on this experiments.