Pages

Saturday, December 12, 2009

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' :) .
Start your choice of editor, enter the following program and save it as helloworld.py
Example 3.2. Using a Source File
#!/usr/bin/python
# Filename : helloworld.py
print 'Hello World'
(Source file: code/helloworld.py)
Run this program by opening a shell (Linux terminal or DOS prompt) and entering the command python
helloworld.py. If you are using IDLE, use the menu Edit -> Run Script or the keyboard shortcut
Ctrl-F5. The output is as shown below.


$ python helloworld.py
Hello World

If you got the output as shown above, congratulations! - you have successfully run your first Python program.
In case you got an error, please type the above program exactly as shown and above and run the program
again. Note that Python is case-sensitive i.e. print is not the same as Print - note the lowercase p in
the former and the uppercase P in the latter. Also, ensure there are no spaces or tabs before the first character
in each line - we will see why this is important later.

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.


One of the very basic requirements is syntax highlighting where all the different parts of your Python
program are colorized so that you can see your program and visualize its running.
If you are using Windows, then I suggest that you use IDLE. IDLE does syntax highlighting and a lot
more such as allowing you to run your programs within IDLE among other things. A special note: don't
use Notepad - it is a bad choice because it does not do syntax highlighting and also importantly it does
not support indentation of the text which is very important in our case as we will see later. Good editors
such as IDLE (and also VIM) will automatically help you do this.
If you are using Linux/FreeBSD, then you have a lot of choices for an editor. If you are an experienced
programmer, then you must be already using VIM or Emacs. Needless to say, these are two of the most
powerful editors and you will be benefitted by using them to write your Python programs. I personally
use VIM for most of my programs. If you are a beginner programmer, then you can use Kate which is
one of my favorites. In case you are willing to take the time to learn VIM or Emacs, then I highly recommend
that you do learn to use either of them as it will be very useful for you in the long run.
If you still want to explore other choices of an editor, see the comprehensive list of Python editors
[http://www.python.org/cgi-bin/moinmoin/PythonEditors] and make your choice. You can also choose
an IDE (Integrated Development Environment) for Python. See the comprehensive list of IDEs that support
Python [http://www.python.org/cgi-bin/moinmoin/IntegratedDevelopmentEnvironments] for more
details. Once you start writing large Python programs, IDEs can be very useful indeed.
I repeat once again, please choose a proper editor - it can make writing Python programs more fun and
easy.

Using the interpreter prompt


Start the intepreter on the command line by entering python at the shell prompt. Now enter print
'Hello World' followed by the Enter key. You should see the words Hello World as output.
For Windows users, you can run the interpreter in the command line if you have set the PATH variable
appropriately. Alternatively, you can use the IDLE program. IDLE is short for Integrated DeveLopment
Environment. Click on Start -> Programs -> Python 2.3 -> IDLE (Python GUI). Linux users can use
IDLE too.
Note that the <<< signs are the prompt for entering Python statements.
Example 3.1. Using the python interpreter prompt
$ python
Python 2.3.4 (#1, Oct 26 2004, 16:42:40)
[GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'hello world'
hello world
>>>
Notice that Python gives you the output of the line immediately! What you just entered is a single Python
statement. We use print to (unsurprisingly) print any value that you supply to it. Here, we are
supplying the text Hello World and this is promptly printed to the screen.
How to quit the Python prompt
To exit the prompt, press Ctrl-d if you are using IDLE or are using a Linux/BSD shell. In case
of the Windows command prompt, press Ctrl-z followed by Enter.

Installing Python



For Linux/BSD users
If you are using a Linux distribution such as Fedora or Mandrake or {put your choice here}, or a BSD
system such as FreeBSD, then you probably already have Python installed on your system.
To test if you have Python already installed on your Linux box, open a shell program (like konsole or
gnome-terminal) and enter the command python -V as shown below.
$ python -V
Python 2.3.4
Note
$ is the prompt of the shell. It will be different for you depending on the settings of your OS,
hence I will indicate the prompt by just the $ symbol.
If you see some version information like the one shown above, then you have Python installed already.
However, if you get a message like this one:
$ python -V
bash: python: command not found
then, you don't have Python installed. This is highly unlikely but possible.
In this case, you have two ways of installing Python on your system.
• Install the binary packages using the package management software that comes with your OS, such
as yum in Fedora Linux, urpmi in Mandrake Linux, apt-get in Debian Linux, pkg_add in FreeBSD,
etc. Note that you will need an internet connection to use this method.
Alternatively, you can download the binaries from somewhere else and then copy to your PC and install
it.
• You can compile Python from the source code [http://www.python.org/download/] and install it. The
compilation instructions are provided at the website.
For Windows Users
Visit Python.org/download [http://www.python.org/download/] and download the latest version from
this website (which was 2.3.4 [http://www.python.org/ftp/python/2.3.4/Python-2.3.4.exe] as of this writing.
This is just 9.4 MB which is very compact compared to most other languages. The installation is
just like any other Windows-based software.

Caution
When you are given the option of unchecking any optional components, don't uncheck any!
Some of these components can be useful for you, especially IDLE.
An interesting fact is that about 70% of Python downloads are by Windows users. Of course, this doesn't
give the complete picture since almost all Linux users will have Python installed already on their systems
by default.
Using Python in the Windows command line
If you want to be able to use Python from the Windows command line, then you need to set the
PATH variable appropriately.
For Windows 2000, XP, 2003 , click on Control Panel -> System -> Advanced -> Environment
Variables. Click on the variable named PATH in the 'System Variables' section, then select
Edit and add ;C:\Python23 (without the quotes) to the end of what is already there. Of course,
use the appropriate directory name.
For older versions of Windows, add the following line to the file C:\AUTOEXEC.BAT :
'PATH=%PATH%;C:\Python23' (without the quotes) and restart the system. For Windows
NT, use the AUTOEXEC.NT file.

Wednesday, November 11, 2009

What Programmers Say


You may find it interesting to read what great hackers like ESR have to say about Python:
• Eric S. Raymond is the author of 'The Cathedral and the Bazaar' and is also the person who coined
the term 'Open Source'. He says that Python has become his favorite programming language
[http://www.linuxjournal.com/article.php?sid=3882]. This article was the real inspiration for my first
brush with Python.
• Bruce Eckel is the author of the famous 'Thinking in Java' and 'Thinking in C++' books. He says
that no language has made him more productive than Python. He says that Python is perhaps the
only language that focuses on making things easier for the programmer. Read the complete interview
[http://www.artima.com/intv/aboutme.html] for more details.
• Peter Norvig is a well-known Lisp author and Director of Search Quality at Google (thanks to
Guido van Rossum for pointing that out). He says that Python has always been an integral part of
Google. You can actually verify this statement by looking at the Google Jobs
[http://www.google.com/jobs/index.html] page which lists Python knowledge as a requirement for
software engineers.
• Bruce Perens is a co-founder of OpenSource.org and the UserLinux project. UserLinux aims to create
a standardized Linux distribution supported by multiple vendors. Python has beaten contenders
like Perl and Ruby to become the main programming language that will be supported by UserLinux.

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.
The only and very significant advantage that I feel Perl has, is its huge CPAN [http://cpan.perl.org] library
- the Comprehensive Perl Archive Network. As the name suggests, this is a humongous collection
of Perl modules and it is simply mind-boggling because of its sheer size and depth - you can do virtually
anything you can do with a computer using these modules. One of the reasons that Perl has more libraries
than Python is that it has been around for a much longer time than Python. Maybe I should suggest a
port-Perl-modules-to-Python hackathon on comp.lang.python
[http://groups.google.com/groups?q=comp.lang.python] :)
Also, the new Parrot virtual machine [http://www.parrotcode.org] is designed to run both the completely
redesigned Perl 6 as well as Python and other interpreted languages like Ruby, PHP and Tcl. What this
means to you is that maybe you will be able to use all Perl modules from Python in the future, so that
will give you the best of both worlds - the powerful CPAN library combined with the powerful Python
language. However, we will have to just wait and see what happens.