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.#

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.3.#

Working with the dictionary named language_code, defined below using the second method that was discussed, create a dataset in the Comma Separated Values format. Each line of the CSV file should contain the code and the full name of the language, separated by a comma. The CSV file should have the following header: ‘code,language_name’. The language codes must be also be ordered alphabetically.

The output should look as follows:

code,language_name
dut,Dutch
eng,English
fre,French
ger,German
por,Portuguese
spa,Spanish
swe,Swedish
language_code = {
    'fre': 'French',
    'swe': 'Swedish',
    'eng': 'English',
    'ger': 'German',
    'spa': 'Spanish',
    'dut': 'Dutch',
    'por': 'Portuguese'
 }

print('code,language_name')

for item in sorted(language_code):
    print( f'{item},{language_code[item]}' )

Exercise 6.4.#

The code below creates a new dictionary. This dictionary connects a number of ISBNs to the titles of the books they identify.

isbn = {
9780143105985 : 'White Noise' ,
9780241984536 : 'Libra' ,
9781925480665 : 'Mao II' ,
9781447289395 : 'Underworld' ,
9780743595728 : 'The Body Artist' ,
9781925480665 : 'Cosmopolis' ,
9780330524919 : 'Falling man' ,
9781439169971 : 'Point Omega'
}
  • Add the novel Zero K to the dictionary. This novel has ISBN13 9781501138072.

  • Write some code which can print the title that corresponds to ISBN 9781447289395.

  • Print a list of all the novels. Display both the ISBN and the title.

# Create the dictionary
books_by_isbn = {
    9780143105985 : 'White Noise',
    9780241984536 : 'Libra',
    9781925480665 : 'Mao II',
    9781447289395 : 'Underworld',
    9780743595728 : 'The Body Artist',
    9781925480665 : 'Cosmopolis',
    9780330524919 : 'Falling man',
    9781439169971 : 'Point Omega'
}

Add the novel Zero K to the dictionary. This novel has ISBN 9781501138072.

books_by_isbn[9781501138072] = 'Zero K'

Write some code which can print the title that corresponds to ISBN 9781447289395.

index = 9781447289395
print( f"\nThe ISBN of the novel '{books_by_isbn[index] }' is {index}.\n" )

Print a list of all the novels. Display both the ISBN and the title.

for isbn in books_by_isbn:
    print( books_by_isbn[isbn] + ' (' + str(isbn) + ')' )

Exercise 6.3.#

We have a dictionary of countries in the European Union and 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.

# The keys of the dictionary are the countries
for country in sorted(eu_capitals):
    print( country )

Finally, 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]}.' )