Data Structures

Python has data structures at its heart, they are flexible and easy to use.
Lists
An example:
list = [1, 2, 3, -4.3, "blah", ['a', 'b', 'c']]
	
Many operations can be performed on lists: Some operations have a nicer syntax than the above, for example:
list.append('a')
	
is equivalent to:
list + ['a']
	
Tuples
An example:
tuple = (x, y)
	
Tuples are like lists, except that once an instance has been created, it cannot be changed. The most common use of tuples is in making functions return more than one value:
return (a, b)
	
In fact, there is a short hand for tuples that makes this look even better:
return a, b
	
Neat, huh?
Dictionaries
This is simply another name for a hash table, it provides a mapping between keys and objects with a constant time look up of keys in the table. An example:
dict = {'feffi': 'feffi@hotmail.com', 'munchkin': 'munchkin@humbug.org'}
dict['feffi']
	
outputs:
'feffi@hotmail.com'
	
There are a few operations defined for dictionaries:

Now to show how to pass arbitrary arguments to a function. If a function is defined thus:

def function(args**):
     print args
and call it like this:
function(foo='bar', bar='baz')
	
then it will output:
{'foo': 'bar', 'bar': 'baz'}
	

Prev Contents Next
Clinton Roy