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.