This blog assumes that you have a Linux machine with Python already installed.
If you don't, then you can use an AWS cloud based image that has Python. For help, refer to my blog
I am using Python 2.7 for this tutorial.
DICTIONARIES
A dictionary is a list of key:value pairs.
"Name":"Abraham Lincoln" is a key value pair.
1. Dictionary is defined as follows.
dict1 = {'Name':'James' , 'Age':27, 'Height':5.9}
2. A value can be retrieved as follows.
dict1['Name']
Example Program - 7:
********************************************************************
#! /bin/python
dict1 = {'Name': 'James', 'Age':27, 'Height':5.9}
print "\n dict1 = ", dict1 # Check the displayed order of objects
print "\n Name: ", dict1['Name']
dict1['Name'] = 'Andrew'
print "\n Updated dict1 = ", dict1
print "\n Keys of dict1 = ", dict1.keys()
del dict1['Name']
print "\n Updated dict1 = ", dict1
print "\n The length of dict1 = ", len(dict1)
dict2 = dict1
# Let us clear dict1
dict1.clear()
print "\n Cleared dict1 = ", dict1
dict1 = dict2
print "\n Updated dict1 = ", dict1
print "\n dict2 = ", dict2
********************************************************************
Why do you think both dict1 and dict2 are empty at the end of the program? You can find the answer in the next lesson.
EXERCISE - 4
Write a program that
1. Defines two dictionaries and prints them
2. Clears dictionary1
3. Assigns dictionary2 to dictionary1 and prints them both
4. Creates two more dictionaries.
4. Tries to concatenation (+) and repetition (*) the latest ones. Check what happens.