Files
004_comission/jzzzz123/course_materials/Topic_2_Data_Structures_(with answers).ipynb
louiscklaw c0b4825bea update,
2025-01-31 21:12:16 +08:00

6391 lines
161 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "geOMOVqbWt5E"
},
"source": [
"\n",
"# Data Structures\n",
"\n",
"\n",
"Most of the data types we have encountered so far are **atomic types** (except strings). Atomic data types cannot be broken down into smaller components.\n",
"\n",
"\n",
"\n",
"In many applications, data is related in some way, and should be organized in some structure that mirrors the semantics of data:"
]
},
{
"cell_type": "markdown",
"metadata": {
"cell_style": "split",
"id": "6T83x0VPhhFu"
},
"source": [
"- A shopping cart of items\n",
"\n",
"- A gradebook for a class\n",
"\n",
"- A person's demographic characteristics\n",
"\n",
"- Members of an online community\n",
"\n",
"- Districts of Hong Kong\n",
"\n",
"- Pixels of an image\n",
"\n",
"……\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"cell_style": "split",
"id": "NqZiuIKLhhFv"
},
"source": [
"\n",
"<img src=\"https://raw.githubusercontent.com/justinjiajia/img/master/python/sample-matrix.gif\" width=500/>\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_5wCvr4bhhFw"
},
"source": [
"In programming, we use **data structures** to pack related data together.\n",
"\n",
"In simple terms, a data structure refers to a **container** that organizes a **collection** of data in a particular structure.\n",
"\n",
"In Python, the four common data structures are:\n",
"\n",
"- Lists and tuples (sequential containers)\n",
"- Dictionaries (associative containers)\n",
"- Sets (set containers)\n",
"\n",
"Like numbers, strings, and Booleans, they are built-in data types in Python.\n",
"\n",
"<br>\n",
"\n",
"\n",
"> In programming, it is important to understand what questions we are trying to ask of our data and pick a data structure that can answer these questions quickly."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "WB8fjyIFsmkJ"
},
"source": [
"\n",
"<br>\n",
"\n",
"# 1 Lists and Tuples\n",
"\n",
"Both lists and tuples are data structures containing a **sequence** (an **ordered collection**) of objects (of any type), and can be created with a construct known as a **list**/**tuple display**:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "vWIrm62usmkK",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "a8ee3377-7a0a-4214-a2f2-be0d92e78ebe"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['apple', 'orange', 'banana', 'mango']"
]
},
"metadata": {},
"execution_count": 6
}
],
"source": [
"fruits = ['apple', 'orange', 'banana', 'mango']\n",
"fruits"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "center",
"id": "x1VQ5fhahhFz",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "1f693ec7-5c1f-44e4-c852-a55077100bda"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"list"
]
},
"metadata": {},
"execution_count": 3
}
],
"source": [
"type(fruits)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "center",
"id": "ehC3qi82smkR",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "56d17648-3f3f-4703-8b6a-fe2fcfc8846f"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(1, 4, 9, 16, 25)"
]
},
"metadata": {},
"execution_count": 4
}
],
"source": [
"squares = (1, 4, 9, 16, 25) # Paratheses can be dropped; squares = 1, 4, 9, 16, 25\n",
"squares"
]
},
{
"cell_type": "code",
"source": [
"squares = 1, 4, 9, 16, 25\n",
"squares"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "_9mGNMmaxZ4V",
"outputId": "cc644eb3-5f09-48d8-8514-fe41aad31bc3"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(1, 4, 9, 16, 25)"
]
},
"metadata": {},
"execution_count": 6
}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "qN4-FpWGsmkZ",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "7fab3b47-32fa-4931-c54b-953e32fd8674"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"tuple"
]
},
"metadata": {},
"execution_count": 5
}
],
"source": [
"type(squares)"
]
},
{
"cell_type": "markdown",
"source": [
"To create a tuple with only one item, you need to add a comma after the item, otherwise Python will not recognize the variable as a tuple."
],
"metadata": {
"id": "L_Y5J4mwgJcO"
}
},
{
"cell_type": "code",
"source": [
"height = (175,)\n",
"height"
],
"metadata": {
"id": "td7hBe8NgLeb",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "ad98efbe-f83d-4a28-ba52-200b796c46fb"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(175,)"
]
},
"metadata": {},
"execution_count": 7
}
]
},
{
"cell_type": "code",
"source": [
"height = (175)\n",
"height"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "I8bNA4bixnTe",
"outputId": "d792a3ff-61af-4a7a-da78-a40869f0a643"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"175"
]
},
"metadata": {},
"execution_count": 8
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3dtDT246smkd"
},
"source": [
"The physical content of a list or a tuple consists of **object references** (i.e., the address of the memory location where the object is allocated) rather than actual objects:\n",
"\n",
"\n",
"<img src=\"https://raw.githubusercontent.com/justinjiajia/img/master/python/list.png\" width=300 style=\"float: left; margin-bottom: 1.5em; margin-top: 1.5em; margin-right: 10%; \" /><img src=\"https://raw.githubusercontent.com/justinjiajia/img/master/python/tuple.png\" width=300 style=\"float: left; margin-top: 1.5em;\"/>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "QFrNiiiesmkf"
},
"source": [
"The elements of a list or a tuple can be of varying types:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "Hn1NS5YOsmkf"
},
"outputs": [],
"source": [
"mixed_list = ['Mike', 1.83, True]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "29MXB2h-hhGA"
},
"outputs": [],
"source": [
"mixed_tuple = ('spam', 2, False)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "YTWf6R8psmkj"
},
"source": [
"Both lists and tuples are ***nestable***:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "Cpll5RPWWt5L",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "6f3c99aa-3899-47d1-a484-8978483b7d07"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[['apple', 'orange', 'banana', 'mango'], [2.0, True]]"
]
},
"metadata": {},
"execution_count": 11
}
],
"source": [
"nested_list = [fruits, [2.0, True]]\n",
"nested_list"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "TGK2YcWeWt5M",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "b2f0e954-bf55-4a27-bba4-6ed57b4e2da7"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"((1, 4, 9, 16, 25), ('spam', 9), True)"
]
},
"metadata": {},
"execution_count": 12
}
],
"source": [
"nested_tuple = squares, ('spam', 9), True\n",
"nested_tuple"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "z0H3Uk7Ysmkq"
},
"source": [
"\n",
"\n",
"<img src=\"https://raw.githubusercontent.com/justinjiajia/img/master/python/nested_list.png\" width=320 style=\"float: left; margin-bottom: 1.5em; margin-top: 1.5em; margin-right: 10%; \"/><img src=\"https://raw.githubusercontent.com/justinjiajia/img/master/python/nested_tuple.png\" width=355 style=\"float: left; margin-top: 1.5em;\"/>"
]
},
{
"cell_type": "markdown",
"source": [
"*Exercise:* create a nested list/tuple which contains the information about a particular student, for example, the first element is his/her name, the second element is a sub-list/sub-tuple that contain his/her grades on math and english."
],
"metadata": {
"id": "tx8_oGh5cHAg"
}
},
{
"cell_type": "code",
"source": [
"#write your codes here\n",
"['Charles',[90, 70]]"
],
"metadata": {
"id": "Ib17mT1ycKDX",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "f4111756-bdda-47f4-df20-cebb8ccd7791"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['Charles', [90, 70]]"
]
},
"metadata": {},
"execution_count": 13
}
]
},
{
"cell_type": "code",
"source": [
"('Jane',(100, 95))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "MZIesejNzEdB",
"outputId": "3e5434ce-5b84-4405-d933-4b5d048661b3"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('Jane', (100, 95))"
]
},
"metadata": {},
"execution_count": 14
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ze1Ovz_lsmk1"
},
"source": [
"<br>\n",
"\n",
"## 1.1 Indexing\n",
"\n",
"\n",
"As a sequence maintains a left-to-right order among its elements, the elements can be indexed by **integers** (representing positions in the sequence) and individually accessed by using the indexing operator (`[]` that encloses an integer). The elements of a list or a tuple can be indexed positionally in the same way as the characters in a string.\n",
"\n",
"Python supports both positive indexing and negative indexing.\n",
"\n",
"The set for positive indexing contains the integers $0, 1, \\dots,$ and $n-1$ (**0-based indexing**), while that for negative indexing contains the integers $-1, -2, \\dots,$ and $-n$.\n",
"\n",
"<img src=\"https://raw.githubusercontent.com/justinjiajia/img/master/python/list_indexing.png\" width=380/>\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "center",
"id": "vcqYdEwtsmk2"
},
"outputs": [],
"source": [
"fruits = ['apple', 'orange', 'banana', 'mango']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "57whCZP5Wt5N",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 36
},
"outputId": "9a811a3a-0cd6-458a-8347-19cb1ff877de"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'banana'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 16
}
],
"source": [
"fruits[2]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Za2gVr3cXSPW",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 36
},
"outputId": "ee02c87c-eced-4f47-c47e-792221c1a8f3"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'orange'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 17
}
],
"source": [
"fruits[-3]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "center",
"id": "H79kQ1Hqsmk-"
},
"outputs": [],
"source": [
"squares = (1, 4, 9, 16, 25)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "Fs2b6g-TWt5O",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "59934400-5f1a-478e-ee39-cb01dbcf8e13"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"16"
]
},
"metadata": {},
"execution_count": 19
}
],
"source": [
"squares[3]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "5YxTouUcWt5O",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "3916a95a-ffdc-493b-fbec-dd3e517b29fd"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"25"
]
},
"metadata": {},
"execution_count": 20
}
],
"source": [
"squares[-1]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "csWHd3_vsmlS"
},
"source": [
"Accessing items in a subsequence can be done by simply appending additional indicies:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "WraxiKVHsmlS"
},
"outputs": [],
"source": [
"nested_list = [['apple', 'orange', 'banana', 'mango'], [2.0, True]]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "al9lEobGWt5P",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "fcda206e-d5bb-49a5-ded5-13d288a3f652"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['apple', 'orange', 'banana', 'mango']"
]
},
"metadata": {},
"execution_count": 22
}
],
"source": [
"nested_list[-2]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "uuT843uoWt5P",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 36
},
"outputId": "9165c518-ac38-4ada-bc8c-6cddc72a6649"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'apple'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 23
}
],
"source": [
"nested_list[-2][0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "8Smo4Fx0smlb"
},
"outputs": [],
"source": [
"nested_tuple = (1, 4, 9, 16, 25), ('spam', 9), True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "S7HCbbqPWt5Q",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 36
},
"outputId": "3618d4a4-c74d-4152-cf03-6e9634dc0eed"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'spam'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 25
}
],
"source": [
"nested_tuple[1][-2]"
]
},
{
"cell_type": "markdown",
"source": [
"*Exercise*: Write code to access `'m'` from `nested_tuple`"
],
"metadata": {
"id": "R3om_Fu0G9uV"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "XB90fz1TWt5Q",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 36
},
"outputId": "d91c5cec-88f3-4004-880e-40ceb12dd070"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'m'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 26
}
],
"source": [
"# Write your code here\n",
"nested_tuple[1][-2][-1]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "aoY4Si2gsmlr"
},
"source": [
"**<font color='steelblue' > Question</font>**: What will be the result of `fruits[-2][3]`?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "fHbJ2u6jY6uT"
},
"outputs": [],
"source": [
"fruits = ['apple', 'orange', 'banana', 'mango']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "RxaOnTyyaD7E",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 36
},
"outputId": "2f03e1ee-42a8-448e-8708-c65be9b10f51"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'a'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 28
}
],
"source": [
"fruits[-2][3]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rZYCQRNfsmlx"
},
"source": [
"<br>\n",
"\n",
"## 1.2 Slicing\n",
"\n",
"\n",
"\n",
"<img src=\"https://blog.tecladocode.com/content/images/size/w1000/2019/04/citric-citrus-color-997725.jpg\" width=400 />\n",
"<Br>\n",
" \n",
"\n",
"Indexing is limited to accessing one element at a time.\n",
"\n",
"**Slicing**, on the other hand, can extract a **segment** of a sequence (called a **slice**).\n",
"\n",
"The slice operator also works with lists and tuples as it does with strings. The slicing operator `[i:j]` returns the part of the list from the element indexed by `i` to the element indexed by `j`, ***including the first but excluding the last***:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "spsID6vvWt5S"
},
"outputs": [],
"source": [
"fruits = ['apple', 'orange', 'banana', 'mango']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "YPn1zK2RhhGq",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "0b161dc7-6dc7-4cab-f39e-0265c97f8a4b"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['orange']"
]
},
"metadata": {},
"execution_count": 30
}
],
"source": [
"# still return a list\n",
"fruits[1:2]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "NE-rbMS7Wt5T"
},
"outputs": [],
"source": [
"squares = 1, 4, 9, 16, 25"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "nX8Fw3Q-sml6",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "ba8919be-13a3-4c9a-aeba-0dbfd61aeb93"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(9,)"
]
},
"metadata": {},
"execution_count": 32
}
],
"source": [
"# still return a sequence\n",
"squares[-3:-2]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "R6QhmUnlb2jL"
},
"source": [
"In slicing, either or both of the two indexes can be dropped. If the 1st index is omitted, the slice starts at the beginning of the list; if the 2nd index is omitted, the slice goes to the end of the list:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "B4E-L_eusml1",
"scrolled": true,
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "09ba70b1-3f48-4040-dd16-308ba061bddc"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['apple', 'orange']"
]
},
"metadata": {},
"execution_count": 33
}
],
"source": [
"fruits[:2] # The slice starts at the beginning"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Mi5dWDvRY6uV",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "1a2102ae-4311-4083-a832-602f814de855"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['banana', 'mango']"
]
},
"metadata": {},
"execution_count": 34
}
],
"source": [
"fruits[2:] # The slice goes to the end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "k9YODAiysml9",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "f14b45ac-193a-4a81-cc52-959da1e61ec9"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(1, 4, 9, 16, 25)"
]
},
"metadata": {},
"execution_count": 35
}
],
"source": [
"squares[:] # The slice starts at the beginning and goes to the end"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "TZhnqGj7smmB"
},
"source": [
"<br>\n",
"\n",
"## 1.3 Working with Operators and Built-in functions\n",
"\n",
"Several Python operators and built-in functions can also be used with lists and tuples (in ways analogous to strings):\n",
"\n",
"- The `+` operator concatenates lists or tuples, while the `*` operator repeats a list or a tuple a given number of times:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "hc8xrHnfsmmC",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "7629830b-243a-4e51-ed22-927a73af2194"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['apple', 'orange', 'banana', 'mango', True]"
]
},
"metadata": {},
"execution_count": 36
}
],
"source": [
"fruits + [True]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "FFtynsAqsmmD",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "226c5070-cc59-4554-a7b4-9286c933e751"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(1, 4, 9, 16, 25, 30, False)"
]
},
"metadata": {},
"execution_count": 37
}
],
"source": [
"squares + (30, False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "32rTfa49smmF",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "545ebd64-1cd1-430b-92ec-2253cc10ea52"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[1, 2, 3, 1, 2, 3, 1, 2, 3]"
]
},
"metadata": {},
"execution_count": 38
}
],
"source": [
"[1, 2, 3] * 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "AHXuQLBzsmmH",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "91e5cea7-8075-445d-baf7-b4a99ea9bcdc"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('spam', 2, True, 'spam', 2, True)"
]
},
"metadata": {},
"execution_count": 39
}
],
"source": [
"('spam', 2, True) * 2"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "yLZuU-PSsmmJ"
},
"source": [
"- The operators `in` and `not in` do membership tests and return `True` or `False`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "BQePVG6EsmmL",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "947112e6-eba8-406d-d288-ba8ae167ee97"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"execution_count": 40
}
],
"source": [
"30 not in fruits"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "btHhs599smmM",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "c4d93a5b-2d5d-423e-92f7-b9185001ceec"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {},
"execution_count": 41
}
],
"source": [
"'spa' in ('spam', 2, True) # 'spam' is a member of ('spam', 2, True) but not 'spa'"
]
},
{
"cell_type": "code",
"source": [
"'spam' in ('spam', 2, True)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4mB-lu5l3lIm",
"outputId": "1b2f9006-2283-4a1c-db2f-811b8034c991"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"execution_count": 42
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3Qg617cLsmmO"
},
"source": [
"- These two sequence types also support comparisons (using `<`, `>`, `==`, `>=`, `<=`, and `!=`). In particular, lists and tuples are compared lexicographically using comparison of corresponding elements:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "Y9OrMvtesmmP",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "f4f571ea-fba6-4694-9a10-e332fafd9f65"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {},
"execution_count": 43
}
],
"source": [
"['Mike', 1.83, True] <= ['Mike', 1.80, False]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "2X1sJUDfsmmS",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "d8497118-ac6c-49c3-fcbe-c50ccf72a7d0"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"execution_count": 44
}
],
"source": [
"('spam', 2, False) <= ('spam', 2, True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "center",
"id": "fYIDLf5FsmmV",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "56cc0d10-90a6-44d0-ec85-4eb7d29580cd"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {},
"execution_count": 45
}
],
"source": [
"['Mike', 1.83, True] <= ['Mike', 1.80]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Imh00eHcsmmY",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 141
},
"outputId": "99acfc54-33ad-49d2-e290-95fc07132863"
},
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "'<=' not supported between instances of 'int' and 'str'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-46-1a12f170eb6e>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m(\u001b[0m\u001b[0;34m'spam'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m<=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'spam'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'2'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# cannot compare between different types\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: '<=' not supported between instances of 'int' and 'str'"
]
}
],
"source": [
"('spam', 2, False) <= ('spam', '2', True) # cannot compare between different types"
]
},
{
"cell_type": "markdown",
"source": [
"*Exercise:* Predict which is larger, ['large',10, 'big'] or ['small',1, 'little'] ? Write codes to verify your prediction.\n",
"\n",
"How can you re-arrange the items in the list so that the result would change?"
],
"metadata": {
"id": "w4ssOkWKilkf"
}
},
{
"cell_type": "code",
"source": [
"# write your codes here\n",
"['large',10, 'big'] < ['small',1, 'little']"
],
"metadata": {
"id": "Prqa1lePixpH",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "6f433318-d052-4342-9b64-536421dcb953"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"execution_count": 47
}
]
},
{
"cell_type": "code",
"source": [
"[10,'large', 'big'] < [1,'small', 'little']"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "a7Yvzqg54gJz",
"outputId": "7af7c6f2-d3c2-4f63-9cf0-1409abeb0427"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {},
"execution_count": 48
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ljaSPUeFsmmp"
},
"source": [
"\n",
"\n",
"- [`len()`](https://docs.python.org/3/library/functions.html#len) returns the number of elements in a list or a tuple:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "COjAprS7smmq",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "c7f0db8f-19cc-41ea-c687-1373e63e0275"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"4"
]
},
"metadata": {},
"execution_count": 49
}
],
"source": [
"len(fruits)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "rBXAHpsfsmmt",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "9e8e5c2f-e960-4d36-a019-b12972b96ad6"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"5"
]
},
"metadata": {},
"execution_count": 50
}
],
"source": [
"len(squares)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "oHxGJ5HFRWEn",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "1501de35-465f-4c8e-f982-19ff4246121c"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[['apple', 'orange', 'banana', 'mango'], [2.0, True]]"
]
},
"metadata": {},
"execution_count": 51
}
],
"source": [
"nested_list"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "H7zrNw9fRWEp",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "c33a7e14-c197-41b3-c2c4-ada47c5c7af7"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"((1, 4, 9, 16, 25), ('spam', 9), True)"
]
},
"metadata": {},
"execution_count": 53
}
],
"source": [
"nested_tuple"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "l4kuDQzqWt5a",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "b6ec9831-e012-470f-89ff-8cd753e12ae9"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"2"
]
},
"metadata": {},
"execution_count": 52
}
],
"source": [
"len(nested_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "rrztaT6kWt5b",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "bffea2e6-19b4-4582-a176-07a6e27a692c"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"3"
]
},
"metadata": {},
"execution_count": 54
}
],
"source": [
"len(nested_tuple)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_r8H9gbmsmmx"
},
"source": [
"- [`max()`](https://docs.python.org/3/library/functions.html#max) ([`min()`](https://docs.python.org/3/library/functions.html#min)) returns the largest (smallest) element. Can work with elements of comparable types:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "JKqf-UzZsmmy",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 36
},
"outputId": "b8253e35-d628-4cb5-fab2-ac3a5b293916"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'orange'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 55
}
],
"source": [
"max(fruits) # based on lexicographic order"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "T83bKfwAsmm0",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "d3f24599-c888-4801-a666-ca44939fef6b"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"1"
]
},
"metadata": {},
"execution_count": 56
}
],
"source": [
"min(squares)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "jmxohMk5Wt5d",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 141
},
"outputId": "6a594ae0-41ea-4c1f-8a9f-25a4011d8fe4"
},
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "'>' not supported between instances of 'bool' and 'str'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-57-009b5d963fb9>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'spam'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'2'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mTrue\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;31mTypeError\u001b[0m: '>' not supported between instances of 'bool' and 'str'"
]
}
],
"source": [
"max(('spam', '2', True))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1eKJ2S9Tsmm2"
},
"source": [
"- [`sum()`](https://docs.python.org/3/library/functions.html#sum) returns the sum of all elements in a sequence. Can only work with numeric elements:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "Ib2WG_0qsmm2",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "84f82764-8cae-4cc9-8f95-907697a90777"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"55"
]
},
"metadata": {},
"execution_count": 58
}
],
"source": [
"sum(squares)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "7TL7PE1esmm4",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 141
},
"outputId": "aa81d95c-9293-4aac-ec72-0812a4c2b9af"
},
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "unsupported operand type(s) for +: 'int' and 'str'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-59-42ba78dc6ca7>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfruits\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'"
]
}
],
"source": [
"sum(fruits)"
]
},
{
"cell_type": "markdown",
"source": [
"*Exercise:* I have a list of student grades [90, 80, 75, 88, 67].Try to find the average grade of all students in the student grades list/tuple by using sum() and len()"
],
"metadata": {
"id": "N2saKdyMkMxP"
}
},
{
"cell_type": "code",
"source": [
"# write your codes here\n",
"sum([90, 80, 75, 88, 67])/len( [90, 80, 75, 88, 67])"
],
"metadata": {
"id": "KsnLBNZ0kLtY",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "189be95c-9218-465e-fa11-3fc5a0bd3994"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"80.0"
]
},
"metadata": {},
"execution_count": 60
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0OAW-rrpsmnA"
},
"source": [
"<br>\n",
"\n",
"## 1.4 Common List and Tuple Methods\n",
"\n",
"- `s.index(x[, i[, j]])` returns index of the first occurrence of `x` in `s` (at or after index `i` and before index `j`):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "Gp60RjjpsmnB",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "c83e2e49-4da4-404b-a623-653a0c10e98f"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['Mike', 1.83, True, 'Mike', 1.83, True]"
]
},
"metadata": {},
"execution_count": 61
}
],
"source": [
"repeated_list = ['Mike', 1.83, True] * 2\n",
"repeated_list"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "KDAWbqFGsmnD",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "dc25b6b3-5884-4db5-bd64-fd6a393f6a54"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('spam', 2, True, 'spam', 2, True, 'spam', 2, True)"
]
},
"metadata": {},
"execution_count": 62
}
],
"source": [
"repeated_tuple = ('spam', 2, True) * 3\n",
"repeated_tuple"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "EnufXB0iY6uf",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "1be52fb3-5340-4663-fe34-48fab445dd94"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"2"
]
},
"metadata": {},
"execution_count": 63
}
],
"source": [
"repeated_list.index(True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "AMSdOJjdsmnC",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "4b07d455-5896-468d-dda5-37f9dc4e9bc9"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"5"
]
},
"metadata": {},
"execution_count": 64
}
],
"source": [
"repeated_list.index(True, 3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "dHso_HLZsmnE",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "2986375b-d59c-406c-99f4-5675aa593c05"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"3"
]
},
"metadata": {},
"execution_count": 65
}
],
"source": [
"repeated_tuple.index('spam', 1, 4)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5x8cgt8lsmnH"
},
"source": [
"- `s.count(x)` returns the total number of occurrences of `x` in `s`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "Mcz91bqtsmnH",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "46343732-f45b-461a-ebcf-78e4462b58b6"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"2"
]
},
"metadata": {},
"execution_count": 66
}
],
"source": [
"repeated_list.count(True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "ucU9eHnPlV2K",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "7c42815c-7f13-49b5-fd84-b48c341b1c56"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"3"
]
},
"metadata": {},
"execution_count": 67
}
],
"source": [
"repeated_tuple.count('spam')"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IENczqRvsmnM"
},
"source": [
"- Use `dir()` to display all the names accessible to a list or a tuple:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "ncU4VkdUsmnO",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "a27e5c6a-8ca9-4126-b05f-50365bb1a100"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['__add__',\n",
" '__class__',\n",
" '__class_getitem__',\n",
" '__contains__',\n",
" '__delattr__',\n",
" '__delitem__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__getitem__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__iadd__',\n",
" '__imul__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__iter__',\n",
" '__le__',\n",
" '__len__',\n",
" '__lt__',\n",
" '__mul__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__reversed__',\n",
" '__rmul__',\n",
" '__setattr__',\n",
" '__setitem__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" 'append',\n",
" 'clear',\n",
" 'copy',\n",
" 'count',\n",
" 'extend',\n",
" 'index',\n",
" 'insert',\n",
" 'pop',\n",
" 'remove',\n",
" 'reverse',\n",
" 'sort']"
]
},
"metadata": {},
"execution_count": 68
}
],
"source": [
"dir(repeated_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "gqBPp6GrsmnQ",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "ab6e320b-c02d-4bbe-b8c6-0dad164577e4"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['__add__',\n",
" '__class__',\n",
" '__class_getitem__',\n",
" '__contains__',\n",
" '__delattr__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__getitem__',\n",
" '__getnewargs__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__iter__',\n",
" '__le__',\n",
" '__len__',\n",
" '__lt__',\n",
" '__mul__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__rmul__',\n",
" '__setattr__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" 'count',\n",
" 'index']"
]
},
"metadata": {},
"execution_count": 69
}
],
"source": [
"dir(repeated_tuple)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0EL2_EU3smnS"
},
"source": [
"<br>\n",
"\n",
"## 1.5 Lists are Mutable\n",
"\n",
"Once a list is created, elements can be added, deleted, shifted, and moved around at will.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "f6Znvo_0sBsi"
},
"source": [
"\n",
"### 1.5.1 Item Assignment\n",
"\n",
"\n",
"We can use indexing on the left side of an assignment to identify the element to be modified:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "2RPSofPXsmnS"
},
"outputs": [],
"source": [
"fruits = ['apple', 'orange', 'banana', 'mango']\n",
"fruits"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "8RtdapZTsmnU",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "b7041d6e-9054-4399-b174-c811fe2caf41"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['apple', 'melon', 'banana', 'mango']"
]
},
"metadata": {},
"execution_count": 70
}
],
"source": [
"fruits[1] = 'melon' # item assignment\n",
"fruits"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "v7YAXktSsmnX"
},
"source": [
"<img src=\"https://raw.githubusercontent.com/justinjiajia/img/master/python/list_v1.png\" width=280/>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Qc687JeHsmnv"
},
"source": [
"### 1.5.2 Deleting List Elements\n",
"\n",
"List elements can be deleted with [the `del` statement](https://docs.python.org/3/reference/simple_stmts.html#del):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "LNrXQbj5smnw"
},
"outputs": [],
"source": [
"del fruits[2:]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "8Xjzu9GDRWFt",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "26f4f10e-a758-424f-cde5-21361fa6df7d"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['apple', 'melon']"
]
},
"metadata": {},
"execution_count": 72
}
],
"source": [
"fruits"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dZX29V-J07JM"
},
"source": [
"\n",
"### 1.5.3 Methods That Modify a List\n",
"\n",
"Python provides several built-in methods that can be used to modify lists:"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fRzTKJzjsmoe"
},
"source": [
"- [`sort(key=None, reverse=False)`](https://docs.python.org/3/library/stdtypes.html#list.sort) sorts the list in ascending order ***in place*** (i.e., modifies the original list directly):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "grBplHk-smoe"
},
"outputs": [],
"source": [
"fruits = ['apple', 'orange', 'banana', 'mango']\n",
"fruits.sort()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "XXC4eDOysmog",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "55d25235-e1e3-4e6f-b401-924975009ea7"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['apple', 'banana', 'mango', 'orange']"
]
},
"metadata": {},
"execution_count": 74
}
],
"source": [
"fruits"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Dp8XqRW_smol"
},
"source": [
"If `reverse` is set to `True`, the list elements are sorted as if each comparison were reversed:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "-8_MZ7lEsmom",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "d88397aa-1ae3-4b11-a771-6c55199d4436"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['orange', 'mango', 'banana', 'apple']"
]
},
"metadata": {},
"execution_count": 75
}
],
"source": [
"fruits.sort(reverse=True)\n",
"fruits"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cxAGtfr-Wt59"
},
"source": [
"If any comparison fails, the entire sort will fail:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "MiF8LGT2smoh",
"scrolled": true,
"colab": {
"base_uri": "https://localhost:8080/",
"height": 158
},
"outputId": "6dcebcc6-af98-431c-d622-9e0b613660bb"
},
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "'<' not supported between instances of 'int' and 'str'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-76-93da8e74689a>\u001b[0m in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0memployee\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'Charles'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Business Analyst'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m9\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mTrue\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[0memployee\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msort\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;31mTypeError\u001b[0m: '<' not supported between instances of 'int' and 'str'"
]
}
],
"source": [
"employee = ['Charles', 'Business Analyst', 9, True]\n",
"employee.sort()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6v_1z0uVsmon"
},
"source": [
"We can change the rule for comparison by specifying the comparison `key`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "zZFXYRRwY6ul",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "75b7585c-36f0-472c-d7f5-5c501ac436ed"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['Charles', 'Business Analyst', 9, True]"
]
},
"metadata": {},
"execution_count": 77
}
],
"source": [
"employee"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "OL2GlFA-smon",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "83620b0f-2720-4c47-df85-957ecc8b7311"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[9, 'Business Analyst', 'Charles', True]"
]
},
"metadata": {},
"execution_count": 80
}
],
"source": [
"employee.sort(key=str) #sort values of varying types as if they were all strings\n",
"employee"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "g2n4fU68smop"
},
"source": [
"`key` specifies the name of a ***1-argument*** function (`len`, `str`, `max`, etc.) to use to extract a comparison `key` from each element.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "khtn1Pw4Wt5-",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "6fb7bc12-d43f-4bdd-fcf8-b7ba7e08cf72"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['c', 'ee', 'ddd', 'bbbb', 'aaaaa']"
]
},
"metadata": {},
"execution_count": 81
}
],
"source": [
"repeated_letters = ['aaaaa', 'bbbb', 'c', 'ddd', 'ee']\n",
"repeated_letters.sort(key=len)\n",
"repeated_letters"
]
},
{
"cell_type": "markdown",
"source": [
"*Exercise:* we have a list consisting of string numbers\n",
"```python\n",
"string_numbers=['0','5','10','2','8']\n",
"```\n",
"Sort this list according to the numeric values represented by these string numbers."
],
"metadata": {
"id": "rucjyNSZr2_u"
}
},
{
"cell_type": "code",
"source": [
"# write your codes here\n",
"string_numbers=['0','5','10','2','8']\n",
"string_numbers.sort(key=int)\n",
"string_numbers"
],
"metadata": {
"id": "9_ra-Ti3r7zo",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "7c889be6-a953-4b2d-ede8-c83b5dcf43a8"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['0', '2', '5', '8', '10']"
]
},
"metadata": {},
"execution_count": 82
}
]
},
{
"cell_type": "markdown",
"source": [
"*Exercise:* Sort the following numbers by absolute value in descending order:\n",
"\n",
"```python\n",
"numbers=[0.12, 0.78, 0.5, -0.43, -0.87, 1.0, 0.64]\n",
"```"
],
"metadata": {
"id": "ODAhpqZkg9mf"
}
},
{
"cell_type": "code",
"source": [
"# write your codes here\n",
"numbers=[0.12, 0.78, 0.5, -0.43, -0.87, 1.0, 0.64]\n",
"numbers.sort(key=abs, reverse=True)\n",
"numbers"
],
"metadata": {
"id": "PpKFkyqEhTSj",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "f4152ec8-2f97-4152-d5c5-765937e33a4b"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[1.0, -0.87, 0.78, 0.64, 0.5, -0.43, 0.12]"
]
},
"metadata": {},
"execution_count": 83
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "QjJGui-tWt6A"
},
"source": [
"There is a built-in function, `sorted()`, that does the same thing but returns a new sorted list rather than modifying the original one in place:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "XCto7bMnsmop",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "733c45c6-f7f1-446b-cc54-ceb31c7f841c"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Help on built-in function sorted in module builtins:\n",
"\n",
"sorted(iterable, /, *, key=None, reverse=False)\n",
" Return a new list containing all items from the iterable in ascending order.\n",
" \n",
" A custom key function can be supplied to customize the sort order, and the\n",
" reverse flag can be set to request the result in descending order.\n",
"\n"
]
}
],
"source": [
"help(sorted)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "pm1oU8lFsmox",
"scrolled": true,
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "706682d7-08cc-4685-bef1-a6d07de8018d"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['apple', 'banana', 'mango', 'orange']"
]
},
"metadata": {},
"execution_count": 85
}
],
"source": [
"sorted(fruits) # note that this would not modify the original list"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "U4hP1LzrWt6B",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "b0188418-3d36-4472-b7c0-688744536f0f"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['orange', 'mango', 'banana', 'apple']"
]
},
"metadata": {},
"execution_count": 86
}
],
"source": [
"fruits"
]
},
{
"cell_type": "markdown",
"source": [
"*Exercise:* Two words are anagrams if you can rearrange the letters from one to spell the other. For example, \"fried\" and \"fired\" are anagrams (similarly, \"race\" and \"care\").\n",
"\n",
"Given two arbitrary strings, write codes to decide whether these two strings are anagrams."
],
"metadata": {
"id": "N7e1NT25yroq"
}
},
{
"cell_type": "code",
"source": [
"#write your codes here\n",
"s1 = \"race\"\n",
"s2 = \"care\"\n",
"sorted(s1) == sorted(s2)"
],
"metadata": {
"id": "jKNFLU3yyv4X",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "e96e18a0-658b-4c8d-9d03-026407919043"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"execution_count": 89
}
]
},
{
"cell_type": "markdown",
"source": [
"- `insert(index, object)` takes an element and insert it at a particular index. The return value is `None` (this applies to all following methods unless explicitly mentioned):"
],
"metadata": {
"id": "mkfrB0D4tyTS"
}
},
{
"cell_type": "code",
"source": [
"fruits = ['peach', 'melon']"
],
"metadata": {
"id": "JoKGiV0It1Pp"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"fruits.insert(2, 'peach')"
],
"metadata": {
"id": "-3jNSSNOt24t"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"fruits"
],
"metadata": {
"id": "siD4I17ft7Z3",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "e9dbf8b6-dfed-45e4-9e7a-eb1fa0f3b621"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['peach', 'melon', 'peach']"
]
},
"metadata": {},
"execution_count": 92
}
]
},
{
"cell_type": "code",
"source": [
"fruits.insert(2, ['olive', 'banana']); fruits"
],
"metadata": {
"id": "B_arTjOpt9LR",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "9b3b806f-8e97-4005-da89-3d258432e6d2"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['peach', 'melon', ['olive', 'banana'], 'peach']"
]
},
"metadata": {},
"execution_count": 93
}
]
},
{
"cell_type": "markdown",
"source": [
"\n",
"- `append(object)` takes an element and adds it to the end of a list:"
],
"metadata": {
"id": "e4UH6yNIuO9Y"
}
},
{
"cell_type": "code",
"source": [
"fruits.append('plum'); fruits"
],
"metadata": {
"id": "n2MgWRN3uhGk",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "2a005051-8708-40fb-8526-65e0013bc6c7"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['peach', 'melon', ['olive', 'banana'], 'peach', 'plum']"
]
},
"metadata": {},
"execution_count": 94
}
]
},
{
"cell_type": "code",
"source": [
"fruits.append(['litchi', 'banana']); fruits"
],
"metadata": {
"id": "TPwHw0nguir8",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "12f5417f-e29a-4e99-f3c9-d74ed62ec6c6"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['peach', 'melon', ['olive', 'banana'], 'peach', 'plum', ['litchi', 'banana']]"
]
},
"metadata": {},
"execution_count": 95
}
]
},
{
"cell_type": "markdown",
"source": [
"*Exercise:* Define a list `L1 = [\"bacon\", \"eggs\"]`.\n",
"\n",
"predict the output of the following codes\n",
"```python\n",
"L1.append(\"juice\")\n",
"or\n",
"L1+'juice'\n",
"or\n",
"L1+['juice']\n",
"or\n",
"L1.insert(2,'juice')\n",
"or\n",
"L1.insert(2,['juice'])\n",
"```"
],
"metadata": {
"id": "5LqL_0uWushe"
}
},
{
"cell_type": "code",
"source": [
"L1 = [\"bacon\", \"eggs\"]\n",
"# write codes to verify your prediction here\n"
],
"metadata": {
"id": "vWg8olLivCeU"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "pTMyUNaPsmny"
},
"source": [
"### 1.5.4 Tuples and Strings are Immutable"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "gABrejziWt5o",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 141
},
"outputId": "ed5d8f0a-e259-4abc-b090-5506b3397159"
},
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "'str' object does not support item assignment",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-96-beb867b7dabe>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m'spam'\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'u'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment"
]
}
],
"source": [
"'spam'[2] = 'u'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "NJOSNasCsmny",
"scrolled": true,
"colab": {
"base_uri": "https://localhost:8080/",
"height": 141
},
"outputId": "17824074-0df5-43e6-f747-6132664db102"
},
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "'tuple' object does not support item assignment",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-97-4891aef20299>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msquares\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
]
}
],
"source": [
"squares[2] = 3"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "FBxHg68gWt5t"
},
"source": [
"\n",
"## 1.6 Conversions\n",
"\n",
"We can convert between the different sequence types easily by using the type functions (e.g., `list()` and `tuple()`) to cast sequences to the desired types:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "JG8OCZoJsmpJ",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "6da99323-7ec3-4f37-a1c6-0163c42c6d60"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[1, 4, 9, 16, 25]"
]
},
"metadata": {},
"execution_count": 98
}
],
"source": [
"list((1, 4, 9, 16, 25))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "vrngCHCQsmpK",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "fd05d4cd-75af-472d-9aaa-fdfbbec47d33"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['s', 't', 'r', 'i', 'n', 'g']"
]
},
"metadata": {},
"execution_count": 99
}
],
"source": [
"list('string')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "3MCjdaRSsmpL",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "ca393999-5401-4806-ead3-0257cc7b86a1"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('apple', 'orange', 'banana', 'mango')"
]
},
"metadata": {},
"execution_count": 100
}
],
"source": [
"tuple(['apple', 'orange', 'banana', 'mango'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "rzYFzfK1smpM",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "831e0650-42a6-4fe4-f175-1979027abf70"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('P', 'y', 't', 'h', 'o', 'n')"
]
},
"metadata": {},
"execution_count": 101
}
],
"source": [
"tuple('Python')"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "iW6GH28LsmpN"
},
"source": [
"Type functions are actually constructors of objects of the corresponding types, and are also called factory functions. We will see this more clearly when we introduce classes.\n",
"\n",
"Calling a type function without an argument constructs an empty object of the corresponding type:"
]
},
{
"cell_type": "code",
"source": [
"list()"
],
"metadata": {
"id": "-YMD-AXzxz1A",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "5699ac54-aacb-4627-ee2f-4ea0d61dd405"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[]"
]
},
"metadata": {},
"execution_count": 102
}
]
},
{
"cell_type": "code",
"source": [
"tuple()"
],
"metadata": {
"id": "5tkqaC6Qx1Ym",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "088de9b3-ff3c-46c3-c113-7656c0afc7db"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"()"
]
},
"metadata": {},
"execution_count": 103
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qpeu6kEVsmoy"
},
"source": [
"\n",
"## 1.7 Unpacking Sequences\n",
"\n",
"Python has a very powerful assignment feature, called **sequence unpacking**, that allows a sequence of variables on the left of an assignment to be assigned values from a sequence on the right of the assignment:\n",
"\n",
"Unpacking is especially useful to assign values from a list/tuple to several variables."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "sKaykWy6smoy"
},
"outputs": [],
"source": [
"student = 'Bob', 19, 'Finance'\n",
"name, age, study = student"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "f7I4-flusmoz",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 36
},
"outputId": "779fb33f-c6e6-4a06-9623-5e7af11ccbb5"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'Bob'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 105
}
],
"source": [
"name"
]
},
{
"cell_type": "code",
"source": [
"age"
],
"metadata": {
"id": "wjbPjbbBecBV",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "dd4a124c-847d-4186-b73c-c077de83de1b"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"19"
]
},
"metadata": {},
"execution_count": 106
}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "Xz8vK7Mesmo0",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 36
},
"outputId": "9a813e79-0316-4b17-ef53-12cf6b12cc11"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'Finance'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 107
}
],
"source": [
"study"
]
},
{
"cell_type": "code",
"source": [
"name = student[0]\n",
"age = student[1]\n",
"study = student[2]"
],
"metadata": {
"id": "pA1algubBM7_"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "Iyi4djvYsmo6"
},
"source": [
"\n",
"This does the equivalent of several assignment statements, all on one easy line.\n",
"\n",
"Unpacking is also useful to swap the values of multiple variables:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "XVuXGENPWt6D",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "b8fa132e-c19b-45bb-e2c0-0c8c9330956b"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(19, 'Finance', 'Bob')"
]
},
"metadata": {},
"execution_count": 108
}
],
"source": [
"age, study, name"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "KcvYkvpJsmo6"
},
"outputs": [],
"source": [
"name, age, study = age, study, name"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "kiATR6YM3EK3",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "e009f5e6-fba9-4b70-a275-de01d6883ca0"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(19, 'Finance', 'Bob')"
]
},
"metadata": {},
"execution_count": 110
}
],
"source": [
"name, age, study"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "elLvgxrismo_"
},
"source": [
"Unpacking can be done ***deeply***:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "idhsHMA-smpA"
},
"outputs": [],
"source": [
"(color, (coord_x, coord_y, coord_z)) = ['red', [1.2, 2.0, 3.9]]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "jOiDNps-smpC",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "ae914443-700f-498b-bd6b-d0c6fcccdf77"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('red', 2.0)"
]
},
"metadata": {},
"execution_count": 112
}
],
"source": [
"color, coord_y"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Ex_-ZYf6Wt6F",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 36
},
"outputId": "1b7d7535-101b-4bd9-c89b-fdb874be1413"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'red'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 114
}
],
"source": [
"color"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "m9b5XjvNWt6F",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "a636e4a2-427d-4015-f62c-b5027b7ec275"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"1.2"
]
},
"metadata": {},
"execution_count": 115
}
],
"source": [
"coord_x"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EA1NXFOZsmo2"
},
"source": [
"\n",
"\n",
"When unpacking, the number of variables on the left must match the number of values in the sequence:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ncDxsDXIsmo4",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 141
},
"outputId": "d1e93c7d-7d7d-469c-a8a3-3f960e395090"
},
"outputs": [
{
"output_type": "error",
"ename": "ValueError",
"evalue": "too many values to unpack (expected 2)",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-116-655ea365627f>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstudent\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: too many values to unpack (expected 2)"
]
}
],
"source": [
"name, age = student"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "VCBeppbGsmpD"
},
"source": [
"\n",
"### The `*` Operator\n",
"\n",
"In Python, the `*` character is not only used for **multiplication** and **replication**, but also for unpacking/packing.\n",
"\n",
"- When used before a name on the left of an assignment, it creates a variable that gathers up any superfluous elements during sequence unpacking:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "IlhRHnposmpD"
},
"outputs": [],
"source": [
"numbers = (1, 2, 3, 4, 5)\n",
"first, *rest = numbers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "nqhd3R5qRWG_",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "c09afa66-9a59-41bb-aad7-e2dd6b88c709"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"1"
]
},
"metadata": {},
"execution_count": 4
}
],
"source": [
"first"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "vkuiL8fDRWHC",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "45652abd-834b-405c-d5c3-51c9a01990dd"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[2, 3, 4, 5]"
]
},
"metadata": {},
"execution_count": 5
}
],
"source": [
"rest"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "MKK6z4uGWt6H",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "060b172c-64fb-45b6-cd11-b443796b6830"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[2, 3, 4]"
]
},
"metadata": {},
"execution_count": 6
}
],
"source": [
"first, *middle, last = numbers\n",
"middle"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "DIFslb7ZsmpE",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "73685925-14cc-430e-fe04-857fa7b91c86"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('a', ['b'], 'c')"
]
},
"metadata": {},
"execution_count": 7
}
],
"source": [
"head, *body, tail = 'abc'\n",
"head, body, tail"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ksMYBWCHsmpG"
},
"source": [
"> The starred variable always ends up ***containing a list***.\n",
"\n"
]
},
{
"cell_type": "markdown",
"source": [
"\n",
"**<font color='steelblue' >Question</font>**: What does `first`, `remaining`, `others` hold after evaluating the following?\n",
"\n",
"```python\n",
"fruits = ['apple', 'orange', 'banana', 'mango']\n",
"((first, *remaining), *others) = fruits\n",
"```"
],
"metadata": {
"id": "rJwY7896fPbp"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "8HA9Q1OQYyZx"
},
"outputs": [],
"source": [
"# write code here to verify\n",
"fruits = ['apple', 'orange', 'banana', 'mango']\n",
"((first, *remaining), *others) = fruits"
]
},
{
"cell_type": "code",
"source": [
"first"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"id": "LA1g1oiBO1BK",
"outputId": "58168afb-7bac-492a-fcce-aae07c43153b"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'a'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 9
}
]
},
{
"cell_type": "code",
"source": [
"remaining"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "JdFunpXzO2mu",
"outputId": "afc3ea90-d625-4cde-e556-eaafdf692d0c"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['p', 'p', 'l', 'e']"
]
},
"metadata": {},
"execution_count": 10
}
]
},
{
"cell_type": "code",
"source": [
"others"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "toJYVmELO4Mw",
"outputId": "10d4e470-db77-4571-8b23-8b0c26b51ec5"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['orange', 'banana', 'mango']"
]
},
"metadata": {},
"execution_count": 11
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fV2RsdkNMgoy"
},
"source": [
"- When used before a sequence inside a list or tuple display, `*` unpacks its individual values:\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "km2mWBQVR3rS",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "66ea2816-8b02-438f-af37-34fc288e5988"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"([2, 3, 4, 5], 'a')"
]
},
"metadata": {},
"execution_count": 12
}
],
"source": [
"rest, first"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "CQq3XN2wRiCI",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "2fef3576-7893-4a8f-de4d-d1a150ba0f64"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(2, 3, 4, 5, 'a')"
]
},
"metadata": {},
"execution_count": 13
}
],
"source": [
"*rest, first"
]
},
{
"cell_type": "code",
"source": [
"numbers"
],
"metadata": {
"id": "TKfvEu9ex1jW",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "e1f21ae9-d9f5-4ba5-fa5a-0150550283a4"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(1, 2, 3, 4, 5)"
]
},
"metadata": {},
"execution_count": 14
}
]
},
{
"cell_type": "code",
"source": [
"body"
],
"metadata": {
"id": "Q6tD8RL2x2if",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "3e20f21f-12ff-4e31-cfd2-6ee0f1ed936d"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['b']"
]
},
"metadata": {},
"execution_count": 15
}
]
},
{
"cell_type": "code",
"source": [
"[numbers, body]"
],
"metadata": {
"id": "1cUTTYzJx4Kj",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "1aaba1c2-e212-42a0-85c9-73a2830b23d0"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[(1, 2, 3, 4, 5), ['b']]"
]
},
"metadata": {},
"execution_count": 16
}
]
},
{
"cell_type": "code",
"source": [
"[*numbers, *body]"
],
"metadata": {
"id": "3adKPxR1x7-C",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "7cde5616-cb2b-4bb8-b3f3-f4d284f9b2be"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 'b']"
]
},
"metadata": {},
"execution_count": 17
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "F19zAUc6lV4L"
},
"source": [
"\n",
"## 1.8 List Comprehensions\n",
"\n",
"List comprehension offers a **shorter syntax** when you want to create a new list based on the values of an existing list.\n",
"\n",
"As **expressions** that implement iteration protocol in Python, **comprehensions** allow a collection to be built from another collection by\n",
"\n",
"- iterating over the items in the source collection in turn;\n",
"- in each iteration, dispensing one item from the source collection and running the item (that passes the test specified by the predicate) through the output expression;\n",
"- and collecting all the results to form the new collection.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "reypB2y0lV4M"
},
"outputs": [],
"source": [
"a_list = [1, '4', 9, 'a', 4]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "aKB-hn9LWt6L",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "268065be-aaa4-491b-e800-5e26c812979e"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"execution_count": 2
}
],
"source": [
"isinstance(1, int) #isinstance() allows you to judge if a value's data type is int"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "BR9yhdSbRWHd",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "6310add1-5760-4e1b-aaf5-44d73baa2f57"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[1, 81, 16]"
]
},
"metadata": {},
"execution_count": 3
}
],
"source": [
"[e ** 2 for e in a_list if isinstance(e, int)]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Oqv-axMaRWHe"
},
"source": [
"<img src=\"https://raw.githubusercontent.com/justinjiajia/img/master/python/comprehension.png\" width=500/>"
]
},
{
"cell_type": "markdown",
"source": [
"> We will introduce how to use \"for\" keywords in details later."
],
"metadata": {
"id": "TKrPUgT9qupw"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "center",
"id": "ayE9FQjNRWHe",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "8c9b2886-d6de-4ba0-a444-5967db125019"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[2, '44', 18, 'aa', 8]"
]
},
"metadata": {},
"execution_count": 4
}
],
"source": [
"[e * 2 for e in a_list]"
]
},
{
"cell_type": "markdown",
"source": [
"*Exercise:* Use a list comprehension to create a new list called `slist` that contains the square of all odd numbers in `[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`."
],
"metadata": {
"id": "GBv4obW93DAM"
}
},
{
"cell_type": "code",
"source": [
"# write your codes here\n",
"nlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
"slist = [n ** 2 for n in nlist if n% 2 ==1]\n",
"slist"
],
"metadata": {
"id": "NMskYDiz3CEx",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "fe4b79f9-a7b3-4fba-9542-389074483177"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[1, 9, 25, 49, 81]"
]
},
"metadata": {},
"execution_count": 69
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "hVVD5_IshhJj"
},
"source": [
"Objects that are capable of returining its members one at a time (or over which we can iterate) is called **iterables**.\n",
"\n",
"\n",
"The input iterable can have nesting structures:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "fx4EQkZ3_8dW",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "0e554c99-c6e3-4bef-a733-f9a8b2178783"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[95, 92, 89, 100, 59]"
]
},
"metadata": {},
"execution_count": 8
}
],
"source": [
"gradebook = [['Alice', 95], ['Troy', 92], ['James', 89], ['Charles', 100], ['Bryn', 59]]\n",
"score_only = [pair[1] for pair in gradebook]\n",
"score_only"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8WCmC0LzlV4P"
},
"source": [
"\n",
"\n",
"\n",
"The assignment of each item to the **loop variable** can leverage sequence unpacking to make the handling of nested data easier:\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "fpikH322Y46U",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "55632860-3980-4815-902d-c7e185fccd95"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['Alice', 'Troy', 'James', 'Charles', 'Bryn']"
]
},
"metadata": {},
"execution_count": 10
}
],
"source": [
"gradebook = [['Alice', 95], ['Troy', 92], ['James', 89], ['Charles', 100], ['Bryn', 59]]\n",
"name_only = [name for name, score in gradebook]\n",
"name_only"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ERs8VdXUlV4Q",
"scrolled": true,
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "f33b479b-fc8b-407c-9d17-ee525a522581"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"87.0"
]
},
"metadata": {},
"execution_count": 11
}
],
"source": [
"# Calculate the average score\n",
"sum([score for name, score in gradebook])/len(gradebook)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "-FKTmVUQlV4T",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "7e8918ff-0d2c-48b2-efa8-fc9a1ab35577"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"4"
]
},
"metadata": {},
"execution_count": 12
}
],
"source": [
"# Find how many students earned scores above 80\n",
"len([score for name, score in gradebook if score > 80])"
]
},
{
"cell_type": "markdown",
"source": [
"**In-Class exercise:** Follow the logic, get average score of students whose score are below 90\n",
"\n",
"Hint:\n",
"1. find all students whose score are below 90\n",
"2. get the sum of their scores\n",
"3. get the number of such students"
],
"metadata": {
"id": "c5lJ2QvK4jbh"
}
},
{
"cell_type": "code",
"source": [
"# write your codes here\n",
"sum([score for name, score in gradebook if score < 90])/len([score for name, score in gradebook if score < 90])"
],
"metadata": {
"id": "jVrSkzsx4k_V",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "15fc55d7-3a0e-41cf-97af-733ef80eedc2"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"74.0"
]
},
"metadata": {},
"execution_count": 13
}
]
},
{
"cell_type": "markdown",
"source": [],
"metadata": {
"id": "nKuC7gkL52T_"
}
},
{
"cell_type": "markdown",
"metadata": {
"id": "wyBuwQWnlV4U"
},
"source": [
"Comprehensions also work with other collections (e.g., **dictionaries**, **sets**, etc.) as we will see."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "YgHyLb35smpR"
},
"source": [
"<br>\n",
"\n",
"# 2 Dictionaries\n",
"\n",
"\n",
"| Name | Income | Years | Criminal |\n",
"|-----|-----|-----|-----|\n",
"| Amy | 27 |4.2 | No | \n",
"| Sam | 32 |1.5 | No |\n",
"| ... |\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ZnAPkqMlWt6R"
},
"outputs": [],
"source": [
"customer_1 = ['Amy', 27, 4.2, 'No']\n",
"customer_2 = ['Sam', 32, 1.5, 'No']"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qgp3SIJZWt6R"
},
"source": [
"How can a programmer know how to obtain a person's income without knowing too much about the context/data?\n",
"\n",
"But why not attach labels to individual items to reflect the meaning of our data:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "F1fjEmxGWt6R"
},
"outputs": [],
"source": [
"customer_1 = {'name': 'Amy', 'income': 27, 'years': 4.2, 'criminal': 'No'}\n",
"customer_2 = {'name': 'Sam', 'income': 32, 'years': 1.5, 'criminal': 'No'}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Zltbs4GSWt6R"
},
"source": [
"It leads to a new data structure supported by Python, called **dictionaries**.\n",
"\n",
"A dictionary is a container that maps a set of labels called **keys** to a set of values. The dictionary is the first **compound type** that we've seen that is **not a sequence type**.\n",
"\n",
"The association of a key and a value is called a **key-value pair**, and a dictionary can be defined by using the `{key: value}` syntax:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "XEASBFL2smpR",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "dcaa5d46-0b5f-43af-fb0b-d22445f79b27"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'Alice': 95, 'Troy': 90, 'James': 89}"
]
},
"metadata": {},
"execution_count": 14
}
],
"source": [
"gradebook = {'Alice': 95, 'Troy': 90, 'James': 89}\n",
"gradebook"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "oIBhNHCHsmpS",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "fb984a4c-4fab-493d-e3da-69f67fbc9160"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"dict"
]
},
"metadata": {},
"execution_count": 15
}
],
"source": [
"type(gradebook)"
]
},
{
"cell_type": "markdown",
"source": [
"We can also create an empty dict by using d=dict()"
],
"metadata": {
"id": "nYq63ynk5tRJ"
}
},
{
"cell_type": "code",
"source": [
"newdict = dict()\n",
"type(newdict)"
],
"metadata": {
"id": "cmCvv8WS5vTR",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "67affdd8-7461-40d5-b4d1-69c6b18cfc54"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"dict"
]
},
"metadata": {},
"execution_count": 16
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZTL_-UVVsmpT"
},
"source": [
"Dictionary keys are ***unique*** and can be any ***immutable*** data type (e.g., numbers, strings, Booleans, tuples containing only immutable elements):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "XO-Iy4KwsmpU",
"scrolled": true,
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "1a8eef73-38e2-4303-d2c3-ec9e353f5b58"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{1: 'one', 2: 'dos', 3: 'tres'}"
]
},
"metadata": {},
"execution_count": 17
}
],
"source": [
"spnum = {1: 'uno', 2: 'dos', 3: 'tres', 1: 'one'}\n",
"spnum"
]
},
{
"cell_type": "code",
"source": [
"{[1]: 'uno', 2: 'dos', 3: 'tres'}"
],
"metadata": {
"id": "EhwcHgZC-ZYg",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 141
},
"outputId": "120a69d2-bdc2-4504-b27a-e9b5d66efc0c"
},
"execution_count": null,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "unhashable type: 'list'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-18-1badf3fd8cbd>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m{\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'uno'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'dos'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'tres'\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "DM33a9uQsmpY"
},
"source": [
"\n",
"## 2.1 Indexing\n",
"\n",
"Unlike **sequences**, which are indexed by a range of integers, dictionaries are ***indexed by keys***:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "WGIQX2kNsmpZ",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "5e0cd39d-738c-4538-a4f7-d83fe2727fea"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"89"
]
},
"metadata": {},
"execution_count": 19
}
],
"source": [
"# values can be accessed via keys\n",
"gradebook['James']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "yOPvz80fsmpa",
"scrolled": true,
"colab": {
"base_uri": "https://localhost:8080/",
"height": 158
},
"outputId": "f3a0365a-826c-4f9e-b0eb-a633534eb3ce"
},
"outputs": [
{
"output_type": "error",
"ename": "KeyError",
"evalue": "2",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-20-129f50d5ea3b>\u001b[0m in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# cannot be accessed by position\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mgradebook\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[0m\n\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m: 2"
]
}
],
"source": [
"# cannot be accessed by position\n",
"gradebook[2]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Nvh6eGjHsmpb"
},
"source": [
"Similar to lists, dictionaries (more precisely, dictionary values) are ***mutable*** and can grow and shrink as needed.\n",
"\n",
"Assigning a new value to an existing key updates an entry:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "nOwpX9kiWt6W",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "47d9142e-c698-4ec3-e87b-558ce3d436d9"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'Alice': 95, 'Troy': 90, 'James': 89}"
]
},
"metadata": {},
"execution_count": 21
}
],
"source": [
"gradebook"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "eRVOhjOnsmpd",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "6ccea652-d321-4e22-abd8-0c9bf2e9b075"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'Alice': 95, 'Troy': 92, 'James': 89}"
]
},
"metadata": {},
"execution_count": 22
}
],
"source": [
"gradebook['Troy'] = 92\n",
"gradebook"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "8GmjBbZ1hhJv",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "9678f45b-0180-492c-c46f-9a9b786fd8f5"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{1: 'uno', 2: 'dos', 3: 'tres'}"
]
},
"metadata": {},
"execution_count": 23
}
],
"source": [
"spnum[1] = 'uno'\n",
"spnum"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dm6gPWK8Wt6X"
},
"source": [
"Assigning a new key and value adds an entry:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "pnRQC7jJsmpb",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "061e7620-030b-426a-a1f7-a20a74e8dfa8"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'Alice': 95, 'Troy': 92, 'James': 89, 'Charles': 100}"
]
},
"metadata": {},
"execution_count": 24
}
],
"source": [
"gradebook['Charles'] = 100\n",
"gradebook"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "mAN3T2HHhhJt",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "bfd99f86-86c5-4a0d-a997-7ee4b7d59583"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{1: 'uno', 2: 'dos', 3: 'tres', 4: 'cuatro'}"
]
},
"metadata": {},
"execution_count": 25
}
],
"source": [
"spnum[4] = 'cuatro'\n",
"spnum"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "FH7T2mb9smpf"
},
"source": [
"Use the `del` statement with the key specified to delete an entry:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "sf8QmdYfsmpf",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "762efca2-a00d-4a59-f47c-7f7246c6cace"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'Alice': 95, 'Troy': 92, 'James': 89}"
]
},
"metadata": {},
"execution_count": 26
}
],
"source": [
"del gradebook['Charles']\n",
"gradebook"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "VhusMz9IhhJx",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "231d7eb4-4b17-4fc1-d931-3defd94bb838"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{1: 'uno', 2: 'dos', 3: 'tres'}"
]
},
"metadata": {},
"execution_count": 27
}
],
"source": [
"del spnum[4]\n",
"spnum"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "PHXi052tsmph"
},
"source": [
"Python dictionaries are nestable and versatile:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "s7wUyV4Fsmph",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "9ed93163-8852-4492-ef74-62ece2e30d9a"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'fname': 'Joe',\n",
" 'lname': 'Fonebone',\n",
" 'age': 51,\n",
" 'spouse': 'Edna',\n",
" 'children': ['Ralph', 'Betty', 'Joey'],\n",
" 'pets': {'dog': {'name': 'Fido', True: ['healthy', 'lovely']}, 'cat': 'Sox'},\n",
" ('email', 'mobile'): 'contact info'}"
]
},
"metadata": {},
"execution_count": 29
}
],
"source": [
"person = {'fname': 'Joe', 'lname': 'Fonebone', 'age': 51, 'spouse': 'Edna', 'children': ['Ralph', 'Betty', 'Joey'],\n",
" 'pets': {'dog': {'name': 'Fido', True: ['healthy', 'lovely']}, 'cat': 'Sox'},\n",
" ('email', 'mobile'): 'contact info'}\n",
"person"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "25BJ69hBsmpk"
},
"source": [
"Simply append additional indicies or keys to retrieve values in a subsequence or subdictionary:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ia7oPYt1smpl",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "e9aa7ff2-40da-4566-af75-ab7c81e11725"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'lovely'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 30
}
],
"source": [
"person['pets']['dog'][True][1]"
]
},
{
"cell_type": "markdown",
"source": [
"*Exercise:* Use indexing operations to:\n",
"1. print the content 'Betty'.\n",
"2. delete the entry of the 'cat'."
],
"metadata": {
"id": "BCJAJcGq7Mk1"
}
},
{
"cell_type": "code",
"source": [
"# write your codes here\n",
"person['children'][1]"
],
"metadata": {
"id": "BDd5Rcg47XKL",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "cf98b13e-ac4f-45ee-8c0e-2db129597bc2"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'Betty'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 31
}
]
},
{
"cell_type": "code",
"source": [
"del person['pets']['cat']\n",
"person"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "8kNYMlvO-heA",
"outputId": "695fee32-c286-449e-af05-de911e3c248f"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'fname': 'Joe',\n",
" 'lname': 'Fonebone',\n",
" 'age': 51,\n",
" 'spouse': 'Edna',\n",
" 'children': ['Ralph', 'Betty', 'Joey'],\n",
" 'pets': {'dog': {'name': 'Fido', True: ['healthy', 'lovely']}},\n",
" ('email', 'mobile'): 'contact info'}"
]
},
"metadata": {},
"execution_count": 32
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "yE6-9QyMsmpq"
},
"source": [
"\n",
"## 2.2 Working with Operators and Built-in Functions"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "KSQfY3YUsmpq"
},
"source": [
"Many of the operators and built-in functions that can be used with sequences work with dictionaries as well. But they operate ***primarily on keys of dictionaries***:\n",
"\n",
"- The membership operator: `in` or `not in`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "mD4eAJXBsmpq",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "3bf388e3-d6b0-46a3-a646-2d94cee9b127"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'Alice': 95, 'Troy': 92, 'James': 89}"
]
},
"metadata": {},
"execution_count": 33
}
],
"source": [
"gradebook"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "OZANsgxyhhJ4",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "63997f5b-f3d4-4d23-a1fb-48ed32fef5b3"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{1: 'uno', 2: 'dos', 3: 'tres'}"
]
},
"metadata": {},
"execution_count": 34
}
],
"source": [
"spnum"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "93FPtWKDsmpr",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "df6877bc-f7c2-4b11-9bbb-a954880d681b"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"execution_count": 35
}
],
"source": [
"'Troy' in gradebook"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "zesbqCHmsmpu",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "756f7483-2f7a-4a87-cf67-78f24da14510"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {},
"execution_count": 36
}
],
"source": [
"'dos' in spnum"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "smFX60K_smpu"
},
"source": [
"- The `*` operator:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "UOmvcShzsmpv",
"scrolled": true,
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "b312dbad-72ba-4c75-b57f-4ab7458e92c8"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('Alice', ['Troy'], 'James')"
]
},
"metadata": {},
"execution_count": 37
}
],
"source": [
"head, *middle, tail = gradebook\n",
"head, middle, tail"
]
},
{
"cell_type": "code",
"source": [
"head, *middle, tail"
],
"metadata": {
"id": "2gvSGf3zo5Wv",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "dd3cc847-cf6c-4193-e36d-92e99a16bd44"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('Alice', 'Troy', 'James')"
]
},
"metadata": {},
"execution_count": 38
}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "ZqxHc4lthhJ7",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "da3cd6ed-83ac-45dd-8e4d-bf647df586f6"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(1, [2, 3])"
]
},
"metadata": {},
"execution_count": 39
}
],
"source": [
"first, *rest = spnum\n",
"first, rest"
]
},
{
"cell_type": "code",
"source": [
"first, *rest"
],
"metadata": {
"id": "7cUbTYi2BBRu",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "8a5e0ca7-e604-4477-e3b3-2094bf872883"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(1, 2, 3)"
]
},
"metadata": {},
"execution_count": 40
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1QjonRsXsmpy"
},
"source": [
"- The following show the effects of common built-in fucntions when working with dictionaries:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "e8QJnTKIsmpy",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "adaabc7e-2784-4f72-94e5-0f0ca8d444a9"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'Troy'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 41
}
],
"source": [
"max(gradebook)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "80HODaL8smpy",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "2591c443-152a-434e-bb13-5193e876e76b"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"1"
]
},
"metadata": {},
"execution_count": 42
}
],
"source": [
"min(spnum)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "ii_AhqQ2smpz",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "ba7f36b6-2ee2-4ac1-9e0c-d74acef1d28c"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['Alice', 'James', 'Troy']"
]
},
"metadata": {},
"execution_count": 43
}
],
"source": [
"sorted(gradebook)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "tPGTMV-tsmpz",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "f1b5ef30-29dd-4679-8f8f-2bd66a557ae1"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"3"
]
},
"metadata": {},
"execution_count": 44
}
],
"source": [
"len(spnum)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dG-Ayd3-smp1"
},
"source": [
"\n",
"## 2.3 Dictionary Methods\n",
"\n",
"As with lists and tuples, there are several built-in methods that can be invoked on dictionaries.\n",
"\n",
"Call `dir()` to list all the names available for a dictionary object:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "mS6mWsD5smp2",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "016f73e7-5896-4e6e-9073-18d8e52b0bf9"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['__class__',\n",
" '__contains__',\n",
" '__delattr__',\n",
" '__delitem__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__getitem__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__iter__',\n",
" '__le__',\n",
" '__len__',\n",
" '__lt__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__reversed__',\n",
" '__setattr__',\n",
" '__setitem__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" 'clear',\n",
" 'copy',\n",
" 'fromkeys',\n",
" 'get',\n",
" 'items',\n",
" 'keys',\n",
" 'pop',\n",
" 'popitem',\n",
" 'setdefault',\n",
" 'update',\n",
" 'values']"
]
},
"metadata": {},
"execution_count": 119
}
],
"source": [
"dir(gradebook)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "RALfTHposmp3"
},
"source": [
"\n",
"\n",
"- [`.get(<key>[, <default>])`](https://docs.python.org/3/library/stdtypes.html#dict.get) returns the value for `key` if `key` is present, else `default` (defaulting to `None`):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "S_G-bh5KYgbq",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "327c6491-18e4-4742-e94b-eab66b8aa569"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'Alice': 95, 'Troy': 92, 'James': 89}"
]
},
"metadata": {},
"execution_count": 45
}
],
"source": [
"gradebook = {'Alice': 95, 'Troy': 92, 'James': 89}\n",
"gradebook"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "FXyYsHPtsmp3",
"scrolled": true,
"colab": {
"base_uri": "https://localhost:8080/",
"height": 141
},
"outputId": "b94d7189-b9a9-4fc9-b0a7-e39045b33576"
},
"outputs": [
{
"output_type": "error",
"ename": "KeyError",
"evalue": "'Bryn'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-46-7b2bc86f53f2>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mgradebook\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Bryn'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m: 'Bryn'"
]
}
],
"source": [
"gradebook['Bryn']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "h-r_AYuAsmp3"
},
"outputs": [],
"source": [
"gradebook.get('Bryn') # return nothing but won't interrupt exectution"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "dVa2J9zWsmp4",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "855f4e90-f215-47da-ca94-c0f338d92d1c"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0"
]
},
"metadata": {},
"execution_count": 48
}
],
"source": [
"gradebook.get('Bryn', 0) # return 0"
]
},
{
"cell_type": "markdown",
"source": [
"**<font color='steelblue' >Question</font>**: Given the following list:\n",
"\n",
"```python\n",
"fruits = ['apple', 'pear', 'peach', 'banana', 'apple',\n",
" 'strawberry', 'lemon', 'apple', 'blueberry', 'banana']\n",
"```\n",
"\n",
"Write code to count the number of duplicates for each unique fruit. Use a Python dictionary to maintain each pair of the fruit name and its count.\n",
"\n",
"The expected output is as follows:\n",
"\n",
"```python\n",
"{'apple': 3, 'pear': 1, 'peach': 1, 'banana': 2, 'strawberry': 1,\n",
" 'lemon': 1, 'blueberry': 1}\n",
"```"
],
"metadata": {
"id": "D0NEVia7G7cX"
}
},
{
"cell_type": "code",
"source": [
"fruits = ['apple', 'pear', 'peach', 'banana', 'apple',\n",
" 'strawberry', 'lemon', 'apple', 'blueberry', 'banana']\n",
"\n",
"# write your code below (hint: use for loop and .get() method)\n",
"\n",
"fruits_count = {}\n",
"for name in fruits:\n",
" fruits_count[name] = fruits_count.get(name, 0) + 1\n",
"fruits_count"
],
"metadata": {
"id": "FF3h3BljHDX0",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "3b14a1ad-bd8b-455e-952d-93af7f4bcbfd"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'apple': 3,\n",
" 'pear': 1,\n",
" 'peach': 1,\n",
" 'banana': 2,\n",
" 'strawberry': 1,\n",
" 'lemon': 1,\n",
" 'blueberry': 1}"
]
},
"metadata": {},
"execution_count": 49
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "w1b8ft4tsmqG"
},
"source": [
"- [`.update(<other>)`](https://docs.python.org/3/library/stdtypes.html#dict.update) merges a dictionary with\n",
"\n",
" - another dictionary;\n",
" - an object that can produce pairs of keys and values (e.g., a list of tuples that represent key-value pairs);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "1EcG1NcLsmqH",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "1b37d3d6-a10a-4147-ee99-961f2696ebf2"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{1: 'one', 2: 'dos', 3: 'tres', 4: 'four'}"
]
},
"metadata": {},
"execution_count": 50
}
],
"source": [
"spnum = {1: 'uno', 2: 'dos', 3: 'tres'}\n",
"spnum.update({1: 'one', 4: 'four'})\n",
"spnum"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "JyxxyoowsmqI",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "d5ea0a8f-01bb-4ba9-a18a-bf437e48582b"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'Alice': 95, 'Troy': 92, 'James': 89, 'Amy': 77, 'Peter': 85, 'Mike': 63}"
]
},
"metadata": {},
"execution_count": 51
}
],
"source": [
"gradebook.update({'Amy': 77, 'Peter': 85, \"Mike\": 63})\n",
"gradebook"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "MlsSnKtgsmqI",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "bc4c11cd-68d1-4c8a-fd93-c50d56a0c69b"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{1: 'uno', 2: 'dos', 3: 'tres', 4: 'cuatro'}"
]
},
"metadata": {},
"execution_count": 52
}
],
"source": [
"spnum.update([(1, 'uno'), (4, 'cuatro')]) # or [[1, 'uno'], [4, 'cuatro']]\n",
"spnum"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "RfN5bPM6RWIH"
},
"source": [
"\n",
"### Methods for Dictionary Traversals\n",
"\n",
"We'll often be in situations where we want to traverse the keys and values of a dictionary separately.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "woXSa_OPsmqP"
},
"source": [
"- [`values()`](https://docs.python.org/3/library/stdtypes.html#dict.keys) ([`keys()`](https://docs.python.org/3/library/stdtypes.html#dict.keys)) returns a **dictionary view** consisting of only `value`s (`key`s):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "CRdErOAXsmqP",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "e2801630-839e-4c3d-8b78-71a34fac7397"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"dict_values(['uno', 'dos', 'tres', 'cuatro'])"
]
},
"metadata": {},
"execution_count": 53
}
],
"source": [
"spnum.values() # a dictionary view object"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "JAa3uScHsmqP",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "6317debb-0231-4420-9070-48fafb946c34"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"dict_keys([1, 2, 3, 4])"
]
},
"metadata": {},
"execution_count": 54
}
],
"source": [
"spnum.keys()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IyIF53FPRWIM"
},
"source": [
"Dictionary views are read-only and dynamic. They will be updated when we modify the dictionary from which they are derived."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1asuTyS8smqL"
},
"source": [
"- [`items()`](https://docs.python.org/3/library/stdtypes.html#dict.items) returns a **dictionary view** consisting of `(key, value)` pairs:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "R8QULhAlRWIJ",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "2d112919-00a3-48d4-b5f2-0e11cf3317b2"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"dict_items([('Alice', 95), ('Troy', 92), ('James', 89), ('Amy', 77), ('Peter', 85), ('Mike', 63)])"
]
},
"metadata": {},
"execution_count": 55
}
],
"source": [
"gradebook.items()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "cSImmHVzsmqL",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "26a0868a-6038-4eec-f716-cf12011a3a43"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"dict_items([(1, 'uno'), (2, 'dos'), (3, 'tres'), (4, 'cuatro')])"
]
},
"metadata": {},
"execution_count": 56
}
],
"source": [
"spnum.items()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "7eZDyPcmRWIM"
},
"source": [
"Dictionary views can be iterated over to yield their respective data:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "gTYdQV_0RWIM",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "ffd1f488-9631-42d6-f309-35c388dc429e"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[('Alice', 100),\n",
" ('Troy', 97),\n",
" ('James', 94),\n",
" ('Amy', 82),\n",
" ('Peter', 90),\n",
" ('Mike', 68)]"
]
},
"metadata": {},
"execution_count": 57
}
],
"source": [
"[(name, score + 5) for name, score in gradebook.items()]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0EDgCykfRWIN"
},
"source": [
"We can use the dictionary-like syntax in comprehensions (i.e., **dictionary comprehensions**) to construct new dictionaries from existing ones:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Y2iPoaB6RWIN",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "f8c583e9-decb-43f4-c17b-f72e3e816456"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'Alice': 100, 'Troy': 97, 'James': 94, 'Amy': 82, 'Peter': 90, 'Mike': 68}"
]
},
"metadata": {},
"execution_count": 58
}
],
"source": [
"{name: score + 5 for name, score in gradebook.items()}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "l88aW2dcsmqR"
},
"source": [
"\n",
"## 2.4 Conversions\n",
"\n",
"\n",
"A dictionary can also be constructed by casting a collection of key-value pairs to the `dict` type with the `dict()` function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "center",
"id": "0ZnVvVaqsmqR",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "0413d479-7841-4ba1-feba-4a516ff0600c"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'red': 34, 'green': 30, 'brown': 31}"
]
},
"metadata": {},
"execution_count": 59
}
],
"source": [
"dict([(\"red\", 34), (\"green\", 30), (\"brown\", 31)]) # or dict(((\"red\", 34), (\"green\", 30), (\"brown\", 31)))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9RMCmL0ssmqS"
},
"source": [
"We have to be more careful when converting a dictionary to a collection of other types:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ck_LCtnQsmqS",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "84e52380-c1b7-436f-b4f4-c5cf63c953c6"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'red': 34, 'green': 30, 'brown': 31}"
]
},
"metadata": {},
"execution_count": 60
}
],
"source": [
"marbles = dict([(\"red\", 34), (\"green\", 30), (\"brown\", 31)])\n",
"marbles"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "EAuly_C1smqT",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "6d6f2ec8-85f3-412a-c8ec-a3b1f57a25fe"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['red', 'green', 'brown']"
]
},
"metadata": {},
"execution_count": 61
}
],
"source": [
"list(marbles) # the keys will be used by default"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "HYFEWGLSsmqU",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "46c0d9a7-0c12-40d7-84a0-484c6847296a"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(34, 30, 31)"
]
},
"metadata": {},
"execution_count": 62
}
],
"source": [
"# use a view to get the values\n",
"tuple(marbles.values())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "HFwi3xc4smqU",
"scrolled": true,
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "dec722f2-5722-480e-bc30-3df16fbae89a"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[('red', 34), ('green', 30), ('brown', 31)]"
]
},
"metadata": {},
"execution_count": 63
}
],
"source": [
"# or the key-value pairs\n",
"list(marbles.items())"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BtEc5PLNsmqZ"
},
"source": [
"\n",
"<br>\n",
"\n",
"# 3 Sets\n",
"\n",
"A set is an ***unordered***, ***mutable*** collection of ***distinct immutable*** elements.\n",
"\n",
"***Non-empty*** sets can be created by using the curly brace notation (`{}`):\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "RnTDmCYXWt6w",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "e5565d3d-fa0c-4e0a-faaa-6542f8e4cf3e"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{1, 2, 3, 4, 5}"
]
},
"metadata": {},
"execution_count": 1
}
],
"source": [
"integers = {5, 2, 3, 1, 4, 2}\n",
"integers # The duplicate is eliminated"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "7VMI_oEnsmqa",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "b0a2b563-0938-486f-81bf-2a4b4bcc2b07"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"set"
]
},
"metadata": {},
"execution_count": 2
}
],
"source": [
"type(integers)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "SLyPbM6zWt6w",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "700366e7-6774-4b5c-9fa7-77a362b9bc9b"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'cat', 'dog', 'pig', 'rabbit'}"
]
},
"metadata": {},
"execution_count": 3
}
],
"source": [
"mammals = {'cat', 'dog', 'rabbit', 'pig', 'cat'}\n",
"mammals # The duplicate is eliminated"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "PhQcDDY_ZX4g"
},
"source": [
"An ***empty*** set can only be defined with the `set()` function."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "i0k9cDF2ZYqo",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "b996b58e-ff92-449a-fcf3-d993a34737b7"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"dict"
]
},
"metadata": {},
"execution_count": 67
}
],
"source": [
"type({})"
]
},
{
"cell_type": "code",
"source": [
"type(set())"
],
"metadata": {
"id": "bGSB6VtzroBa",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "c6a2a81f-c2cf-4790-c585-b0045582bf2c"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"set"
]
},
"metadata": {},
"execution_count": 4
}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "18MKryUl4nEt",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "fae56f32-18f5-4761-e7a6-6c57ebde3fec"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'apple', 'banana', 'mango', 'orange'}"
]
},
"metadata": {},
"execution_count": 7
}
],
"source": [
"set(fruits * 3)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SSOOhg9Csmqc"
},
"source": [
"\n",
"\n",
"\n",
"The elements in a set can be objects of different types. But they must be ***immutable***:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "Kpy77DVssmqc",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "41422748-2427-4060-d625-b6abc75bc988"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{('Amy', True), 3.14159, 42, None, 'ham'}"
]
},
"metadata": {},
"execution_count": 8
}
],
"source": [
"{42, 'ham', 3.14159, None, ('Amy', True)}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "QhEcHsiosmqd",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 141
},
"outputId": "8fdfc344-5d74-482d-8a68-b28a8e2e3568"
},
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "unhashable type: 'list'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-9-2c778a306ebd>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m{\u001b[0m\u001b[0;36m42\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'ham'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3.14159\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'Amy'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m}\u001b[0m \u001b[0;31m# lists are mutable and cannot be the elements in a set\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
]
}
],
"source": [
"{42, 'ham', 3.14159, None, ['Amy', True]} # lists are mutable and cannot be the elements in a set"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "iuPE67Nosmqe"
},
"source": [
"\n",
"\n",
"Like other collections, sets support membership operators `in` and `not in` and built-in function `len()`:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "EX0Z4_6lsmqf",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "52d549c8-af6c-4626-c503-4008a5190c5e"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"execution_count": 10
}
],
"source": [
"'parrot' not in mammals"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cell_style": "split",
"id": "3j19HYzzsmqe",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "f2e3c096-b496-406c-f5d7-784937e72ae0"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"5"
]
},
"metadata": {},
"execution_count": 11
}
],
"source": [
"len(integers)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "r6_V3Kxmsmqf"
},
"source": [
"However, sets do not support indexing, slicing, or other ***sequence-like*** behavior, because they do not record element position or order of insertion."
]
}
],
"metadata": {
"colab": {
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 0
}