first experiences with Python

Michele Simionato mis6 at pitt.edu
Sat Jul 6 06:53:48 EDT 2002


I became aware of the existence of Python a couple of months ago. 
The reason was an exceptional wave of heat in Pittsburgh, my problem 
the conversion from Fahrenheits to Celsius (I recently moved from Europe 
to the States and I hate Fahrenheits !).
In the old days, I would have written a two line program in Basic to make
the conversion. Unfortunately (or fortunately ;-) I was using Linux and there 
is no Basic on it. How to do the conversion ? Well, I thought, there should 
be some simple pre-installed language to do this kind of trivial jobs.
Of course, the natural language under Unix-like systems would be C, but I 
immediately discarded this possibility due to my past experience (definitely, 
I don't like C; moreover I don't think it is a good idea to use a difficult 
tool to do a simple task). Therefore I looked at the list of languages 
installed on my machine and I discovered Python:

"Python is an interpreted, interactive, object-oriented programming language"
(quoted from the RPM package description in Red-Hat Linux)

Well, I thought, maybe this is what I want. The fact that it was an interpreted
language suggested simplicity of use (possibly).Moreover, it was documented and
available on any Red Hat Linux system for free, already installed. Then I 
printed the tutorial and at a first look the language seemed quite simple.
But actually it was more than that: it did what I expected ! I pretty soon
discovered it from my first program, the conversion from Fahrenheits to 
Celsius. Since the tutorial (Guido forgive me !) is not well written and lacks 
such an essential thing as documenting the interactive input methods, I was 
forced to guess the sintax. I thought: which is the simplest way I would 
implement an input command, what's the obvious way things should work ? 
Of course

F=input("Input the temperature in Fahrenheits: ") 

Then I tried, but from my past experience with others languages, I was
sure of having troubles ("in computer science, things never work the obvious 
way"): but incredibly it worked since the very first attempt. This never 
happened to me before ! Encouraged by this little success, I solved my next 
problem guessing too: how to print the result in Celsius rounded to an 
integer ? What would be the obvious way, I asked myself ? Of course

print "Temperature in Celsius: ",int(round((F-32)*5/9.0))

if the function round() exists. And actually it is a built-in function and does
what expected. Having a language that did what I expected was unusual and 
excited me very much (I still remember the experience with my first program in 
C, when I wasted a lot of time since I couldn't imagine that to compute 1+1
I explicitly needed to link the math libraries !).
Some time later the wave of heat passed and I decided to give a try to Python
with a less trivial project. An old favorite of mine is a program to manage my 
archive of books, making various kind of histograms (for instance the number of
books read versus the months of the year, the number of books versus the 
genre, etc.). The first version was implemented in AmigaBasic in 1987 (when
I bought my first computer!), followed by a version in Pascal in 1988. Then
for many years I did not program very much, I was too busy with other stuff.
In 1999 I wrote a version for Mathematica, which however left me unsatisfied
for many reasons. I wanted something better. And I found it !
Using Python, I implemented the most effective and sophisticated version
I ever wrote in less than a week (I mean working only in my free time) !
I was really impressed by the power of the language and ever more by its
simplicity of use, particularly comparing my experience with Python with the
experience I had with C.
The enormous advantage of Python, is that you can reuse what you already
know: for instance, to plot the histograms I didn't need to learn a new set 
a commands, I simply used Python to write and execute a gnuplot program. 
Then I used Python to write a latex program which included the gnuplot 
pictures, I added some tables and comments and I invoked latex2html to 
create a Web page with very professional-looking results: and all that in 
an impressively short time ! 

The single feature that impressed me the most, is Python's ability to
write programs that write programs, even in other languages ! But there are 
many other things that I liked, for instance the functions exec() and eval() 
which were new to me (I like this idea of automatic code generation). 
Moreover, I like the import statement that makes it possible for  the user 
to create his own library of functions with no effort. Of course, this is 
an essential feature for a language which has to be scalable to
larger applications. Python is as simple and readable as Basic but as full 
featured as the most powerful languages of the XXI st century. I don't care
about the intrinsic slowness of an interpreted language with respect to a
compiled one, it is of no point for my applications; moreover, Python is not 
really slow since most of the work is done by the powerful routines of the 
standard library: for instance my Python program was dozens of times faster 
than my Mathematica program implementing the same algorithm.
No doubt that programming languages have undergone an enormous progress in
the last years, but unfortunately this progress has been hidden in many 
languages by rather obscure syntaxes and technical complications which
are absent in Python. Finally, one can spend his time in learning concepts, 
not a compiler's syntactical idiosyncrasies!

Anyway, let me stop this praise to Python (people reading comp.language.python 
are already convinced) and let me come to the reason why I am posting this
message. I want to raise a point about Python's accessibility to new users. 
Given for granted that the language per se is good and easy to learn, my
questions are: how good is Python's documentation ? How could the Python 
community better help newcomers ? What are the most difficult things to 
learn and those that should be better explained ? What are the best references 
that should be recommended according to the newcomer's background ?

Some of these questions should be asked to newbies, people who learned Python 
in the last six months, certainly not to experienced pythonists which cannot 
have a fresh look on these matters. For instance, in my personal opinion, the 
Python official tutorial becomes good only when you have already learned the 
language, but this is not what should happen to a tutorial written for 
newcomers. It is true that the Python home-page contains a list of links
to "Python for beginners", but I feel that the official Python tutorial
should be modified and simplified. Or, even better, I'd like to see in the 
official distribution a mini-tutorial for absolute beginners, to be read
before the other tutorial. The reason is that many people (including myself)
prefer to read the official documentation before searching in other places,
especially because the official documentation is supposed to be updated
whereas the unofficial one, who knows ? Therefore I overlooked the
"Python for beginners" links and discovered the hard way that wasn't a 
good idea.

A very simple and readable source for beginners I would recommend is the book 
"How to think as a computer scientist" (the other references in "Python for 
beginners" are good too). It has two features:

1) it is free;
2) it is written to be understood by high school students with no programming 
experience, thus everybody can read it; somebody with experience can absorb 
it in a couple of days.

