Tuesday, March 10, 2015

Basics of the Python Dictionaries - Python Basics - Python Tutorial

basics of the Python dictionariesBasics of the Python Dictionaries


In this tutorial, we are about cover the basics of the Python dictionaries.  This tutorial’s main focus is placed on what a dictionary is and how to create a dictionary in Python. We will cover dictionaries in full in a later chapter this is only a brief overview. We will walk you through creating a dictionary and assigning it to a variable and then call that variable to display the dictionary.


What is a Python Dictionary?


A dictionary in Python is not a list of words and their meanings but you could make an actual dictionary if you wanted to use the dictionary data type in Python. Dictionaries are different from strings and list because the do not follow a sequence order but it does follow something called mapping which is also a collection of data but not in a specific order so Python stores the data via a key. Dictionaries do not comply with a left to right ordering like list and strings. Dictionaries are also mutable which means we can change a dictionary after it is created so a dictionary can grow or shrink on demand.


What is Mapping?


Since dictionaries do not follow a sequence order like a string or list we need a way to get values stored in a dictionary and we do this by giving the value of a key. The key is tied to a value which gives the ability to identify a value. In sequences like list, we can use the index to get the value and in dictionaries we use a key to get the value.


Python Dictionary Syntax


When we create a dictionary in Python we use curly braces   and inside those curly braces we provide a key and value like “key: value” the key and value are separated by a colon( : ). It is important to keep in know which is the key and which is the value. The key will always be on left and the value will always be on the right.


Python Dictionary Syntax


‘pet': “dog”, ‘age': 7, ‘name': ‘maggie’


Take a look at our example above. The keys in this dictionary are ‘pet’, ‘age’ and ‘name’. The values in this dictionary are ‘dog’, 7 and ‘maggie’.


Python Dictionary Examples


In these examples, we are going to show you how to create some dictionaries and assign them too variables.  Then we will show you how to call the whole dictionary and call a key to get the value associated with that value.


#create a dictionary and assign it to a variable
>>> a = "pet": "cat", "age": 2, "name": "Missy"

#Call the dictionary after it is created
>>> a
'name': 'Missy', 'age': 2, 'pet': 'cat'

#Call a value from the dictionary by its key
>>> a["name"]
'Missy'

#Something new lets change the age of the cat
>>> a["age"] += 1

#call the new dictionary
>>> a
'name': 'Missy', 'age': 3, 'pet': 'cat'

#To prove there is no index with dictionaries
>>> a[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 1

Basics of the Python Dictionaries Conclusion


We have covered the very basics of the Python dictionaries and we still have not scratched the surface. We will be revisiting dictionaries in a future chapter where we will dig deep into dictionaries and learn everything there is to know about Python dictionaries. If you have any questions please leave us a message below.



No comments:

Post a Comment