Created
March 29, 2012 12:45
-
-
Save dhconnelly/2237146 to your computer and use it in GitHub Desktop.
dictionaries tutorial for GSMST number theory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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