It is a little long, but I have found myself spending less time in reading
a 200 page simple book with lots of examples that an hypercondensed 90 page 
reference manual with no examples. A little disadvantage is that it is not
updated to Python2.2, unfortunately. I don't know if it will be updated.
After the book I immediately passed to the Python Library Reference, which is 
over 600 hundred pages, but very well written and useful (you don't need to 
read the full reference, only the parts you need for your application, and 
reading the contents will give you a clear understanding of the potentialities 
of the language). Really, I had less trouble in learning from the Library 
Reference than from the tutorial ! 
It seems that the tutorial is written with the aim to wet the appetite, not 
with the aim to teach the language, but in my opinion this should NOT be 
the case for a tutorial.
After having some experience with Python, I looked for the newsgroup.
comp.lang.python is very interesting and helpful, but certainly it is not for
the absolute beginner; however, after few weeks of experience with Python
I have become able to follow the discussion: then I asked a question on Tkinter
such that I could have hardly found the solution by myself and I had a 
precise, terse and effective answer in less than four hours.
Moreover, by reading the postings I learned a lot about more recent features
of Python not documented in the book and which I skipped in the Reference
Library. Definitely, I will suggest to post in comp.language.python as the most
effective way to get help (however, be aware that some questions can start
a flame war ;-).

Summarizing, this is the advice I would give to a newbie (other people can
give their advice or tell their experiences too):

1) skip the tutorial
2) read one of the references in http://www.python.org/doc/Newbies.html
3) read the sections of the Reference Library you are interested in
4) for tricky points, ask comp.lang.python
5) finally, remember that the code is there, look in the standard distribution
programs and learn how professionals write!

I started to appreciate the standard tutorial in between steps 4) and 5):
at this point it is definitely useful. 
Hoping this will be helpful to somebody, 

---
Michele Simionato - Dept. of Physics and Astronomy
210 Allen Hall Pittsburgh PA 15260 U.S.A.
Phone: 001-412-624-9041 Fax: 001-412-624-9163
Home-page: http://www.phyast.pitt.edu/~micheles/



More information about the Python-list mailing list