16
Feb 12

Data Structures

This tutorial is an introduction to python data structures.  We will go through the basics of tuples, lists, dictionaries etc...

Tuples:

Tuples are easiest to think of as a constant array.  Once a tuple is set it's members cannot be changed.  These don't seem to make a lot of sense at first, but in more advanced applications (wxPython) they are passed around so it is important to be familiar with them.  They are created by assigning a variable to a list of items enclosed in parentheses.

 

Once a tuple is created they are accessed just like arrays.  The number in braces ([]) corresponds to the displacement or item number.  Items are are placed in a tuple starting at zero and going up.  In our example this means that the number 1 (first item) is referenced by a zero, the number two by a 1 etc...  A -1 will get the item at the end of the tuple and a number : number will get a slice.  A slice is a portion of a list.  In this example our slice gets the numbers in positions 1,2 and 3 but not 4.  When you specify a range the value in the position of the number on the left of the colon is included, but not the number to the right.  The slice goes up to that number, but does not include it.  There are other ways to slice up and separate the values of tuple (and other data structures) that are not covered  here.

Lists:

Lists are very similar to tuples in that that are a list of items, but values in a list can be modified.  Lists are also denoted with braces rather than parentheses.  The same relationship regarding selecting items in a tuple carries over to lists.

 

As you can see the same rules for accessing items in a tuple apply to a list.  The reason lists are so much more usable is because their values can be modified as shown below.  Also, keep in mind that these values can be of any data type.  This applies to tuples as well as dictionaries which we will get to shortly.

 

Dictionaries:

Dictionaries are very similar to normal arrays, but instead of being accessed by a number representing an element in the array a name (string) or letter (char) could also be used.  Lets just go through some examples.

 

In this example we create a dictionary holding 3 different values as follows: 57, 23 and 11.  In a normal array, these values would be accessed by an instruction like dict1[0], or dict1[2].  Instead, we access dictionaries by writing dict1['value2'].  The general syntax here is DictionaryName['ElementName'] = ElementValue.  The greatest thing about this is that you can add other data types to dictionaries just like we did above with lists.  Lets take a look at that:

 

We use the same dictionary created earlier, but this time we create a list of letters to be assigned to the dictionary.  Data types in Python play very well together and you can come up with a lot of ways to incorporate them into one another.  This example could easily be a list of dictionaries as shown below.

 

This concludes the brief introduction to python variables, data types and data structures.  Spend time working with IDLE, the concepts presented in these write-ups and look in to some of the details not covered here until you become comfortable.  Python is a great language for beginners, but these are not beginners tutorials.  If any of this seemed confusing spend more time online working with fundamental programming cocepts.