Python 5a Notes - Updated 1
Python 5a Notes - Updated 1
1 of 14
Contents
Dictionary in Python
A dictionary is a container object that stores a collection of key/value pairs. It is a
collection that stores the values along with the keys. The keys are like an index
operator. In a list, the indexes are integers. But in dictionary, the key must be a
hashable object. A dictionary cannot contain duplicate keys. Each key maps to one
value. A key and its corresponding value form an item stored in a dictionary. The data
structure is called as dictionary because it resembles a word dictionary, where the
words are the keys and the words’ definitions are the values. A dictionary is also
known as a map hich maps each key to a value.
Create Dictionary
In Python, we can create a Dictionary by placing sequence of elements within curly {}
braces and separated by ‘comma’. A Dictionary holds a pair of values, one being the
Key and the other being the corresponding value. Notice that values in a dictionary
can be of any datatype and duplication is allowed. Keys, however, can’t be repeated
and must be immutable.
print("\n-----------------------------\n")
ch = input("")
P. 4 of 14
Elaboration of pythonDictionary.py
1. Dictionary Creation
monthDict = {1:"January", 2:"Feb", 3:"March", 4:"April",
5:"May", 6:"June", 7:"July", 8:"August", 9:"September",
10:"October", 11:"November", 12:"December"}
3. Separator
print("\n-----------------------------\n")
➢ The program prompts the user with the message: "What is the current
month number? ".
➢ The user is expected to input an integer representing the current month (e.g.,
1 for January, 2 for February, etc.).
➢ The input() function takes the user input as a string, and the int()
function converts it into an integer.
Example:
➢ If the user inputs 3, the value of currentMonth will be 3.
➢ The .get() method is used to retrieve the value (month name) associated
with the key currentMonth from the monthDict dictionary.
➢ If the key exists in the dictionary, the corresponding month name is stored in
the variable stringMonth.
➢ If the key does not exist, .get() will return None.
Example:
➢ If currentMonth = 3, the method retrieves "March" because it is the value
associated with the key 3.
P. 7 of 14
Example Output:
➢ If currentMonth = 3, the output will be:
You are advised to take a look in the following example (addToDictionary.py) for
further illustration:
print(myDict)
print("\n-----------------------------------------\n")
print("\n-----------------------------------------\n")
P. 9 of 14
ch = input("")
After running the program, you should get the output like this:
P. 10 of 14
Elaboration of addToDictionary.py
1. Creating an Empty Dictionary
myDict = {}
Resulting myDict:
{
"England": "London",
"France": "Paris",
"Japan": "Tokyo",
"Korea": "Seoul"
}
P. 11 of 14
4. Separator
print("\n-----------------------------------------\n")
After transformation:
listKey = ["England", "France", "Japan", "Korea"]
listValue = ["London", "Paris", "Tokyo", "Seoul"]
P. 12 of 14
The loop iterates over the indices of the listKey and listValue lists:
➢ For each index i, it prints:
The capital of <listKey[i]> is <listValue[i]>.
Example Output:
The capital of England is London.
The capital of France is Paris.
The capital of Japan is Tokyo.
The capital of Korea is Seoul.
6. Separator
print("\n-----------------------------------------\n")
➢ The for loop iterates over the sorted key-value pairs, unpacking each pair into
k (key) and v (value).
Example Output:
The capital of Korea is Seoul.
The capital of Japan is Tokyo.
The capital of France is Paris.
The capital of England is London.
P. 14 of 14
Classwork 1
Open file fruitDictionary - practice.py. You will see a list named fruitList with several
fruit names as items.
Try to complete the remaining code that the following requirements are fulfilled:
1. Create an empty dictionary called fruitDict
2. Use for-loop to ask user input the price of each fruit
3. Populate the fruitDict dictionary with fruitname as key and price as value
4. Print out the price list as shown in the figure below