Python Diaries: A Quasi-Newbie (Re-)learns Programming

Send to Kindle

By Sheldon Greaves

pythonShellA 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? 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:

>>> 5*9
>>> 3*9/2

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"
>>> 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 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…

This entry was posted in Computer Science, Education, Mathematics, Programming, Research Tools, Science Education, Tools. Bookmark the permalink.

2 Responses to Python Diaries: A Quasi-Newbie (Re-)learns Programming

  1. GS test says:

    An Old Programmer Learns Python | Citizen Scientists League

  2. Ian says:

    I would not feel too bad about the loss of basic as the language of choice for beginners. A newbie learning python will have a far better introduction to good programming habits and concepts than a newbie learning basic.

Leave a Reply