Python is a really powerful and flexible language. Give you the power to go from simple scripting to OO design and more, with a lot of functions and libraries. Following are some useful tips to exploit the language facilites. They are notes of common used functions or patterns.

Iterating on ...


Simple iteration from 1 to 100:
for i in range(1, 100):
   print i

Iteration over the elements of a list:

names = ['juan', 'pedro', 'jose']
for n in names:
   print n

Iteration over elements of a list and enumerate them:

names = ['juan', 'pedro', 'jose']
for i, n in enumerate(names):
   print "index: ", i, "name:", n


Iterating over keys of a dictionary:
namesDict = {1:'juan', 2:'pedro', 3:'jose'}
for k in namesDict.keys():
   print k
Iterating over values is the same but using nameDict.values() For iterating over both keys and values:
namesDict = {1:'juan', 2:'pedro', 3:'jose'}
for k, v in namesDict.iteritems():
   print "key: ", k, "name:", v
This is better than iterate over keys and use nameDict[k] for get the values.

Data structures

You can find here a more detailed explanation of the MetaData usage.