Last active
February 10, 2017 00:53
-
-
Save joetechem/f697f828f0c1b68e57f085708a12358e to your computer and use it in GitHub Desktop.
showing an empty python dictionary
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
# Python 2.7 | |
# By Joe | |
# Below is a basic review of dictionaries in Python | |
# starting with an empty dictionary, then adding information to it | |
# Example: | |
# MAKE AN EMPTY DICTIONARY | |
john = {} | |
# ADDING KEY-VALUE PAIRS TO THE DICTIONARY | |
john['last_name'] = 'smith' | |
# RETURNING ALL KEY-VALUE PAIRS IN THE DICTIONARY | |
# Now, let's show the key-value pair in the dictionary | |
print(john) | |
# would return {'last_name': 'smith'} | |
# is same as: {'key': 'value} | |
# ACCESS VALUES BY CALLING ITS KEY | |
# Let's say you wanted to search a dictionary that had a whole lot of data (like thousands or more of dictionary items) | |
# Depending on how specific, this searching could take a very long time | |
# If you knew the key, you could access that and get its associated value returned | |
# for example: | |
print(john['last_name']) | |
# would return that key's value | |
# You can relate this in the real-world by searching for one word in a very large dictionary, glossary, or encyclopedia | |
# You COULD search each word starting from the beginning (would take quite a while) | |
# OR you could use different search methods: Look up the starting letter of the word (or key) | |
# This narrows your search down a bit | |
# Now you can just search for the word (key) in that selection to get its VALUE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment