Operation Manual

only the basics and fails to introduce some of the concepts required for creating useful or interesting programs. The next example,
however, uses some of the basic tools required to make interactive programs in Python.
As in Example 1, begin by opening a new blank document in IDLE or your text editor, and then start the program with the
following shebang line:
#!/usr/bin/env python
As previously discussed, this line isnt strictly necessary unless the program is going to be made executable, but it does no harm
and is a good habit to develop.
Next, add a comment to the program to provide context if you need to open the file at a later date. Note that this needs to be
entered as a single line, as with all code lines that end in a symbol:
# Example 2: A Python program from the
Raspberry Pi User Guide
In Python, anything following a hash symbolwith the exception of the shebang line—is treated as a comment. When a
comment is found, Python ignores it and skips to the next line. Commenting your code is good practice: although you might know
what a particular section of code does now, things might not be so clear when you open the file again six months down the line.
Comments also help make code more maintainable, and if you decide to share your code with other people, your comments help
them understand what each section is supposed to do. For simple programs, its not strictly necessary to include commentsbut
as with adding the shebang line, its a very good habit to get in to. Comments can be on their own line, as with the preceding
comment, or at the end of a line, in which case Python will run the code line up until it reaches the hash symbol.
Next, ask the user for his or her name using the following line:
userName = raw_input(What is your name? )
This small line actually achieves quite a lot. The first part, userName =, tells Python to create a new variablea location for
storing a piece of informationcalled userName. The equals symbol tells Python that the variable should be set to whatever
follows. However, in this case what follows isnt just a piece of information, but another command: raw_input. This is a tool
designed to accept string (text) input from the keyboard, and allows for a message to be printed to the default output so the user
knows what to type. This helps keep the program simplewithout the ability to print a prompt telling the user what to type, a
second line with a print command would be required. Remember to leave a space at the end of the prompt; otherwise, the
users input will begin immediately after the question mark.
When asking the user to type in text, always use raw_input. This provides security that the input command alone does not—if you just use input, a
user may inject his or her own code into your program and have it crash or work contrary to your intentions.
With the user’s name now stored safely in the userName variable, the program can begin to get clever. Welcome the user using
the following line:
print Welcome to the program,, userName
This line demonstrates a secondary function of the print command introduced in Example 1: the ability to print out the contents
of variables. This print command is split into two: the first section prints everything between the two quotation marks, and the
comma tells print that more should be printed to the same line. Simply typing the variable name userName is enough for
Python to know that it should print the contents of that variable, resulting in a message customised to the user’s own name.
An easy way to achieve neat formatting when printing output is to use the .format instruction at the end of a print command. If you’re using .format,
the print line could be as follows instead:
print Welcome, {0}, to this program..format(userName)
This example program is going to take the form of a simple but friendly calculator. Unlike Example 1, it will continue to run until
the user tells it otherwise. This is achieved using a loop, just as in Scratch. Begin the loop by typing the following two lines:
goAgain = 1
while goAgain == 1:
Why ==?