Game Development Reference
In-Depth Information
How are you? Write back to me soon.
Sincerely,
Bob''' # end of the multi-line string and print
statement
print 'P.S. I miss you.' # still inside the
def-block
writeLetter() # This is the first line outside the
def-block.
Constant Variables
You may have noticed that
HANGMANPICS
's name is in all capitals. This is the
programming convention for constant variables.
Constants
are variables whose values do
not change throughout the program. Although we can change
HANGMANPICS
just like any
other variable, the all-caps reminds the programmer to not write code that does so.
Constant variables are helpful for providing descriptions for values that have a special
meaning. Since the multi-string value never changes, there is no reason we couldn't copy
this multi-line string each time we needed that value. The
HANGMANPICS
variable never
varies. But it is much shorter to type
HANGMANPICS
than it is to type that large multi-line
string.
Also, there are cases where typing the value by itself may not be obvious. If we set a
variable
eggs = 72
, we may forget why we were setting that variable to the integer
72
.
But if we define a constant variable
DOZEN = 12
, then we could set
eggs = DOZEN *
6
and by just looking at the code know that the
eggs
variable was set to six dozen.
Like all conventions, we don't
have
to use constant variables, or even put the names of
constant variables in all capitals. But doing it this way makes it easier for other
programmers to understand how these variables are used. (It even can help you if you are
looking at code you wrote a long time ago.)
Lists
I will now tell you about a new data type called a
list
. A list value can contain several
other values in it. Try typing this into the shell:
['apples', 'oranges', 'HELLO
WORLD']
. This is a list value that contains three string values. Just like any other value,
you can store this list in a variable. Try typing
spam = ['apples', 'oranges',
'HELLO WORLD']
, and then type
spam
to view the contents of
spam
.
>>> spam = ['apples', 'oranges', 'HELLO WORLD']
>>> spam