[Tutor] Learning programming

Tim Peters tim_one@email.msn.com
Sat, 3 Apr 1999 13:17:04 -0500


[John Kleinjans, seeking a way to teach programming]

John, like everyone else so far, I recommend you use one of the Python IDEs
(Integrated Development Enviornments).  Any will do!  PythonWin and Mac IDE
were covered in some detail, so I'll run thru it for IDLE on Win95/98/NT:

> 1) get a windowing environment running

Starting with 1.5.2, the Python Windows installer also (but optionally)
installs Tcl/Tk.  That's a very popular cross-platform windowing system, and
IDLE builds on top of it.

> 2) get Python running in one window

IDLE comes with 1.5.2 too, so it's a matter of starting IDLE.  Note that the
Windows installer creates a "python" entry under Start -> Python.  That's
enough to bring up a DOS box Python.  I change its properties by hand to
bring up IDLE instead.  Or create a desktop shortcut, etc -- each platform
has a bunch of shortcuts specific to it.

> 3) get a text editor running in another window

IDLE is also a (simple but reasonably powerful) editor, and any number of
files can be opened and/or created from its File menu, each in its own
independent window.

> 4) with the text editor, write your program (call me "prog")

Piece o' cake.  Any of the IDEs will have special editor support for Python
too, from automatic indentation to syntax coloring.

> 5) save your program to disk
>     a) in the Python directory
>     b) with a ".py" extension

File -> Save (or hit Ctrl+S).  The standard Windows "Save As" dialog box
appears; unless it's the first time they've turned on their computer,
they've seen this dialog a thousand times before <wink>.

> 6) switch to the Python window and
> >>> import prog

Easier in IDLE to do Edit -> Run module, from the editor window.
Unfortunately, if you *haven't* saved the file before doing this, it doesn't
save the file for you, but instead pops up an error box saying "Please save
first!".  At least it's self-explanatory <wink>.

> 7) at this point,
>     a) your program might actually run
>     b) you'll probably get some error messages
> 8) switch back to the editor window

When you get an error msg from Python, you also get a traceback.  The
traceback includes the appropriate file names and line numbers.  IDLE knows
how to parse these lines, so you just need to point at the error,
right-click, and select "Go to file/line" from the pop-up context menu.
IDLE opens the file (or switches to its window if it's already open),
positions the cursor on the offending line, and gives that window the focus.
Very handy!  Again, all the IDEs have something like this built in.

> 9) edit the program. Try to fix it.
> 10) SAVE IT TO DISK AGAIN
> 11) switch back to Python and type
> >>> reload(prog)

This is reduced to steps 5 & 6 again.  IDLE's "Run module" knows whether or
not it's the first time the module has been run, and magically does a
"reload" if it's not the first time.

> ...
> Beginning programmers would take a very long time to figure this out.
> They'd probably quit first.

Self-selection at work:  then they don't have what it takes to become "real
programmers" <0.5 wink>.  No way around it, computers are still a pain in
the ass to use, and it requires a certain pig-headedness to succeed.  An IDE
will take away much of the needless tedium.  In return, each has its own
quirks to stumble into and worm around.  Very much a net win, though.

> ...
> So now, of course, the newbie has to write a program like ...
>
> print "Hello world\n"
>
> ... and get that running. And that is the big hurdle for someone who is
> learning how to program for the very first time. Even in Python.

D:\Python>python
Python 1.5.2b2 (#0, Feb 16 1999, 17:09:09) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> print "Hello world"
Hello world
>>>

That is,

1) Start with interactive mode.

2) Anything hard to learn in that sequence is due to the OS, and can't
possibly be made simpler without replacing the OS -- the clumsiness of a DOS
box under Windows isn't Python's fault, nor is it something Python (or any
other program) can do anything about.

3) Your program was far too elaborate:  Python supplies the newline for you
<wink>.

> Python looks like a good OO language.

I expect it's *much* better for teaching OO than is C++; the latter is a
"efficiency at any cost" language that's most appropriate for demanding
professional use (as a professional C++ programmer, I'm allowed to say that
<wink>).  I don't know of any language (& I know of a lot) in which OO is
easier to "get going" than in Python, although there are several languages
in which OO plays a more central role.  Eiffel in particular is a sparkling
jewel in the OO world, but supports a theory of programming that's probably
too heavy for high-school level.  I'm not an educator, but Python is what
I'd pick if I were you.  Java is another good choice for teaching OO, in
some ways better, in others worse.

> I'd like to learn Python and OO thinking/programming, too.

If you have the source distribution, I'd recommend studying
Demo/classes/Dates.py.  I wrote that in 1993 to try out Python's then-new
operator overloading features, and many people have written to say they've
found it especially helpful for "getting into" OO.  I'm not sure why, except
that it's mostly a procedural module with a simple OO interface; and
calendar trivia is inherently fascinating to many ("what day of the week
will my birthday fall on in the year 1 billion?").  The math is subtler than
it looks, but you're not going to find any simpler calendar algorithms (this
is *much* simpler than most), and 95% of it is junior-high level integer
arithmetc.

for-a-real-education-try-rewriting-dates.py-in-c++<wink>-ly y'rs  - tim