Think of a few scenarios in which we can use a dictionary?
. So, today we will see how we can use the dictionary to maintain a database of Countries and their capitals.
When we create a database, what are the operations we expect to do on it?
● Insert
● Delete
● Display
● search
Let us see the functions/methods, which help us in doing these.
writes code on IDLE Create a dictionary of countries and their capitals.
Print the countries in the dictionary.
Note: Explain how the use of the list is displaying keys as a normal list
Print the capitals stored in the dictionary.
Note: The use of the list is displaying values as a normal list
Get the capital of India from a dictionary using the get() function.
Create an empty dictionary and start an infinite while loop.
Print the menu to maintain a country-capital repository and get user choice.
Check if the choice entered is 1, the get country, capital as user input , and insert these values into the dictionary.
Check if the choice entered is 3 and print all capitals as a list.
Check if the choice entered is 4, get as the user input a country and then print its capital.
Check if the choice entered is 5, get as the user input a country and delete that from the dictionary.
If the option entered is none of the above, exit from the program.
Comments
Post a Comment