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

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.

Output

$ 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.

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 .

Important

Use comments sensibly in your program to explain some important details
of your program - this is useful for readers of your program so that they
can easily understand what the program is doing. Remember, that person
can be yourself after six months!

The comments are followed by a Python statement - this just
prints the text 'Hello World'. The print
is actually an operator and 'Hello World' is referred to as a
string - don't worry, we will explore these terminologies in detail later.

[1]
one of the leading Perl6/Parrot hackers and the author of the amazing
'Beginning Perl' book