Hardware Reference
In-Depth Information
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 . his line tells the operating
system where it should look for the Python iles. 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 ilename.
To ensure the program runs regardless of where the Python executable is installed, the irst
line of your program should read as follows:
#!/usr/bin/env python
his line tells the operating system to look at the $PATH environment variable—which is
where Linux stores the location of iles that can be executed as programs—for the location of
Python, which should work on any Linux distribution used on the Pi. he $PATH variable
contains a list of directories where executable iles are stored, and is used to ind 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 stan-
dard output device. Enter the following line in your new project:
print “Hello, World!”
he inal 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 difering
shades of grey in the print edition). his 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
Search WWH ::




Custom Search