{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Dictionaries - Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 6.1.\n", "\n", "The code below creates a new dictionary. This dictionary connects a number of ISBNs to the titles of the books they identify.\n", "\n", "```\n", "isbn = {\n", "9780143105985 : 'White Noise' ,\n", "9780241984536 : 'Libra' ,\n", "9781925480665 : 'Mao II' ,\n", "9781447289395 : 'Underworld' ,\n", "9780743595728 : 'The Body Artist' ,\n", "9781925480665 : 'Cosmopolis' ,\n", "9780330524919 : 'Falling man' ,\n", "9781439169971 : 'Point Omega'\n", "}\n", "```\n", "\n", "* Add the novel Zero K to the dictionary. This novel has ISBN13 9781501138072.\n", "* Write some code which can print the title that corresponds to ISBN 9781447289395. \n", "* Print a list of all the novels. Display both the ISBN and the title. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "isbn = {\n", "9780143105985 : 'White Noise' ,\n", "9780241984536 : 'Libra' ,\n", "9781925480665 : 'Mao II' ,\n", "9781447289395 : 'Underworld' ,\n", "9780743595728 : 'The Body Artist' ,\n", "9781925480665 : 'Cosmopolis' ,\n", "9780330524919 : 'Falling man' ,\n", "9781439169971 : 'Point Omega'\n", "}\n", "\n", "isbn[9781501138072] = 'Zero K'\n", "\n", "index = 9781447289395\n", "print( \"\\nThe ISBN of the novel '{}' is {}.\\n\".format( isbn[index] , index ) )\n", "\n", "for novel in isbn:\n", " print( isbn[novel] + ' (' + str(novel) + ')' )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 6.2.\n", "\n", "Using the dictionary ‘data’ below, print the following sentence: “Louis Elsevier was a printer. He was born in 1540 in Leuven and died in 1617 in Leiden.”\n", "\n", "```\n", "data = dict()\n", "\n", "data[\"firstName\"] = 'louis'\n", "data[\"lastName\"] = 'elsevier'\n", "data[\"profession\"] = 'printer'\n", "data[\"yob\"] = 1540\n", "data[\"yod\"] = 1617\n", "data[\"pob\"] = 'leuven'\n", "data[\"pod\"] = 'leiden'\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = dict()\n", "\n", "data[\"firstName\"] = 'louis'\n", "data[\"lastName\"] = 'elsevier'\n", "data[\"profession\"] = 'printer'\n", "data[\"yob\"] = 1540\n", "data[\"yod\"] = 1617\n", "data[\"pob\"] = 'leuven'\n", "data[\"pod\"] = 'leiden'\n", "\n", "sentence = '{} {} was a {}. '.format( data['firstName'].title() , data['lastName'].title() , data['profession'] )\n", "sentence += 'He was born in {} in {} and died in {} in {}.'.format( data[\"yob\"] , data.get(\"pob\" , \"[Unknown]\" ).title() , data[\"yod\"] , data.get(\"pod\" , \"[Unknown]\" ).title() )\n", "\n", "print( sentence )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 6.3.\n", "\n", "Copy the code below in your code editor. Using the dictionary ‘eu’, print a sentence which gives information about the current number of countries in the EU. Add some code which can print a list of all the countries of the EU in alphabetical order. Finally, print a list of all the capitals of these countries, also in alphabetical order.\n", "\n", "```\n", "eu = {\n", "'Italy':'Rome' , 'Luxembourg':'Luxembourg' ,\n", "'Belgium':'Brussels' , 'Denmark':'Copenhagen' ,\n", "'Finland':'Helsinki' , 'France':'Paris' ,\n", "'Slovakia':'Bratislava' , 'Slovenia':'Ljubljana' ,\n", "'Germany':'Berlin' , 'Greece':'Athens' ,\n", "'Ireland':'Dublin' , 'Netherlands':'Amsterdam' ,\n", "'Portugal':'Lisbon' , 'Spain':'Madrid' ,\n", "'Sweden':'Stockholm' , 'United Kingdom':'London' ,\n", "'Cyprus':'Nicosia' , 'Lithuania':'Vilnius' , 'Czech\n", "Republic':'Prague' , 'Estonia':'Tallin' ,\n", "'Hungary':'Budapest' , 'Latvia':'Riga' ,\n", "'Malta':'Valetta' , 'Austria':'Vienna' ,\n", "'Poland':'Warsaw' , 'Croatia':'Zagreb'\n", ",'Romania':'Bucharest' , 'Bulgaria':'Sofia' }```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "eu = {\n", "'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' , 'United Kingdom':'London' , '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' }\n", "\n", "print( 'The EU currently has {} member countries:'.format( len(eu) ) )\n", "\n", "\n", "for country in sorted(eu):\n", " print( country )\n", "\n", " \n", "\n", "print('\\n')\n", "\n", "def getSortedKeys( dict ):\n", " return sorted( dict , key=lambda x: dict[x]) \n", "\n", "\n", "for country in getSortedKeys( eu ):\n", " print( 'The capital of {} is {}.'.format( country , eu[country] ) )\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.2" } }, "nbformat": 4, "nbformat_minor": 2 }