
P A R T I I I
P R O G R A M M I N G W I T H T H E R A S P B E R R Y P I
176
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
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
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!”
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 12-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 for-
getting to put an end-quote in a
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.
Summary of Contents for A
Page 1: ......
Page 2: ......
Page 3: ...Raspberry Pi User Guide 2nd Edition...
Page 4: ......
Page 5: ...Raspberry Pi User Guide 2nd Edition Eben Upton and Gareth Halfacree...
Page 10: ......
Page 26: ...R A S P B E R R Y P I U S E R G U I D E S E C O N D E D I T I O N 10...
Page 28: ......
Page 29: ...Chapter 1 Meet the Raspberry Pi...
Page 37: ...Chapter 2 Getting Started with the Raspberry Pi...
Page 56: ......
Page 57: ...Chapter 3 Linux System Administration...
Page 79: ...Chapter 4 Troubleshooting...
Page 89: ...Chapter 5 Network Configuration...
Page 109: ...Chapter 6 The Raspberry Pi Software Configuration Tool...
Page 122: ......
Page 123: ...Chapter 7 Advanced Raspberry Pi Configuration...
Page 140: ......
Page 141: ...Chapter 8 The Pi as a Home Theatre PC...
Page 151: ...Chapter 9 The Pi as a Productivity Machine...
Page 160: ......
Page 161: ...Chapter 10 The Pi as a Web Server...
Page 172: ......
Page 173: ...Chapter 11 An Introduction to Scratch...
Page 189: ...Chapter 12 An Introduction to Python...
Page 216: ......
Page 218: ......
Page 219: ...Chapter 13 Learning to Hack Hardware...
Page 234: ......
Page 235: ...Chapter 14 The GPIO Port...
Page 249: ...Chapter 15 The Raspberry Pi Camera Module...
Page 265: ...Chapter 16 Add on Boards...
Page 280: ......
Page 281: ...Appendix A Python Recipes...
Page 287: ...Appendix B Raspberry Pi Camera Module Quick Reference...
Page 293: ...Appendix C HDMI Display Modes...