{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# HyperLearning AI - Introduction to Python\n",
"An introductory course to the Python 3 programming language, with a curriculum aligned to the Certified Associate in Python Programming (PCAP) examination syllabus (PCAP-31-02).
\n",
"https://knowledgebase.hyperlearning.ai/courses/introduction-to-python\n",
"\n",
"\n",
"## 05. Data Aggregates Part 2\n",
"https://knowledgebase.hyperlearning.ai/en/courses/introduction-to-python/modules/5/data-aggregates-part-2\n",
"\n",
"In this module we will introduce additional common collection data structures available in Python and associated methods, including:\n",
"\n",
"* **Tuples** - constructing, accessing and manipulating tuple data collection structures\n",
"* **Dictionaries** - constructing, accessing and manipulating dictionary data collection structures"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. Tuples\n",
"#### 1.1. Defining a Tuple"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"()\n",
"('elem1',)\n",
"(10,)\n",
"('a',)\n",
"\n",
"\n",
"(1, 2, 3, 4, 5)\n"
]
}
],
"source": [
"# Create an empty tuple\n",
"empty_tuple = ()\n",
"print(empty_tuple)\n",
"\n",
"# Create a tuple with only 1 element by using a trailing comma\n",
"tuple_one_element_1 = 'elem1', \n",
"print(tuple_one_element_1)\n",
"tuple_one_element_2 = 10, \n",
"print(tuple_one_element_2)\n",
"tuple_one_element_3 = ('a', )\n",
"print(tuple_one_element_3)\n",
"\n",
"# If we forget the trailing comma, then Python will recognise it as a string\n",
"print(type(tuple_one_element_1))\n",
"tuple_one_element_4 = ('a')\n",
"print(type(tuple_one_element_4))\n",
"\n",
"# Create a tuple with 5 elements\n",
"my_tuple = (1, 2, 3, 4, 5)\n",
"print(my_tuple)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('a', 'b', 'c')"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Initialise a tuple with a list\n",
"my_tuple_from_a_list = tuple([\"a\", \"b\", \"c\"])\n",
"my_tuple_from_a_list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.2. Standard for Heterogeneity"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('Barack', 'Obama', 59)\n",
"5\n"
]
}
],
"source": [
"# Create a heterogeneous tuple containing information about an individual\n",
"individual_tuple = (\"Barack\", \"Obama\", 59)\n",
"print(individual_tuple)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.4. Index and Negative Indexing"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Barack\n",
"59\n",
"American\n",
"1.85\n"
]
}
],
"source": [
"# Access elements in a tuple by their index numbers\n",
"my_tuple = (\"Barack\", \"Obama\", \"04/08/1961\", 59, \"Male\", 1.85, \"American\")\n",
"print(my_tuple[0])\n",
"print(my_tuple[3])\n",
"print(my_tuple[-1])\n",
"print(my_tuple[-2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.5. Slice Notation"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('Barack', 'Obama', '04/08/1961', 59, 'Male', 1.85, 'American')\n",
"('04/08/1961', 59, 'Male', 1.85, 'American')\n",
"('Barack', 'Obama', '04/08/1961', 59)\n",
"('04/08/1961', 59, 'Male')\n",
"('Obama', 59, 1.85)\n"
]
}
],
"source": [
"# Use slice notation on tuples\n",
"print(my_tuple[:])\n",
"print(my_tuple[2:])\n",
"print(my_tuple[:4])\n",
"print(my_tuple[2:5])\n",
"print(my_tuple[1::2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.6. Tuple Length"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7\n"
]
}
],
"source": [
"# Using the len() function on tuples\n",
"print(len(my_tuple))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.7. Tuple Membership"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This tuple correponds to information about Barack Obama\n"
]
}
],
"source": [
"# Test whether an element exists in a tuple\n",
"if 'Obama' in my_tuple:\n",
" print(f\"This tuple correponds to information about Barack Obama\")\n",
"else:\n",
" print(f\"This tuple does not correspond to information about Barack Obama\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.8. Deleting Tuples"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('a', 'b', 'c')\n"
]
},
{
"ename": "NameError",
"evalue": "name 'my_tuple_from_a_list' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmy_tuple_from_a_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0mmy_tuple_from_a_list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmy_tuple_from_a_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'my_tuple_from_a_list' is not defined"
]
}
],
"source": [
"# Delete a tuple\n",
"print(my_tuple_from_a_list)\n",
"del my_tuple_from_a_list\n",
"print(my_tuple_from_a_list)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.9. Joining Tuples"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('a', 1, 'b', 2, 'c', 3, 'd', 4, 'e', 5, 'f', 6)\n"
]
}
],
"source": [
"# Join two tuples to create a new tuple\n",
"my_first_tuple = ('a', 1, 'b', 2, 'c', 3)\n",
"my_second_tuple = ('d', 4, 'e', 5, 'f', 6)\n",
"my_third_tuple = my_first_tuple + my_second_tuple\n",
"print(my_third_tuple)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.10. Tuple Assignment"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('Barack', 'Obama', 59)\n",
"\n"
]
}
],
"source": [
"# Use tuple packing to pack values into a single tuple\n",
"barack_obama = \"Barack\", \"Obama\", 59\n",
"print(barack_obama)\n",
"print(type(barack_obama))"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Barack\n",
"Obama\n",
"59\n"
]
}
],
"source": [
"# Use sequence unpacking to assign values to variables from a given sequence\n",
"barack_obama_first_name, barack_obama_last_name, barack_obama_age = barack_obama\n",
"print(barack_obama_first_name)\n",
"print(barack_obama_last_name)\n",
"print(barack_obama_age)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"99\n",
"99\n",
"10\n"
]
}
],
"source": [
"# Swap values assigned to two different variables\n",
"a = 10\n",
"b = 99\n",
"print(a)\n",
"print(b)\n",
"(a, b) = (b, a)\n",
"print(a)\n",
"print(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.11. Tuple Methods"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"6\n",
"0\n",
"0\n",
"1\n"
]
},
{
"ename": "ValueError",
"evalue": "tuple.index(x): x not in tuple",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmy_tuple\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmy_tuple\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmy_tuple\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: tuple.index(x): x not in tuple"
]
}
],
"source": [
"# Create a tuple\n",
"my_tuple = (0, 1, 0, 1, 1, 0, 0, 1, 1, 1)\n",
"\n",
"# tuple.count(x)\n",
"print(my_tuple.count(0))\n",
"print(my_tuple.count(1))\n",
"print(my_tuple.count(2))\n",
"\n",
"# tuple.index(x)\n",
"print(my_tuple.index(0))\n",
"print(my_tuple.index(1))\n",
"print(my_tuple.index(2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.12. Iterating Tuples"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Diatomic Sequence - Element #1: 0\n",
"Diatomic Sequence - Element #2: 1\n",
"Diatomic Sequence - Element #3: 1\n",
"Diatomic Sequence - Element #4: 2\n",
"Diatomic Sequence - Element #5: 1\n",
"Diatomic Sequence - Element #6: 3\n",
"Diatomic Sequence - Element #7: 2\n",
"Diatomic Sequence - Element #8: 3\n",
"Diatomic Sequence - Element #9: 1\n",
"Diatomic Sequence - Element #10: 4\n",
"Diatomic Sequence - Element #11: 3\n",
"Diatomic Sequence - Element #12: 5\n",
"Diatomic Sequence - Element #13: 2\n",
"Diatomic Sequence - Element #14: 5\n",
"Diatomic Sequence - Element #15: 3\n",
"Diatomic Sequence - Element #16: 4\n",
"Diatomic Sequence - Element #1: 0\n",
"Diatomic Sequence - Element #2: 1\n",
"Diatomic Sequence - Element #3: 1\n",
"Diatomic Sequence - Element #4: 2\n",
"Diatomic Sequence - Element #5: 1\n",
"Diatomic Sequence - Element #6: 3\n",
"Diatomic Sequence - Element #7: 2\n",
"Diatomic Sequence - Element #8: 3\n",
"Diatomic Sequence - Element #9: 1\n",
"Diatomic Sequence - Element #10: 4\n",
"Diatomic Sequence - Element #11: 3\n",
"Diatomic Sequence - Element #12: 5\n",
"Diatomic Sequence - Element #13: 2\n",
"Diatomic Sequence - Element #14: 5\n",
"Diatomic Sequence - Element #15: 3\n",
"Diatomic Sequence - Element #16: 4\n",
"0\n",
"1\n",
"1\n",
"2\n",
"1\n",
"3\n",
"2\n",
"3\n",
"1\n",
"4\n",
"3\n",
"5\n",
"2\n",
"5\n",
"3\n",
"4\n",
"Diatomic Sequence - Element #1: 0\n",
"Diatomic Sequence - Element #2: 1\n",
"Diatomic Sequence - Element #3: 1\n",
"Diatomic Sequence - Element #4: 2\n",
"Diatomic Sequence - Element #5: 1\n",
"Diatomic Sequence - Element #6: 3\n",
"Diatomic Sequence - Element #7: 2\n",
"Diatomic Sequence - Element #8: 3\n",
"Diatomic Sequence - Element #9: 1\n",
"Diatomic Sequence - Element #10: 4\n",
"Diatomic Sequence - Element #11: 3\n",
"Diatomic Sequence - Element #12: 5\n",
"Diatomic Sequence - Element #13: 2\n",
"Diatomic Sequence - Element #14: 5\n",
"Diatomic Sequence - Element #15: 3\n",
"Diatomic Sequence - Element #16: 4\n"
]
}
],
"source": [
"# Create a tuple\n",
"my_diatomic_tuple = (0, 1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 5, 2, 5, 3, 4)\n",
"\n",
"# Iterate the tuple using a while loop\n",
"i = 0\n",
"while i < len(my_diatomic_tuple):\n",
" print(f'Diatomic Sequence - Element #{i + 1}: {my_diatomic_tuple[i]}')\n",
" i += 1\n",
"\n",
"# Iterate the tuple using a for loop with the range function\n",
"for x in range(len(my_diatomic_tuple)):\n",
" print(f'Diatomic Sequence - Element #{x + 1}: {my_diatomic_tuple[x]}')\n",
"\n",
"# Iterate the tuple using a for loop with the in operator\n",
"for elem in my_diatomic_tuple:\n",
" print(elem)\n",
"\n",
"# Iterate the tuple using a for loop with the in operator and enumerate function\n",
"for idx, elem in enumerate(my_diatomic_tuple):\n",
" print(f'Diatomic Sequence - Element #{idx + 1}: {elem}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.13. Tuples as Return Values"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The circumference of a circle of radius 10cm is 62.83185307179586cm\n",
"The area of a circle of radius 10cm is 314.1592653589793cm²\n"
]
}
],
"source": [
"import math\n",
"\n",
"# Return both the circumference and area of a circle given a radius\n",
"def circle(radius):\n",
" \"\"\" Return the (circumference, area) of a circle given its radius \"\"\"\n",
" circumference = 2 * math.pi * radius\n",
" area = math.pi * radius * radius\n",
" return (circumference, area)\n",
"\n",
"my_radius = 10\n",
"my_cirumference, my_area = circle(my_radius)\n",
"print(f'The circumference of a circle of radius {my_radius}cm is {my_cirumference}cm')\n",
"print(f'The area of a circle of radius {my_radius}cm is {my_area}cm\\u00b2')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.14. Tuples in Lists"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(1, 'abc'), (2, 'def'), (3, 'ghi')]\n",
"(1, 'abc')\n",
"1\n",
"(2, 'def')\n",
"def\n",
"(3, 'ghi')\n",
"3\n",
"\n"
]
}
],
"source": [
"# Create a list of tuples\n",
"my_list_of_tuples = [(1, 'abc'), (2, 'def'), (3, 'ghi')]\n",
"print(my_list_of_tuples)\n",
"print(my_list_of_tuples[0])\n",
"print(my_list_of_tuples[0][0])\n",
"print(my_list_of_tuples[1])\n",
"print(my_list_of_tuples[1][1])\n",
"print(my_list_of_tuples[2])\n",
"print(my_list_of_tuples[2][0])\n",
"print(type(my_list_of_tuples))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.15. Lists in Tuples"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"([1, 2, 3], ['abc', 'def', 'ghi'])\n",
"[1, 2, 3]\n",
"1\n",
"['abc', 'def', 'ghi']\n",
"def\n",
"\n"
]
}
],
"source": [
"# Create a tuple of lists\n",
"my_tuple_of_lists = ([1, 2, 3], ['abc', 'def', 'ghi'])\n",
"print(my_tuple_of_lists)\n",
"print(my_tuple_of_lists[0])\n",
"print(my_tuple_of_lists[0][0])\n",
"print(my_tuple_of_lists[1])\n",
"print(my_tuple_of_lists[1][1])\n",
"print(type(my_tuple_of_lists))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.16. Multi-Dimensional Tuples"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 0 0 \n",
"0 1 0 \n",
"0 0 1 \n"
]
}
],
"source": [
"# Create an immutable 3 x 3 identity matrix\n",
"identity_matrix = ((1, 0, 0), (0, 1, 0), (0, 0, 1))\n",
"for row in identity_matrix:\n",
" for elem in row:\n",
" print(elem, end=' ')\n",
" print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Dictionaries\n",
"#### 2.1. Defining a Dictionary"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{}\n",
"{'China': 1394, 'India': 1326, 'Japan': 126, 'United Kingdom': 66, 'United States': 330}\n"
]
}
],
"source": [
"# Create an empty dictionary\n",
"my_empty_dict = {}\n",
"print(my_empty_dict)\n",
"\n",
"# Create a dictionary mapping countries (keys) to their population size in millions (value)\n",
"country_population_dict = {\n",
" \"China\": 1394,\n",
" \"India\": 1326,\n",
" \"Japan\": 126,\n",
" \"United Kingdom\": 66,\n",
" \"United States\": 330\n",
"}\n",
"print(country_population_dict)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'China': 1394, 'India': 1326, 'Japan': 126, 'United Kingdom': 66, 'United States': 330}\n"
]
}
],
"source": [
"# Create a dictionary using the dict() function and a list of tuples\n",
"country_population_dict = dict([\n",
" (\"China\", 1394), \n",
" (\"India\", 1326), \n",
" (\"Japan\", 126), \n",
" (\"United Kingdom\", 66), \n",
" (\"United States\", 330)\n",
"])\n",
"print(country_population_dict)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2.2. Accessing Dictionary Items"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The population of Japan is 126m\n",
"The population of China is 1394m\n"
]
}
],
"source": [
"# Access a dictionary value item by its key using square brackets\n",
"japan_population_m = country_population_dict[\"Japan\"]\n",
"print(f'The population of Japan is {japan_population_m}m')\n",
"\n",
"# Access a dictionary value item by its key using the get() method\n",
"print(f'The population of China is {country_population_dict.get(\"China\")}m')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "'Brazil'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Perform a lookup using a non-existent key using square brackets\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mbrazil_population_m\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcountry_population_dict\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"Brazil\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf'The population of Brazil is {brazil_population_m}m'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyError\u001b[0m: 'Brazil'"
]
}
],
"source": [
"# Perform a lookup using a non-existent key using square brackets\n",
"brazil_population_m = country_population_dict[\"Brazil\"]\n",
"print(f'The population of Brazil is {brazil_population_m}m')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The population of Brazil is Nonem\n"
]
}
],
"source": [
"# Perform a lookup using a non-existent key using the get() method\n",
"print(f'The population of Brazil is {country_population_dict.get(\"Brazil\")}m')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2.3. Modifying Dictionary Items"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'China': 2, 'India': 8, 'Japan': 3, 'United Kingdom': 5, 'United States': 1}\n",
"{'China': 2, 'India': 7, 'Japan': 3, 'United Kingdom': 5, 'United States': 1}\n"
]
}
],
"source": [
"# Create a dictionary of GDP rankings by country\n",
"gdp_rankings_dict = {\n",
" \"China\": 2,\n",
" \"India\": 8,\n",
" \"Japan\": 3,\n",
" \"United Kingdom\": 5,\n",
" \"United States\": 1\n",
"}\n",
"print(gdp_rankings_dict)\n",
"\n",
"# Modify the ranking of an existing country\n",
"gdp_rankings_dict[\"India\"] = 7\n",
"print(gdp_rankings_dict)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2.4. Adding Dictionary Items"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'China': 1394, 'India': 1326, 'Japan': 126, 'United Kingdom': 66, 'United States': 330, 'South Korea': 'Seoul'}\n",
"{'China': 1394, 'India': 1326, 'Japan': 126, 'United Kingdom': 66, 'United States': 330, 'South Korea': 52}\n"
]
}
],
"source": [
"# Add a country to the population dictionary\n",
"print(country_population_dict)\n",
"country_population_dict[\"South Korea\"] = 52\n",
"print(country_population_dict)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2.5. Removing Dictionary Items"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'China': 1394, 'India': 1326, 'Japan': 126, 'United Kingdom': 66, 'United States': 330, 'South Korea': 52}\n",
"{'China': 1394, 'India': 1326, 'United Kingdom': 66, 'United States': 330, 'South Korea': 52}\n"
]
}
],
"source": [
"# Remove a key:value pair from the country population dictionary usign the del keyword\n",
"print(country_population_dict)\n",
"del country_population_dict[\"Japan\"]\n",
"print(country_population_dict)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'China': 1394, 'India': 1326, 'United States': 330, 'South Korea': 52}\n",
"{'China': 1394, 'India': 1326, 'United States': 330}\n"
]
}
],
"source": [
"# Remove a key:value pair from the country population dictionary usign the pop() method\n",
"country_population_dict.pop(\"United Kingdom\")\n",
"print(country_population_dict)\n",
"\n",
"# Remove the last inserted key:value pair using the popitem() method\n",
"country_population_dict.popitem()\n",
"print(country_population_dict)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'China': 2, 'India': 7, 'Japan': 3, 'United Kingdom': 5, 'United States': 1}\n"
]
},
{
"ename": "NameError",
"evalue": "name 'gdp_rankings_dict' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgdp_rankings_dict\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0mgdp_rankings_dict\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgdp_rankings_dict\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'gdp_rankings_dict' is not defined"
]
}
],
"source": [
"# Delete the GDP rankings dictionary\n",
"print(gdp_rankings_dict)\n",
"del gdp_rankings_dict\n",
"print(gdp_rankings_dict)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2.6. Iterating Dictionaries"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"China\n",
"India\n",
"United States\n",
"\n",
"1394\n",
"1326\n",
"330\n"
]
}
],
"source": [
"# Use a for loop to iterate through the keys in a given dictionary, printing the keys\n",
"for key in country_population_dict:\n",
" print(key)\n",
"print()\n",
" \n",
"# Use a for loop to iterate through the keys in a given dictionary, printing the values\n",
"for key in country_population_dict:\n",
" print(country_population_dict[key])"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1394\n",
"1326\n",
"330\n",
"\n",
"The population of China is 1394m\n",
"The population of India is 1326m\n",
"The population of United States is 330m\n"
]
}
],
"source": [
"# Use the values() method to iterate over the values of a dictionary\n",
"for v in country_population_dict.values():\n",
" print(v)\n",
"print()\n",
"\n",
"# Use the items() method to iterate over both keys and values of a dictionary\n",
"for k, v in country_population_dict.items():\n",
" print(f'The population of {k} is {v}m')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2.7. Checking Key Existence"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"South Korea does not exist in our dictionary of populations.\n"
]
}
],
"source": [
"# Test for the existence of a key in a given dictionary\n",
"if \"South Korea\" in country_population_dict:\n",
" print(f'The population of South Korea is {country_population_dict[\"South Korea\"]}m')\n",
"else:\n",
" print('South Korea does not exist in our dictionary of populations.')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2.8. Dictionary Comprehension"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}\n"
]
}
],
"source": [
"# Create a dictionary of keys (1 - 10) and values (key squared) using dictionary comprehension\n",
"squares_dict = {num: num*num for num in range(1, 11)}\n",
"print(squares_dict)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Distance between the Earth and the other planets in the Solar System (KM):\n",
"{'Mercury': 91691000, 'Venus': 41400000, 'Mars': 78340000, 'Jupiter': 628730000, 'Saturn': 1275000000, 'Uranus': 2723950000, 'Neptune': 4351400000}\n",
"\n",
"Distance between the Earth and the other planets in the Solar System (AU):\n",
"{'Mercury': 0.61, 'Venus': 0.28, 'Mars': 0.52, 'Jupiter': 4.2, 'Saturn': 8.52, 'Uranus': 18.21, 'Neptune': 29.09}\n"
]
}
],
"source": [
"# Create a dictionary of average distance (in km) of planets from the Earth\n",
"earth_planets_distance_km_dict = {\n",
" \"Mercury\": 91_691_000, \n",
" \"Venus\": 41_400_000, \n",
" \"Mars\": 78_340_000, \n",
" \"Jupiter\": 628_730_000, \n",
" \"Saturn\": 1_275_000_000, \n",
" \"Uranus\": 2_723_950_000, \n",
" \"Neptune\": 4_351_400_000\n",
"}\n",
"print(f'Distance between the Earth and the other planets in the Solar System (km):\\n{earth_planets_distance_km_dict}\\n')\n",
"\n",
"# Convert to AU using dictionary comprehension\n",
"km_to_au = 149_600_000\n",
"earth_planets_distance_au_dict = { planet: round((km / km_to_au), 2) for (planet, km) in earth_planets_distance_km_dict.items() }\n",
"print(f'Distance between the Earth and the other planets in the Solar System (AU):\\n{earth_planets_distance_au_dict}')"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Distance between the Earth and our closest planets in the Solar System (KM):\n",
"{'Mercury': 91691000, 'Venus': 41400000, 'Mars': 78340000}\n",
"\n"
]
}
],
"source": [
"# Create a dictionary of planets within 100km of the Earth\n",
"planetary_neighbour_limit_km = 100_000_000\n",
"earth_closest_planets_dict = { k:v for (k, v) in earth_planets_distance_km_dict.items() if v < planetary_neighbour_limit_km }\n",
"print(f'Distance between the Earth and our closest planets in the Solar System (km):\\n{earth_closest_planets_dict}')"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Distance between the Earth and the other planets in the Solar System (km for inner planets and AU for outer planets):\n",
"{'Mercury': 91691000, 'Venus': 41400000, 'Mars': 78340000, 'Jupiter': 4.2, 'Saturn': 8.52, 'Uranus': 18.21, 'Neptune': 29.09}\n"
]
}
],
"source": [
"# Create a dictionary of average planetary distances from the Earth, using km for the inner planets and AU for the outer planets\n",
"km_to_au = 149_600_000\n",
"inner_planet_limit_km = 100_000_000\n",
"earth_planets_km_au_dict = { k: ( v if v < inner_planet_limit_km else round((v / km_to_au), 2) ) for (k, v) in earth_planets_distance_km_dict.items() }\n",
"print(f'Distance between the Earth and the other planets in the Solar System (km for inner planets and AU for outer planets):\\n{earth_planets_km_au_dict}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2.9. Filtering Dictionaries"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Venus': 41400000, 'Mercury': 91691000, 'Mars': 78340000}\n"
]
}
],
"source": [
"# Create a dictionary of average distance (in km) of planets from the Earth\n",
"earth_planets_distance_km_dict = {\n",
" \"Mercury\": 91_691_000, \n",
" \"Venus\": 41_400_000, \n",
" \"Mars\": 78_340_000, \n",
" \"Jupiter\": 628_730_000, \n",
" \"Saturn\": 1_275_000_000, \n",
" \"Uranus\": 2_723_950_000, \n",
" \"Neptune\": 4_351_400_000\n",
"}\n",
"\n",
"# Filter to keep only the inner planets\n",
"inner_planets = [\"Mercury\", \"Venus\", \"Earth\", \"Mars\"]\n",
"earth_inner_planets_distance_km_dict = { key: earth_planets_distance_km_dict[key] for key in set(inner_planets).intersection(earth_planets_distance_km_dict.keys()) }\n",
"print(earth_inner_planets_distance_km_dict)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2.10. Copying Dictionaries"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Mercury': 91691000, 'Venus': 41400000, 'Mars': 78340000, 'Jupiter': 628730000, 'Saturn': 1275000000, 'Uranus': 2723950000, 'Neptune': 4351400000}\n",
"{'Mercury': 91691000, 'Venus': 41400000, 'Mars': 78340000, 'Jupiter': 628730000, 'Saturn': 1275000000, 'Uranus': 2723950000, 'Neptune': 29.09, 'Earth': 0}\n",
"{'Mercury': 91691000, 'Venus': 41400000, 'Mars': 78340000, 'Jupiter': 628730000, 'Saturn': 1275000000, 'Uranus': 2723950000, 'Neptune': 29.09, 'Earth': 0}\n"
]
}
],
"source": [
"# Create a dictionary of average distance (in km) of planets from the Earth\n",
"earth_planets_distance_km_dict = {\n",
" \"Mercury\": 91_691_000, \n",
" \"Venus\": 41_400_000, \n",
" \"Mars\": 78_340_000, \n",
" \"Jupiter\": 628_730_000, \n",
" \"Saturn\": 1_275_000_000, \n",
" \"Uranus\": 2_723_950_000, \n",
" \"Neptune\": 4_351_400_000\n",
"}\n",
"print(earth_planets_distance_km_dict)\n",
"\n",
"# Create a shallow copy of this dictionary\n",
"shallow_copy_dict = earth_planets_distance_km_dict\n",
"shallow_copy_dict[\"Earth\"] = 0\n",
"\n",
"# Modify the original dictionary and observe changes to the shallow copy\n",
"earth_planets_distance_km_dict[\"Neptune\"] = 29.09\n",
"print(earth_planets_distance_km_dict)\n",
"print(shallow_copy_dict)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Mercury': 91691000, 'Venus': 41400000, 'Mars': 78340000, 'Jupiter': 628730000, 'Saturn': 1275000000, 'Uranus': 2723950000, 'Neptune': 4351400000}\n",
"{'Mercury': 91691000, 'Venus': 41400000, 'Mars': 78340000, 'Jupiter': 628730000, 'Saturn': 1275000000, 'Uranus': 2723950000, 'Neptune': 29.09}\n",
"{'Mercury': 91691000, 'Venus': 41400000, 'Mars': 78340000, 'Jupiter': 628730000, 'Saturn': 1275000000, 'Uranus': 2723950000, 'Neptune': 4351400000, 'Earth': 0}\n"
]
}
],
"source": [
"import copy\n",
"\n",
"# Create a dictionary of average distance (in km) of planets from the Earth\n",
"earth_planets_distance_km_dict = {\n",
" \"Mercury\": 91_691_000, \n",
" \"Venus\": 41_400_000, \n",
" \"Mars\": 78_340_000, \n",
" \"Jupiter\": 628_730_000, \n",
" \"Saturn\": 1_275_000_000, \n",
" \"Uranus\": 2_723_950_000, \n",
" \"Neptune\": 4_351_400_000\n",
"}\n",
"print(earth_planets_distance_km_dict)\n",
"\n",
"# Create a deep copy of this dictionary\n",
"deep_copy_dict = copy.deepcopy(earth_planets_distance_km_dict)\n",
"deep_copy_dict[\"Earth\"] = 0\n",
"\n",
"# Modify the original dictionary and observe no changes to the deep copy\n",
"earth_planets_distance_km_dict[\"Neptune\"] = 29.09\n",
"print(earth_planets_distance_km_dict)\n",
"print(deep_copy_dict)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2.11. Nested Dictionaries"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{1: {1: 1, 2: 2, 3: 3, 4: 4, 5: 5},\n",
" 2: {1: 2, 2: 4, 3: 6, 4: 8, 5: 10},\n",
" 3: {1: 3, 2: 6, 3: 9, 4: 12, 5: 15},\n",
" 4: {1: 4, 2: 8, 3: 12, 4: 16, 5: 20},\n",
" 5: {1: 5, 2: 10, 3: 15, 4: 20, 5: 25}}\n"
]
}
],
"source": [
"import pprint\n",
"\n",
"# Create a nested dictionary\n",
"nested_multiplication_table_2d_dict = {\n",
" 1: {\n",
" 1:1, 2:2, 3:3, 4:4, 5:5\n",
" }, \n",
" 2: {\n",
" 1:2, 2:4, 3:6, 4:8, 5:10\n",
" }, \n",
" 3: {\n",
" 1:3, 2:6, 3:9, 4:12, 5:15\n",
" }, \n",
" 4: {\n",
" 1:4, 2:8, 3:12, 4:16, 5:20\n",
" }, \n",
" 5: {\n",
" 1:5, 2:10, 3:15, 4:20, 5:25\n",
" }\n",
"}\n",
"pprint.pprint(nested_multiplication_table_2d_dict)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{1: {1: 1, 2: 2, 3: 3, 4: 4, 5: 5},\n",
" 2: {1: 2, 2: 4, 3: 6, 4: 8, 5: 10},\n",
" 3: {1: 3, 2: 6, 3: 9, 4: 12, 5: 15},\n",
" 4: {1: 4, 2: 8, 3: 12, 4: 16, 5: 20},\n",
" 5: {1: 5, 2: 10, 3: 15, 4: 20, 5: 25}}\n"
]
}
],
"source": [
"# Create the same nested dictionary using dictionary comprehension\n",
"nested_multiplication_table_2d_dict = { key1: {key2: key1 * key2 for key2 in range(1, 6)} for key1 in range(1, 6) }\n",
"pprint.pprint(nested_multiplication_table_2d_dict)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2.12. Dictionaries from Tuples"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('Russia', 17098242), ('Canada', 9984670), ('United States', 9857348), ('China', 9596961)]\n",
"{'Canada': 9984670,\n",
" 'China': 9596961,\n",
" 'Russia': 17098242,\n",
" 'United States': 9857348}\n"
]
}
],
"source": [
"# Create a list of tuples containing countries and their size in km^2\n",
"country_area_tuples = [(\"Russia\", 17_098_242), (\"Canada\", 9_984_670), (\"United States\", 9_857_348), (\"China\", 9_596_961)]\n",
"print(country_area_tuples)\n",
"\n",
"# Convert this list of tuples to an equivalent dictionary\n",
"country_area_dict = { key[0]: key[1] for key in country_area_tuples}\n",
"pprint.pprint(country_area_dict)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2.13. Sparse Matrix"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The value of the element at row 0 and column 1 is 1\n",
"The value of the element at row 0 and column 3 is 0\n",
"The value of the element at row 2 and column 1 is 0\n",
"The value of the element at row 2 and column 2 is 2\n",
"The value of the element at row 4 and column 1 is 3\n"
]
}
],
"source": [
"# Create a sparse matrix with a dictionary of tuple keys\n",
"sparse_matrix_dict = {(0, 1): 1, (2, 2): 2, (4, 1): 3}\n",
"print(f'The value of the element at row 0 and column 1 is {sparse_matrix_dict.get((0, 1), 0)}')\n",
"print(f'The value of the element at row 0 and column 3 is {sparse_matrix_dict.get((0, 3), 0)}')\n",
"print(f'The value of the element at row 2 and column 1 is {sparse_matrix_dict.get((2, 1), 0)}')\n",
"print(f'The value of the element at row 2 and column 2 is {sparse_matrix_dict.get((2, 2), 0)}')\n",
"print(f'The value of the element at row 4 and column 1 is {sparse_matrix_dict.get((4, 1), 0)}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2.14. Common Dictionary Methods"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Jupiter': 628730000,\n",
" 'Mars': 78340000,\n",
" 'Mercury': 91691000,\n",
" 'Neptune': 4351400000,\n",
" 'Saturn': 1275000000,\n",
" 'Uranus': 2723950000,\n",
" 'Venus': 41400000}\n",
"\n",
"{1: {1: 1, 2: 2, 3: 3, 4: 4, 5: 5},\n",
" 2: {1: 2, 2: 4, 3: 6, 4: 8, 5: 10},\n",
" 3: {1: 3, 2: 6, 3: 9, 4: 12, 5: 15},\n",
" 4: {1: 4, 2: 8, 3: 12, 4: 16, 5: 20},\n",
" 5: {1: 5, 2: 10, 3: 15, 4: 20, 5: 25}}\n",
"{}\n",
"\n",
"{'Jupiter': 628730000,\n",
" 'Mars': 78340000,\n",
" 'Mercury': 91691000,\n",
" 'Neptune': 4351400000,\n",
" 'Saturn': 1275000000,\n",
" 'Uranus': 2723950000,\n",
" 'Venus': 41400000}\n",
"\n",
"{2: None, 3: None, 5: None, 7: None, 11: None}\n",
"{2: 'prime', 3: 'prime', 5: 'prime', 7: 'prime', 11: 'prime'}\n",
"\n",
"41400000\n",
"None\n",
"0\n",
"\n",
"dict_items([('Mercury', 91691000), ('Venus', 41400000), ('Mars', 78340000), ('Jupiter', 628730000), ('Saturn', 1275000000), ('Uranus', 2723950000), ('Neptune', 4351400000)])\n",
"\n",
"dict_keys(['Mercury', 'Venus', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune'])\n",
"\n",
"4351400000\n",
"0\n",
"\n",
"{'Mercury': 91691000, 'Venus': 41400000, 'Mars': 78340000, 'Jupiter': 628730000, 'Saturn': 1275000000, 'Uranus': 2723950000, 'Earth': 0}\n",
"{'Mercury': 91691000, 'Venus': 41400000, 'Mars': 78340000, 'Jupiter': 628730000, 'Saturn': 1275000000, 'Uranus': 2723950000}\n",
"\n",
"4970600000\n",
"{'Mercury': 91691000, 'Venus': 41400000, 'Mars': 78340000, 'Jupiter': 628730000, 'Saturn': 1275000000, 'Uranus': 2723950000, 'Pluto': 4970600000}\n",
"\n",
"{'Asteroid Belt': 390400000,\n",
" 'Jupiter': 628730000,\n",
" 'Mars': 78340000,\n",
" 'Mercury': 91691000,\n",
" 'Pluto': 4970600000,\n",
" 'Saturn': 1275000000,\n",
" 'Sun': 151460000,\n",
" 'Uranus': 2723950000,\n",
" 'Venus': 41400000}\n",
"\n",
"dict_values([91691000, 41400000, 78340000, 628730000, 1275000000, 2723950000, 4970600000, 151460000, 390400000])\n"
]
}
],
"source": [
"import pprint\n",
"\n",
"# Create a dictionary\n",
"earth_planets_distance_km_dict = {\n",
" \"Mercury\": 91_691_000, \n",
" \"Venus\": 41_400_000, \n",
" \"Mars\": 78_340_000, \n",
" \"Jupiter\": 628_730_000, \n",
" \"Saturn\": 1_275_000_000, \n",
" \"Uranus\": 2_723_950_000, \n",
" \"Neptune\": 4_351_400_000\n",
"}\n",
"pprint.pprint(earth_planets_distance_km_dict)\n",
"print()\n",
"\n",
"# dict.clear()\n",
"nested_multiplication_table_2d_dict = { key1: {key2: key1 * key2 for key2 in range(1, 6)} for key1 in range(1, 6) }\n",
"pprint.pprint(nested_multiplication_table_2d_dict)\n",
"nested_multiplication_table_2d_dict.clear()\n",
"pprint.pprint(nested_multiplication_table_2d_dict)\n",
"print()\n",
"\n",
"# dict.copy()\n",
"copy_dict = earth_planets_distance_km_dict.copy()\n",
"pprint.pprint(copy_dict)\n",
"print()\n",
"\n",
"# dict.fromkeys(keys, value)\n",
"keys = [2, 3, 5, 7, 11]\n",
"prime_dict = dict.fromkeys(keys)\n",
"pprint.pprint(prime_dict)\n",
"\n",
"v = \"prime\"\n",
"labelled_prime_dict = dict.fromkeys(keys, v)\n",
"pprint.pprint(labelled_prime_dict)\n",
"print()\n",
"\n",
"# dict.get(key, default)\n",
"print(earth_planets_distance_km_dict.get(\"Venus\"))\n",
"print(earth_planets_distance_km_dict.get(\"Earth\"))\n",
"print(earth_planets_distance_km_dict.get(\"Earth\", 0))\n",
"print()\n",
"\n",
"# dict.items()\n",
"print(earth_planets_distance_km_dict.items())\n",
"print()\n",
"\n",
"# dict.keys()\n",
"print(earth_planets_distance_km_dict.keys())\n",
"print()\n",
"\n",
"# dict.pop(key, default)\n",
"print(earth_planets_distance_km_dict.pop(\"Neptune\"))\n",
"print(earth_planets_distance_km_dict.pop(\"Earth\", 0))\n",
"print()\n",
"\n",
"# dict.popitem()\n",
"earth_planets_distance_km_dict[\"Earth\"] = 0\n",
"print(earth_planets_distance_km_dict)\n",
"earth_planets_distance_km_dict.popitem()\n",
"print(earth_planets_distance_km_dict)\n",
"print()\n",
"\n",
"# dict.setdefault(key, value)\n",
"pluto_average_distance_km = earth_planets_distance_km_dict.setdefault(\"Pluto\", 4_970_600_000)\n",
"print(pluto_average_distance_km)\n",
"print(earth_planets_distance_km_dict)\n",
"print()\n",
"\n",
"# dict.update(iterable)\n",
"dict_updates = {\"Sun\": 151_460_000, \"Asteroid Belt\": 390_400_000}\n",
"earth_planets_distance_km_dict.update(dict_updates)\n",
"pprint.pprint(earth_planets_distance_km_dict)\n",
"print()\n",
"\n",
"# dict.values()\n",
"print(earth_planets_distance_km_dict.values())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2.15. Common Dictionary Functions"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Asteroid Belt': 390400000,\n",
" 'Jupiter': 628730000,\n",
" 'Mars': 78340000,\n",
" 'Mercury': 91691000,\n",
" 'Pluto': 4970600000,\n",
" 'Saturn': 1275000000,\n",
" 'Sun': 151460000,\n",
" 'Uranus': 2723950000,\n",
" 'Venus': 41400000}\n",
"9\n"
]
}
],
"source": [
"# len()\n",
"pprint.pprint(earth_planets_distance_km_dict)\n",
"print(len(earth_planets_distance_km_dict))"
]
}
],
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}