6. Dictionaries#

Exercise 6.1.#

The keys in a dictionary may consist of labels describing the values we are dealing with. The dictionary named person_data in the cell below is used to capture biographical information. Use this dictionary to print the following sentence:

“Isaac Newton was born in 1642 in Woolsthorpe.”

person_data = dict()

person_data['first_name'] = 'isaac'
person_data['last_name'] = 'newton'
person_data['year_of_birth'] = 1642
person_data['place_of_birth'] = 'woolsthorpe'

# We create a sentence variable, to make the code more readable
# Remember that we can use the title() method to capitalise the first character.

sentence = f"{person_data['first_name'].title()} {person_data['last_name'].title()}"
sentence += f" was born in {person_data['year_of_birth']}"
sentence += f" in {person_data['place_of_birth'].title()}."

print(sentence)

Exercise 6.2.#

Create a dictionary that can be used to connect ISBN codes to the titles of the books they identify. The name of this dictionary must be books_by_isbn. Using the second method that was discussed to create a dictionaries.

Upon its creation, the dictionary should be assigned the following items:

ISBN: 9780143105985
title: 'White Noise'

ISBN: 9781447289395 
Title: 'Underworld'

After this, write code to add the following book individually:

ISBN: 9780330452236
Title: 'Falling Man'

Finally, print a list of all the novels in the dictionary ‘books_by_isbn’. For each item in the dictionary, print the sentence ‘The ISBN of the novel ‘[title]’ is [ISBN]’.

Make sure that the code returns the string undefined if the isbn that is mentioned has not been addto the dictionary yet.

books_by_isbn = { 
    9780143105985:'White Noise',
    9781447289395:'Underworld' }

books_by_isbn[9780330452236]='Falling Man'

for isbn in books_by_isbn:
    print( f"The ISBN of the novel '{books_by_isbn.get(isbn,'[Undefined]')}' is {isbn}." )

Exercise 6.3.#

The cell below creates a dictionary named language_code and a list named language_code. Using these two variables, try to print the names of the languages that are given in the list named languages. The output should look as follows:

The list contains the following countries:
German
Spanish
English
language_code = dict()
language_code['dut'] = 'Dutch'
language_code['fre'] = 'French'
language_code['eng'] = 'English'
language_code['ger'] = 'German'
language_code['spa'] = 'Spanish'
language_code['por'] = 'Portuguese'
language_code['swe'] = 'Swedish'

languages = ['ger','spa','eng']

print('The list contains the following countries:')

for language in languages:
    print( language_code[language] )

Exercise 6.4.#

The dictionary named country_continent, defined below, connects the names of countries to the continents they are on.

Print a list of all the countries in this dictionary that are in Asia. The list needs to be sorted alphabetically.

country_continent = {
"Belgium":"Europe", 
"France":"Europe", 
"Italy":"Europe", 
"China":"Asia",
"Thailand":"Asia",
"Japan":"Asia", 
"India":"Asia", 
"Bangladesh":"Asia", 
"Ghana":"Africa", 
"Tunesia":"Africa" }

for country in sorted(country_continent):
    if country_continent[country] == 'Asia':
        print(country)

Exercise 6.5.#

The dictionary named eu_capitals connects the countries in the European Union to their capitals.

# Create the dictionary
eu_capitals = {
    'Italy': 'Rome', 'Luxembourg': 'Luxembourg',
    'Belgium': 'Brussels', 'Denmark': 'Copenhagen',
    'Finland': 'Helsinki', 'France': 'Paris',
    'Slovakia': 'Bratislava', 'Slovenia': 'Ljubljana',
    'Germany': 'Berlin', 'Greece': 'Athens',
    'Ireland': 'Dublin', 'Netherlands': 'Amsterdam',
    'Portugal': 'Lisbon', 'Spain': 'Madrid',
    'Sweden': 'Stockholm',
    'Cyprus': 'Nicosia', 'Lithuania': 'Vilnius', 
    'Czech Republic': 'Prague', 'Estonia': 'Tallin',
    'Hungary': 'Budapest', 'Latvia': 'Riga',
    'Malta': 'Valetta', 'Austria': 'Vienna',
    'Poland': 'Warsaw', 'Croatia': 'Zagreb',
    'Romania': 'Bucharest', 'Bulgaria': 'Sofia'
}

Using this dictionary, print a sentence which gives information about the current number of countries in the EU. The number of items in a dictionary can be determined using the len() function.

print( f'The EU currently has {len(eu_capitals)} member countries:' )

Print a list of all the countries of the EU in alphabetical order.

For each country, print the following sentence: “The capital of [ country ] is [ capital ].”

for country in sorted(eu_capitals):
    print( f'The capital of {country} is {eu_capitals[country]}.' )