Again thanks. I bought a magazine called
Linux Format. In the mag it walks you through getting a simplified space invader type game up and running. Basically that is the type of thing I want to do.
I am currently working through
Python Tutorial-Guido van Rossum.
I just want to get my "head around arrays, linked lists, array lists and hash tables/maps/trees".
Its not really hard once to understand once you have your head in the right place. Heres a 5 minute Python lesson on the fundamentals:
Variables: a variable is just a clob of data, and the entire state of your program is held and manipulated through variables. If you have IDLE available, type the following:
x = 1[enter]
y = 2[enter]
z = x + y[enter]
z[enter]
IDLE will output something like this:
Code:
>>> x = 1
>>> y = 2
>>> z = x + y
>>> print z
3
x, y, and z are variables. Once you type the line "x = 1", it creates a variable called 'x'.
Once you create a variable, you can do things with it:
Code:
>>> name = "Princess"
>>> if name == "Princess":
print "Hi, Princess"
else:
print "You're not princess"
Arrays: arrays are just collections of related data. For example, how would represent the months of the year? I could do this:
Code:
>>> month1 = "Jan"
>>> month2 = "Feb"
>>> month3 = "Mar"
>>> #if you write code like this, you'll lose your job
That's a really awful way to write code, it becomes a maintenace nightmare. Its better to use an array:
Code:
>>> months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
Once you have an array, you can access each element of the array by its position in the array (or index), starting at 0.
Code:
>>> months[0]
'Jan'
>>> months[0]
'Jan'
>>> months[5]
'Jun'
>>> months[11]
'Dec'
>>> months[12]
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
months[12]
IndexError: list index out of range
You access elements in the array using a pair of brackets. Notice, if you try to access an element that doesn't exist, you get an error as shown above.
Arrays are nice, because you can have arrays of arrays (multidimensional arrays) like this:
Code:
>>> board = [['x','o',' '],
['x','o','x'],
['o',' ','x']]
You access multidimensional arrays just like any other kind of array:
Code:
>>> board[2]
['o', ' ', 'x']
>>> board[2][2]
'x'
The code 'board[2]' returned the third element from the array, which just so happened to give me back another array.
The code '
board[2][2]' actually performs two operations at once. The code in red retrieves an array, and the code in blue access an element from that array. Effectively, it retrieves the third row, third element in the 3x3 array.
You can mix arrays and variables if you like:
Code:
>>> thirdrow = board[2]
>>> print thirdrow[2]
'x'
>>> impossibleBoard = [thirdrow, thirdrow, thirdrow]
>>> print impossibleBoard
[['o', ' ', 'x'], ['o', ' ', 'x'], ['o', ' ', 'x']]
Linked lists: linked lists are kinda like arrays, except you can't access elements in the list by index. This might sound like a limitation, but linked lists have the advantage of allowing users to insert and delete items from the list in O(1) time; inserts and deletes in an array take O

time, where n is the length of the list.
Arrays are useful when the size of your data is of fixed size or requires random access to the data. Linked lists are useful when you need to perform lots of inserts or deletes, and you don't need random access. When you start developing your programming skills a little more, you'll learn about two special types of linked lists called stacks and queues.
Unlike other languages like C# and Java, Python makes no distinction between linked lists and arrays. You use the same bracket syntax for making both:
Code:
>>> orders = []
>>> orders.append("pen")
>>> orders.append("paper")
>>> orders.append("mouse")
>>> print orders
['pen', 'paper', 'mouse']
>>> print orders[2]
mouse
>>> orders.sort()
>>> print orders
['mouse', 'paper', 'pen']
>>> orders.pop()
'pen'
>>> print orders
['mouse', 'paper']
As you can see, the 'append' and 'sort' methods do what they look like they do.
The pop() method isn't obvious to newbies: the function returns the last element in a list, then removes that element from the list. Most newbies are confused by this, because they can't imagine why anyone would ever need a function which does that; you'll be surprised just how often it comes up (appending and popping elements from lists is the basic function of a stack).
Hashtables - a hashtable maps keys to values.
Code:
>>> countriesByUser = {"Princess": "USA", "Powa": "Slovenia", "CFLarsen": "Denmark"}
>>> countriesByUser["Princess"]
'USA'
Hashtables are nice because they have O(1) lookup time. However, you don't have indexed access to elements in a dictionary like you have in an array. Indexed access is useful because of the way that most hashtables are implemented: the order of items in a dictionary is, for all intents and purposes, random.
If the order of your data isn't important and you can conveniently access by key, then a hashtable is a good way to store your data.
Now you're a professional Python programmer.