Back

More on dictionaries

Introduction
Here is a dictionary that maps letters and numbers to morse code:

morse = {
"A" : ".-",
"B" : "-...",
"C" : "-.-.",
"D" : "-..",
"E" : ".",
"F" : "..-.",
"G" : "--.",
"H" : "....",
"I" : "..",
"J" : ".---",
"K" : "-.-",
"L" : ".-..",
"M" : "--",
"N" : "-.",
"O" : "---",
"P" : ".--.",
"Q" : "--.-",
"R" : ".-.",
"S" : "...",
"T" : "-",
"U" : "..-",
"V" : "...-",
"W" : ".--",
"X" : "-..-",
"Y" : "-.--",
"Z" : "--..",
"0" : "-----",
"1" : ".----",
"2" : "..---",
"3" : "...--",
"4" : "....-",
"5" : ".....",
"6" : "-....",
"7" : "--...",
"8" : "---..",
"9" : "----.",
"." : ".-.-.-",
"," : "--..--"
}

Q1. Use some code to find out how many key-value pairs there are in the dictionary.
Q2.
Display all the keys.
Q3.
Display all the values.
Q4.
Display all the pairs.
Q5.
Write some code to see if 'a' is in the dictionary.
Q6. 
Write some code to see if 'D' is in the dictionary.
Q7.
Add this code to your morse program: print(morse.popitem()) and then run it a few times. Describe what it does.
Q8. Use the get method to get the morse code for ? and display 'Does not exist' if it isn't in the dictionary.
Q9. The morse code for a question mark is ..--.. so add this to the dictionary and check it gets added correctly.
Q10. Add a second dictionary to your code called punct. It should hold the morse codes for a full stop, comma, equals sign and the 'at' symbol. 
Q11. Find out how to merge two dictionaries in Python. Merge morse with punct and then print out both dictionaries and check they were merged correctly.
Q12. There was a full stop in both dictionaries. How did the merge deal with this?
Q13. Copy everything in the dictionary called morse to a new one called codes. Print out codes.

Back