Hardware Reference
In-Depth Information
Using Python Lists as
Magic.Memory
In all of the programs you have written up until now, you have used some form of
variable to store information that changes as the program runs. Each of these variables
has a name and a value, and if you want your programs to remember more than one
thing, you use more variables.
However, these variables are a fixed size and each can store only one thing (e.g. a num-
ber or a string of text). Many programs you will write as a Minecraft programmer will
need you to store an undefined number of items. You won't always know how many
items you will want to store in a program.
Fortunately, like any modern programming language, Python has a feature called a
list , which can store varying amounts of data as the program runs.
A list is a type of variable in a programming language that can store any number
of items of data. You can add new items to the list, count the length of the list,
access items at particular positions, remove items from anywhere in the list and
do many other things. Lists are a very useful way of storing collections of similar
items that belong together, such as a list of numbers that need sorting, a list of
members in a user group or even a list of blocks in a Minecraft game.
Experimenting with.Lists
The best way to understand lists is to experiment with them in the Python Shell.
Make sure you are careful with the brackets in this section. There are two types
of brackets in use. The round brackets ( ) and the square brackets [ ] . Don't
worry. The difference between these two types of brackets will become clear as
you work through this section.
1. Bring the Python Shell window to the front by clicking on it. Click the mouse to
the right of the last >>> prompt, which is where you will start typing your inter-
active commands. Don't forget that you must stop your existing program run-
ning by choosing Shell Restart Shell from the Python Shell menu, or pressing
CTRL then C. If your previous program is still running, this next section will
not work!
 
Search WWH ::




Custom Search