Operation Manual
Choosing IDLE 3 from the Programming menu instead of IDLE loads the Python 3.0 version. This includes features not available in the version of
Python used by IDLE, but none of those features are used in this chapter. You can safely load either version, and these examples will still work.
It’s good practice to start all Python programs with a line known as a shebang, which gets its name from the # and ! characters
at the beginning of the line. This line tells the operating system where it should look for the Python files. Although this is not
entirely necessary for programs that will be run from within IDLE or will call Python explicitly at the terminal, it is required for
programs that are run directly by calling the program’s filename.
To ensure the program runs regardless of where the Python executable is installed, the first line of your program should read as
follows:
#!/usr/bin/env python
This line tells the operating system to look at the $PATH environment variable—which is where Linux stores the location of files
that can be executed as programs—for the location of Python, which should work on any Linux distribution used on the Pi. The
$PATH variable contains a list of directories where executable files are stored, and is used to find programs when you type their
name at the console or in a terminal window.
To achieve the goal of printing out a message, you should use Python’s print command. As its name suggests, this command
prints text to an output device—by default, to the console or terminal window from which the program is being executed. Its
usage is simple: any text following the word print and placed between quotation marks will be printed to the standard output
device. Enter the following line in your new project:
print “Hello, World!”
The final program should look like this:
#!/usr/bin/env python
print “Hello, World!”
If you’re creating the example program in IDLE rather than a plain text editor, you’ll notice that the text is multicoloured (see
Figure 11-2, where colours are represented as differing shades of grey in the print edition). This is a feature known as syntax
highlighting, and is a feature of IDEs and the more-advanced text editing tools. Syntax highlighting changes the colour of sections
of the text according to their function, in order to make the program easier to understand at a glance. It also makes it easy to
spot so-called syntax errors caused by forgetting to put an end-quote in a print command or forgetting to comment out a
remark. For this short example, syntax highlighting isn’t necessary—but in larger programs, it can be an invaluable tool for finding
errors.
Figure 11-2: Syntax highlighting in IDLE