Hardware Reference
In-Depth Information
This 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.
TIP
Lists
To store multiple pieces of data in Python together, you can use lists. Lists are specified 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.
YOUR TURN!
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 as shown in Figure B-7. You can create your
own functions, or you can use functions that are included in Python or written by other
people. To use a function, you call it by entering its name.
Figure B-7:
Functions are
little machines
that process
inputs to
produce an
output.
 
Search WWH ::




Custom Search