Hardware Reference
In-Depth Information
When you run this program, you should see a message similar to “You are a big turnip” dis-
played. Run the program a few times and you should see a variety of insults, built at random!
You can use the keyboard shortcut F5 to run the program. However, ensure the editor win-
dow containing your program has focus (is active) by clicking on it before pressing F5 so
IDLE knows the correct code to run.
You'll be changing the program to display a personalised message later in this appendix, but
before you do, it's worth examining the code more closely. The following subsections describe
what the different lines of the program do.
Looking at how other people's code works is useful when you're learning to program, and the
World Wide Web is a good source of many examples.
TIP
Variables
Variables are used to store data. Creating a variable is like getting a cardboard box to reserve
some storage space and writing a unique label on it. You put things in the box for storage and
then get them out again later. Whenever you access the box you use its unique label.
Let's start with something simple to illustrate variables:
message = “hello”
The equals sign means assignment , and tells Python to assign (or store) what is on the right-
hand side in the variable named on the left - in this case, the characters h, e, l, l and o are
stored in the variable named message .
Strings
he speech marks (also known as quotation marks in some parts of the world) tell Python
to treat the enclosed characters as a string of letters rather than trying to understand the
word as an instruction.
To display text on the screen, you use the print command followed by what you want dis-
played after it - for example:
message = “hello”
print (message)
 
Search WWH ::




Custom Search