Files
learn-python3/notebooks/beginner/exercises/dictionaries_exercise.ipynb
Jerry Pussinen 46450175dc Initial commit
2018-05-01 13:59:37 +02:00

127 lines
2.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. Populating a dictionary\n",
"Create a dictionary by using all the given variables."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"first_name = 'John'\n",
"last_name = 'Doe'\n",
"favorite_hobby = 'Python'\n",
"sports_hobby = 'gym'\n",
"age = 82"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your implementation\n",
"my_dict = 'TODO'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"assert my_dict == {\n",
" 'name': 'John Doe',\n",
" 'age': 82,\n",
" 'hobbies': ['Python', 'gym']\n",
" }"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2. Accessing and merging dictionaries\n",
"Combine `dict1`, `dict2`, and `dict3` into `my_dict`. In addition, get the value of `special_key` from `my_dict` into a `special_value` variable. Note that original dictionaries should stay untouched and `special_key` should be removed from `my_dict`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"dict1 = dict(key1='This is not that hard', key2='Python is still cool')\n",
"dict2 = {'key1': 123, 'special_key': 'secret'}\n",
"# This is also a away to initialize a dict (list of tuples) \n",
"dict3 = dict([('key2', 456), ('keyX', 'X')])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 'Your impelementation'\n",
"my_dict = 'TODO'\n",
"special_value = 'TODO'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"assert my_dict == {'key1': 123, 'key2': 456, 'keyX': 'X'}\n",
"assert special_value == 'secret'\n",
"\n",
"# Let's check that the originals are untouched\n",
"assert dict1 == {\n",
" 'key1': 'This is not that hard',\n",
" 'key2': 'Python is still cool'\n",
" }\n",
"assert dict2 == {'key1': 123, 'special_key': 'secret'}\n",
"assert dict3 == {'key2': 456, 'keyX': 'X'}"
]
}
],
"metadata": {
"celltoolbar": "Edit Metadata",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3.0
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
}