Hardware Reference
In-Depth Information
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 try 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)
his will display the contents of the variable message on the screen, which in this case is
hello .
If, on the other hand, you enter the following code:
print (“message”)
he word message will be displayed on the screen, because the speech marks tell Python to
treat text within them as a string of characters and not a variable name.
print is slightly confusing in that it displays characters on the screen and has nothing to do
with sending it to a printer to appear on paper.
Lists
To store multiple pieces of data in Python together, you can use lists. Lists are speciied as items
separated by commas within square brackets. Reconsidering the example of the cardboard box, a
list can be considered as a named box with internal dividers to store separate items.
Looking back at the insult generator code, lists of strings are used to store multiple adjectives
and nouns. Because you now know about strings and lists, you can try adding some more words
of your own. Remember to enclose them in quotes ( “” ) and separate them with a comma.
Functions
A function can be thought of as a little machine that may take an input, perform some sort of
processing on it and then produce an output (called its return value) as shown in Figure 2-7.
 
Search WWH ::




Custom Search