05-07-2012, 09:41 AM
A Byte of Python
A Byte of Python.pdf (Size: 337.07 KB / Downloads: 17)
History Lesson
I first started with Python when I needed to write an installer for my software Diamond
[http://www.g2swaroopsoftware/] so that I could make the installation easy. I had to choose
between Python and Perl bindings for the Qt library. I did some research on the web and I came across
an article where Eric S. Raymond, the famous and respected hacker, talked about how Python has become
his favorite programming language. I also found out that the PyQt bindings were very good compared
to Perl-Qt. So, I decided that Python was the language for me.
Then, I started searching for a good book on Python. I couldn't find any! I did find some O'Reilly books
but they were either too expensive or were more like a reference manual than a guide. So, I settled for
the documentation that came with Python. However, it was too brief and small. It did give a good idea
about Python but was not complete. I managed with it since I had previous programming experience, but
it was unsuitable for newbies.
Introduction
Python is one of those rare languages which can claim to be both simple and powerful. You will find
that you will be pleasantly surprised on how easy it is to concentrate on the solution to the problem
rather than the syntax and structure of the language you are programming in.
The official introduction to Python is
Python is an easy to learn, powerful programming language. It has efficient high-level
data structures and a simple but effective approach to object-oriented programming.
Python's elegant syntax and dynamic typing, together with its interpreted nature, make
it an ideal language for scripting and rapid application development in many areas on
most platforms.
Why not Perl?
If you didn't know already, Perl is another extremely popular open source interpreted programming language.
If you have ever tried writing a large program in Perl, you would have answered this question yourself!
In other words, Perl programs are easy when they are small and it excels at small hacks and scripts to
'get work done'. However, they quickly become unwieldy once you start writing bigger programs and I
am speaking this out of experience of writing large Perl programs at Yahoo!
When compared to Perl, Python programs are definitely simpler, clearer, easier to write and hence more
understandable and maintainable. I do admire Perl and I do use it on a daily basis for various things but
whenever I write a program, I always start thinking in terms of Python because it has become so natural
for me. Perl has undergone so many hacks and changes, that it feels like it is one big (but one hell of a)
hack. Sadly, the upcoming Perl 6 does not seem to be making any improvements regarding this.
Choosing an Editor
Before we move on to writing Python programs in source files, we need an editor to write the source
files. The choice of an editor is crucial indeed. You have to choose an editor as you would choose a car
you would buy. A good editor will help you write Python programs easily, making your journey more
comfortable and helps you reach your destination (achieve your goal) in a much faster and safer way.
Using a Source File
Now let's get back to programming. There is a tradition that whenever you learn a new programming
language, the first program that you write and run is the 'Hello World' program - all it does is just say
'Hello World' when you run it. As Simon Cozens 1 puts it, it is the 'traditional incantation to the programming
gods to help you learn the language better' .
How It Works
Let us consider the first two lines of the program. These are called comments - anything to the right of
the # symbol is a comment and is mainly useful as notes for the reader of the program.
Python does not use comments except for the special case of the first line here. It is called the shebang
line - whenever the first two characters of the source file are #! followed by the location of a program,
this tells your Linux/Unix system that this program should be run with this interpreter when you execute
the program. This is explained in detail in the next section. Note that you can always run the program on
any platform by specifying the interpreter directly on the command line such as the command python
helloworld.py .