Learning to program - The Basics - The Raw Materials - ~Other Collection Types

One common style which we will use a lot is to start variable names with a lower case letter and use a capital letter for each first letter of subsequent words in the name, like this:

aVeryLongVariableNameWithCapitalisedStyle

Python a variable takes the type of the data assigned to it. It will keep that type and you will be warned if you try to mix data in strange ways - like trying to add a string to a number. (Recall the example error message? It was an example of just that kind of error.) We can change the type of data that a variable points to by reassigning the variable.

>>> q = 7 # q is now a number
>>> print q
7
>>> q = "Seven" # reassign q to a string
>>> print q
Seven

Note that q was set to point to the number 7 initially. It maintained that value until we made it point at the character string "Seven". Thus, Python variables maintain the type of whatever they point to, but we can change what they point to simply by reassigning the variable. At that point the original data is 'lost' and Python will erase it from memory (unless another variable points at it too) this is known as garbage collection.

Garbage collection can be likened to the mail room clerk who comes round once in a while and removes any packets that are in boxes with no labels. If he can't find an owner or address on the packets he throws them in the garbage.

Hopefully this brief look at VBScript and JavaScript variables has demonstrated the difference between declaration and definition of variables. Python variables are declared by defining them.

今日はこんなもんにしとくか。