A Byte of Python

Using a Source File

Now, let us 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 but it teaches you a lot on how to use the tools of that language. As Simon Cozens [1] says, it is the 'traditional incantation to the programming gods to help you learn the language better' :-).

In DrPython, click on File -> New. If you prefer the keyboard, press Ctrl-N. Enter the program as shown below.

#!/usr/bin/env python
# Filename: helloworld.py
print 'Hello World'

Click on File -> Save. If you prefer the keyboard, press Ctrl-S. Enter the name of the file as helloworld.py and store it in a folder of your choice.

Output

To run the program, click on Program -> Run. If you prefer the keyboard, press Ctrl-E.

Note

If the Run option is not enabled, then it probably means you have an interpreter already running. Click on Program -> End or press Ctrl-D to stop the interpreter.

If you get an error, check whether you have typed the program exactly as shown above and try again. Be careful about use of spaces and tabs in your file.

You can also run the same program in the shell using the python command:

$ python helloworld.py 
Hello World 

Note that, in the shell, you will have to switch to the directory containing helloworld.py for the above command to work.

How It Works

Let us consider the first two lines of the program. You will notice the # symbol at the start of the lines. In Python, any text to the right of the # symbol are considered to be comments. Comments are optional but are useful as notes for the readers of the program.

However, the first line is a special case:

#!/usr/bin/env python

The first two characters in this line, and in this file, are # and ! respectively. This is called the she-bang line. This line allows you to tell your Linux/BSD system to run the given source file with the interpreter that we specify in that line. This is explained in detail in the next section. Although the usage of the she-bang line is specific for Linux/BSD systems, it is considered good practice to include this line even if you are using only Windows for your programs.

We use the /usr/bin/env command (env is short for environment) to find the python command for us and then our script is run with that command. We could have directly specify the location of the python command as #!/usr/bin/python, but some systems have the Python command located at /usr/local/bin/python. Using env allows us to avoid such problems and make our programs more portable (portable means allowing a program to run on different kinds of systems without requiring changes).

You can always specify the command first and then the source file by running python helloworld.py. This method works across platforms.

Important

Writing good comments is important because it helps other readers of your program to understand the program easily. Good comments should concentrate on what the program is doing rather than how the program works (which is explained by the code itself).

Remember that six months after you have written your programs, your comments will help you immensely to recollect how the program is working!

Following the two comment lines, the third line is a Python statement. The print statement is supplied the text 'Hello World' which is promptly printed to the screen.



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