Game Development Reference
In-Depth Information
2 - The Interactive Shell
>>>
Overwriting Variables
Changing the value stored inside a variable is easy. Just perform another assignment
statement with the same variable. Look what happens when you enter the following code
into the interactive shell:
>>> spam = 42
>>> print(spam)
42
>>> spam = 'Hello'
>>> print(spam)
Hello
Initially, the
spam
variable had the integer
42
placed inside of it. This is why the first
print(spam)
prints out
42
. But when we execute
spam = 'Hello'
, the
42
value is
tossed out of the variable and forgotten as the new
'Hello'
string value is placed inside
the
spam
variable.
Replacing the value in a variable with a new value is called
overwriting
the value. It is
important to know that the old value is permanently forgotten. If you want to remember this
value so you can use it later in your program, store it in a different variable before
overwriting the value:
>>> spam = 42
>>> print(spam)
42
>>> oldSpam = spam
>>> spam = 'Hello'
>>> print(spam)
Hello
>>> print(oldSpam)
42
In the above example, before overwriting the value in
spam
, we store that value in a
variable named
oldSpam
.
Using More Than One Variable
When we program we won't always want to be limited to only one variable. Often we'll
need to use multiple variables.