Let’s check out some exercises that will help understand dictionaries better.
Exercise 8-a: Python Dictionary Value by Key
Dictionaries don't have index orders, so speaking about them regarding their first item or last item is not very correct. Next time you print a dictionary it may have a different order than you saw before. Instead they have keys, and we can use keys to call their values.
When was Plato born?
Exercise 8-b: Editing Python Dictionaries
Change Plato's birth year from B.C. 427 to B.C. 428.
You can just use -428 unless you’d like to use a string format such as: “B.C. 428”
In dictionaries later values will always override previous values for the same key.
If you type the new value for the key you’d like to change it will be replaced.
To address a specific key in dictionaries:
dict["born"] = -428
Exercise 8-c: Adding a List to Python Dictionary (Nested Data)
Dictionaries can have nested data too. Also, you can add a new key to a dictionary as they are mutable (changeable). Try to add the key "work" to dict with values shown below.
"work": ["Apology", "Phaedo", "Republic", "Symposium"]
Exercise 8-d: Adding Up to the Value of a Key's Value (integer)
Add 2 inches to the son's height.
Exercise 8-e: Items method of dictionaries to get a list of tuples w/ key-value pairs
Using .items() method generate a list of tuples consisted of each key value pair.
Exercise 8-g: Get Method of Dictionaries for Sophisticated Value Calling
.get() method can be used just to get the value of a key. But it has more tricks up its sleeve.
Try to look for key: "son's age" and if nothing comes up make the .get() return "2".
Exercise 8-h: Clear method to remove everything from a dictionary
Since dictionaries are mutable, they have some methods that tuples don't have.
.clear() is one of them and it clears the whole dictionary.
Exercise 8-i: Len function to get the amount of keys in a dictionary
Using len function, find how many keys there are in the dictionary.
Exercise 8-j: Max function to get the key with the highest value
What's the key with the highest value in the dictionary?