Skip to content

Instantly share code, notes, and snippets.

@dhconnelly
Created March 29, 2012 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhconnelly/2237146 to your computer and use it in GitHub Desktop.
Save dhconnelly/2237146 to your computer and use it in GitHub Desktop.
dictionaries tutorial for GSMST number theory
# let's say we want to keep track of a person.
# me.
daniel = {
'fname': 'Daniel',
'lname': 'Connelly',
'age': 25,
'city': 'Atlanta'
}
print daniel
# get the data from "daniel" labeled "age":
print 'Daniel\'s age is', daniel['age']
# this is called a "dictionary", "map", "hashes", etc.
# or "dict" for short
# what if we want to add another field later?
daniel['hair'] = 'balding'
print daniel
# terminology: called dictionaries
# the "accessors" or "indices" are called "keys"
# so the keys of daniel are 'fname', 'lname', 'age', 'city', 'hair'
# the values are called values.
# let's say I move to California
daniel['city'] = 'San Francisco' # reassigns the value for 'city' to ...
print daniel
# what if we want to delete stuff from the dictionary?
del daniel['age']
print daniel
# what if we just want to know what keys are in the dictionary?
print sorted(daniel.keys())
# what if we just want the values?
print daniel.values()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment