Game Development Reference
In-Depth Information
the value 15 inside the variable spam . But when we enter spam = 3 , the value 15 is
replaced, or overwritten, with the value 3 . Now, when we enter spam + 5 , the expression
evaluates to 8 because the value of spam is now 3.
To find out what the current value is inside a variable, just enter the variable name into
the shell.
Now here's something interesting. Because a variable is only a name for a value, we can
write expressions with variables like this:
>>> spam = 15
>>> spam + spam
30
>>> spam - spam
0
>>>
When the variable spam has the integer value 15 stored in it, entering spam + spam
is the same as entering 15 + 15 , which evaluates to 30 . And spam - spam is the same
as 15 - 15 , which evaluates to 0 . The expressions above use the variable spam twice.
You can use variables as many times as you want in expressions. Remember that Python
will evaluate a variable name to the value that is stored inside that variable, each time the
variable is used.
We can even use the value in the spam variable to assign spam a new value:
>>> spam = 15
>>> spam = spam + 5
20
>>>
The assignment statement spam = spam + 5 is like saying, "the new value of the
spam variable will be the current value of spam plus five." Remember that the variable on
the left side of the = sign will be assigned the value that the expression on the right side
evaluates to. We can also keep increasing the value in spam by 5 several times:
>>> spam = 15
>>> spam = spam + 5
>>> spam = spam + 5
>>> spam = spam + 5
>>> spam
30
 
Search WWH ::




Custom Search