loop over the elements of a list like this: Files: acces_list_index.py; dictionaries.py; iterate_keys_dictionary.py; loop.py
animals = ['cat', 'dog', 'monkey']
for animal in animals:
print animal
nums = [0, 1, 2, 3, 4]
squares = [x ** 2 for x in nums]
print squares
#0, 1, 4, 9, 16]
d = {'person': 2, 'cat': 4, 'spider': 8}
for animal in d:
legs = d[animal]
print 'A %s has %d legs' % (animal, legs)
#A person has 2 legs
#A spider has 8 legs
#A cat has 4 legs
d = {'cat': 'cute', 'dog': 'furry'} # Create a new dictionary with some data
print d['cat'] # Get an entry from a dictionary; prints "cute"
print 'cat' in d # Check if a dictionary has a given key; prints "True"
#cute
#True
animals = ['cat', 'dog', 'monkey']
for idx, animal in enumerate(animals):
print '#%d: %s' % (idx + 1, animal)