By Sheldon Greaves
A couple of weeks ago I wrote a little editorial lamenting the loss of BASIC and HyperCard, two truly remarkable tools for beginning programmers to write some remarkably cool stuff. I was surprised and somewhat taken aback that the post got quite a bit of traffic. It also generated some interesting feedback. If the comments were any indication, the weight of the programmer community leans toward Python as the worthy successor to an entry-level programming language.
Let it never be said that I canâ€™t take a hint.
Iâ€™m going to give Python a shot. Whatâ€™s more, Iâ€™m going to display, nay, flaunt my ignorance and mistakes as I learn. This is an invitation to my more knowledgeable (and hopefully diplomatic) readers to step in and take part in the teaching/learning process. It is also an object lesson that learning something like this is fraught with mistakes. If itâ€™s like any number of other things Iâ€™ve learned in my life, it requires making a lot of mistakes. Fortunately, Iâ€™m at that lovely point of middle age where a mistake usually isnâ€™t going to leave a gaping hole in my ego.
Since Iâ€™m learning from â€œsquare one,â€ let me specify where that is. I took BASIC and some Pascal around 1981. In 1985 I taught myself HyperCard. I even ended up doing some programming on some real, live educational software a year or two later. In 1986 I had a temp job that gave me an opportunity to learn a little bit about UNIX, but I havenâ€™t used it since, so itâ€™s gone. In 1996 I got to take two courses in C for a job assignment that never panned out, and that language has also remained fallow since then.
I have an installation of Python on my MacBook. Two, actually.Â Version 2.7.2 came with my second-hand Mac, and some time ago I downloaded version 3.2.2 but never got around to playing with it.
There are all kinds of documents and guides to Python out there, which can actually be a bit of a problem: where do you start? Python.org is a good place to begin. There are also some free online courses courtesy of MIT which I have not had an opportunity to look at yet. This introduction to Python looks promising. I started with the documents on the main Python page.
There are two ways to access this language. The first is through a terminal window. On the Mac, this is an application that gives you direct access to the operating system via UNIX. A refresher on UNIX commands will probably be a good idea at some point, but isnâ€™t necessary for now. When you see the command line, just type â€œpythonâ€ (sans quotes) and youâ€™ll see a start-up message, then the >>> prompt that tells you youâ€™re in Python. This is what the documentation calls the â€œinterpreter,â€ meaning that you can type in commands and Python executes them immediately. Type in â€œ3-5â€, press return, and Python spits back â€œ-2.â€
This is a good place to just fiddle with various commands as you encounter them. You can use Python as an ad hoc calculator this way:
Notice that on the second expression, the correct answer is 13.5. Python truncated the result. Iâ€™m sure thereâ€™s a setting or parameter that fixes that. By the way, when using the IDLE Python Shell (see below) you get the correct answer.
Python also lets you assign values to variables on the fly. Apparently it isnâ€™t necessary to declare variables or variable types (i.e., is it an integer, float, string, etc.). Python can figure this out and keep track of data types in the background.
So, for instance:
>>> holy_hand_grenade_of_antioch = 5
>>> holy_hand_grenade_of_antioch + 9
>>>holy_hand_grenade_of_antioch = “dead rabbit”
I assigned a variable, holy_hand_grenade_of_antioch the value of 5, then typed the variable. Python returned the assigned value. I typed it again, adding 9, to show that Python is in fact treating it like a number. I then assigned a string to the same variable, and Python returns a string. (Note: apparently Python is named for Monty Python, and references to its namesake are encouraged. When in Romeâ€¦)
Now the interpreter is great fun for mucking about, but there in the downloadable and installable version one gets from python.org there is something called IDLE, which is a â€œshell.â€ Iâ€™m not totally clear on the distinction between a shell and the interpreter in the terminal window.Â Idle is a more complete environment for working on Python programs. When you start it up you get a window that functions as an interpreter, but also includes a basic interface where you can open or create files for actual python programs. There are also some debugging options plus a few more things I havenâ€™t figured out yet.
Apparently one can do most of this from the terminal window, but you need to understand UNIX paths and directories, which I donâ€™t. Just use IDLE for now. It comes with a couple of short program files you can run just to make sure that everything works. One note: if you open a program file (apparently called â€œmodulesâ€), and run it, the output of the program shows up in the â€œPython Shellâ€ window. My attempt to run some of the programs was hampered by the fact that my module window was on top of the Shell window so I didnâ€™t know the thing was working.
Also, one can edit your python files in a text editor of your choice. In fact, itâ€™s a very good idea.
Next week Iâ€™ll try to distill what Iâ€™ve learned about other basic bits and pieces. By the way, I welcome suggestions as to things I need to learn sooner rather than later, along with explanations, corrections, encouragement, and good spam recipes.
Until next timeâ€¦