{ "cells": [ { "cell_type": "markdown", "id": "093299e2-a202-46bc-8563-51012fae421d", "metadata": {}, "source": [ "## Python language familiarity" ] }, { "cell_type": "markdown", "id": "3c7731db-72b3-4719-95ee-b28d0d82a5a8", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Comments" ] }, { "cell_type": "code", "execution_count": 1, "id": "a7d02aa0-ef59-4ade-bc64-7c6e0a9c6eb3", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "# This does nothing." ] }, { "cell_type": "markdown", "id": "4c20d4ef-c491-4739-b729-922a5a73b943", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Basic math and assignment" ] }, { "cell_type": "markdown", "id": "50951e65-71db-4664-a82f-689a1f78f959", "metadata": {}, "source": [ "#### Mathematical formula" ] }, { "cell_type": "code", "execution_count": 2, "id": "145b3c30-d3ce-4d32-9bf2-0068fac2af49", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(2 + 3) * 4" ] }, { "cell_type": "markdown", "id": "e76301f2-5d54-4d29-abd2-7d86eade9661", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Modulo operator" ] }, { "cell_type": "code", "execution_count": 3, "id": "a6dfac9c-ff04-4454-baeb-e96701124100", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "123 % 10" ] }, { "cell_type": "markdown", "id": "9be2ffeb-df5a-4530-9c03-36eae97739d0", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Exponentiation operator" ] }, { "cell_type": "code", "execution_count": 4, "id": "4cc7e96c-bb80-47ec-9fcb-6c80d2e89075", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5**2" ] }, { "cell_type": "markdown", "id": "0326f893-6f34-49e8-bc77-f7f63740d1f2", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Assigning to a variable" ] }, { "cell_type": "code", "execution_count": 5, "id": "00a4e106-5a5b-47ab-a13e-0820afd1b44d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "21" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = (2 + 3) * 4\n", "\n", "x + 1" ] }, { "cell_type": "markdown", "id": "6f1666ed-4168-4b26-8c69-6c3bedcc2de4", "metadata": {}, "source": [ "#### `del` a variable" ] }, { "cell_type": "code", "execution_count": 6, "id": "6b66d843-28c4-4ac1-8482-a0bdb3ed3ccb", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "NameError", "evalue": "name 'x' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[6], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m x\n\u001b[0;32m----> 3\u001b[0m \u001b[43mx\u001b[49m\n", "\u001b[0;31mNameError\u001b[0m: name 'x' is not defined" ] } ], "source": [ "del x\n", "\n", "x" ] }, { "cell_type": "markdown", "id": "1b5d9173-6ba6-4014-8826-4714f43f5ef7", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Undefined variable versus `None`" ] }, { "cell_type": "code", "execution_count": 7, "id": "e09f5304-9332-4e4f-947a-8a7f92182152", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "x = None\n", "\n", "x" ] }, { "cell_type": "markdown", "id": "8ad3681d-a898-40a0-ad92-04aeeef622c1", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Builtin math functions: `min` and `abs`" ] }, { "cell_type": "code", "execution_count": 8, "id": "2e05444a-adee-44ea-9b11-b997d930320e", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "-4" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = -5\n", "\n", "min(x + 1, abs(x) + 1)" ] }, { "cell_type": "markdown", "id": "c31ea66f-8971-4b42-9992-65983d065e49", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Builtin math functions: `round`" ] }, { "cell_type": "code", "execution_count": 9, "id": "6b539bdd-0eea-4343-9702-1d1faad697a6", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 3.14\n", "\n", "round(x)" ] }, { "cell_type": "markdown", "id": "121aad06-7d6f-4b5e-ab83-585f9a7df32a", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Tuple-assignment (using swap as an example)" ] }, { "cell_type": "code", "execution_count": 10, "id": "73cd5796-e4f9-437c-b77a-c57fbb6d9b36", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 5\n", "y = 10\n", "\n", "x, y = y, x\n", "\n", "x" ] }, { "cell_type": "markdown", "id": "77779b9a-5ba0-49de-b7e0-3ecd468a21f8", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Nested tuple-assignment" ] }, { "cell_type": "code", "execution_count": 11, "id": "2d054f21-b4fc-4613-8e32-42da2f67a4e7", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x, (y, z) = 1, (2, 3)\n", "\n", "z" ] }, { "cell_type": "markdown", "id": "8b6c2259-c6ef-4836-9d60-15a63d5e8eb4", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Assignment-assignment (two variables, one value)" ] }, { "cell_type": "code", "execution_count": 12, "id": "74dac7dc-b55c-46ae-aa4b-17f83a9d0d39", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = y = z = 10\n", "\n", "x" ] }, { "cell_type": "markdown", "id": "ebf23bbe-fff7-4ffc-b970-178c852ce902", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Add-assignment `+=`" ] }, { "cell_type": "code", "execution_count": 13, "id": "bdb5f539-cb11-44d9-ba3c-26b1322cd5df", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x += 10\n", "\n", "x" ] }, { "cell_type": "markdown", "id": "b64863f1-e56f-4f00-8ad3-62f478304518", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Strings" ] }, { "cell_type": "markdown", "id": "8e164c8e-569b-446a-9a75-ba59480f12e5", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### String variable" ] }, { "cell_type": "code", "execution_count": 14, "id": "4aac84ac-9edd-4bd3-80ac-7c21328a2d95", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'something'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = \"something\"\n", "\n", "x" ] }, { "cell_type": "markdown", "id": "4dce47b2-d8fe-4915-9d97-901f12f2d8c2", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `len` of a string" ] }, { "cell_type": "code", "execution_count": 15, "id": "055b5fda-0bda-4fe7-931f-e9c257b7f45e", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(x)" ] }, { "cell_type": "markdown", "id": "cb8c157c-400f-4392-9557-dd2598ed2416", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Slicing a string" ] }, { "cell_type": "code", "execution_count": 16, "id": "c27f6577-a533-43f3-94df-87fa8ae681a8", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'thing'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[4:len(x)]" ] }, { "cell_type": "markdown", "id": "61afd5c2-1fd0-4905-89ca-315d6a778be4", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Slicing a string with no end point" ] }, { "cell_type": "code", "execution_count": 17, "id": "cec29d83-398d-4bd5-9ed4-e09d504820a9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'thing'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[4:]" ] }, { "cell_type": "markdown", "id": "712356f1-9ac6-4e8b-bc6e-677d34d8e208", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `str.split`" ] }, { "cell_type": "code", "execution_count": 18, "id": "522e8cc6-4349-41fc-bd62-1498d3246da2", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['These', 'are', 'some', 'words.\\n']" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sentence = \"These are some words.\\n\"\n", "\n", "words = sentence.split(\" \")\n", "\n", "words" ] }, { "cell_type": "markdown", "id": "bda142ed-732b-4c08-9c1b-e4d637f17d27", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `str.join`" ] }, { "cell_type": "code", "execution_count": 19, "id": "ae7081ee-78d1-4690-8dca-4934c2e8a9de", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'These::are::some::words.\\n'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"::\".join(words)" ] }, { "cell_type": "markdown", "id": "a1a4e065-77a5-4a48-9590-67c2e3cbfec9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `str.rstrip`" ] }, { "cell_type": "code", "execution_count": 20, "id": "27c7ee13-aa2e-4dd0-ad20-1791f165b23a", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'These are some words.'" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sentence.rstrip(\"\\n\")" ] }, { "cell_type": "markdown", "id": "c5ffd3b5-6e4c-4dc9-b908-e88314d2f969", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `str.replace`" ] }, { "cell_type": "code", "execution_count": 21, "id": "4a6f0b60-78b5-4098-a923-0e423c311730", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'These are s()me w()rds.\\n'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sentence.replace(\"o\", \"()\")" ] }, { "cell_type": "markdown", "id": "f299e8c0-a015-498c-9a68-354399f06791", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `str.startswith`" ] }, { "cell_type": "code", "execution_count": 22, "id": "133c78cb-8257-4126-b576-91d89da283a3", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sentence.startswith(\"The\")" ] }, { "cell_type": "markdown", "id": "35bf1608-227f-456d-90d2-bd4972a1309d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `str.encode`" ] }, { "cell_type": "code", "execution_count": 23, "id": "fbfe6674-6ad2-4045-b2b5-bfa9abe6a375", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "b'A = \\xcf\\x80r\\xc2\\xb2'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "as_str = \"A = πr²\"\n", "\n", "as_bytes = as_str.encode(\"utf-8\")\n", "\n", "as_bytes" ] }, { "cell_type": "markdown", "id": "664823fd-d691-4ef7-971d-cb4f0b8bcd95", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `bytes.decode`" ] }, { "cell_type": "code", "execution_count": 24, "id": "7e57e5bd-a061-4c7f-b591-63efefa14be1", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'A = πr²'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "as_bytes.decode(\"utf-8\")" ] }, { "cell_type": "markdown", "id": "0e4fc871-f557-4e05-aa06-1495d0a6cb29", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Multiline string" ] }, { "cell_type": "code", "execution_count": 25, "id": "fee9e07e-55f9-46ab-a64e-db00a6929c17", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'This is the first line.\\nThis is the second line.\\nThis is the third line.\\nThe fourth line does not end with a newline.'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "multiline = \"\"\"This is the first line.\n", "This is the second line.\n", "This is the third line.\n", "The fourth line does not end with a newline.\"\"\"\n", "\n", "multiline" ] }, { "cell_type": "markdown", "id": "cd276dbb-da99-4753-984d-f603873a73d8", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Logical operators" ] }, { "cell_type": "markdown", "id": "608d7709-c57a-49a7-bd85-6bc31404c737", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Boolean variable" ] }, { "cell_type": "code", "execution_count": 26, "id": "1cb88303-a487-466a-981b-b0b9d7f64cdf", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "as_bool = (3 == 4)\n", "\n", "as_bool" ] }, { "cell_type": "markdown", "id": "1150f538-db2b-482b-96f4-39c729a84f89", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `not`" ] }, { "cell_type": "code", "execution_count": 27, "id": "627cc4ba-fdde-4288-8a2d-ef5d761a932a", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not False" ] }, { "cell_type": "markdown", "id": "5482680b-f628-4cdf-9ddf-6f2edf17fa5b", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `and`" ] }, { "cell_type": "code", "execution_count": 28, "id": "50bdb7d7-a066-4262-b72e-ddc429e7bc47", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and False" ] }, { "cell_type": "markdown", "id": "b5e7d152-333c-4e6b-b02d-fb430f610fce", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `or`" ] }, { "cell_type": "code", "execution_count": 29, "id": "399dac95-ce14-4777-a060-159ba5f78f38", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True or False" ] }, { "cell_type": "markdown", "id": "a8dc3779-cae4-45a0-a8e8-313532af9461", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### I/O" ] }, { "cell_type": "markdown", "id": "bc4b5cc0-0d9f-4bdc-8851-15a2294820de", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `print`" ] }, { "cell_type": "code", "execution_count": 30, "id": "6ae13ca8-a6dd-46b8-98c8-5b62f68c71e2", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Area of a circle: A = πr²\n" ] } ], "source": [ "print(\"Area of a circle:\", as_str)" ] }, { "cell_type": "markdown", "id": "f300efd8-9a8c-4ee5-9450-927c622f2074", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `print` with an f-string" ] }, { "cell_type": "code", "execution_count": 31, "id": "af0bedec-4d83-4ca1-81c8-02899e37bbb8", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Area of a circle: A = πr²\n" ] } ], "source": [ "print(f\"Area of a circle: {as_bytes.decode()}\")" ] }, { "cell_type": "markdown", "id": "4d06566b-5831-4831-ba14-d89d47521696", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `print` with an f-string that prints the expression, too" ] }, { "cell_type": "code", "execution_count": 32, "id": "ce778217-d74f-4b8c-bac1-85c5f46bd1b3", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "as_bytes.decode() = 'A = πr²'\n" ] } ], "source": [ "print(f\"{as_bytes.decode() = }\")" ] }, { "cell_type": "markdown", "id": "52ea7db2-508b-40af-9377-3bcddbbc7d3c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `input`" ] }, { "cell_type": "code", "execution_count": 33, "id": "80336c11-c52c-48ab-b639-8d945bbd86a1", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ " hello\n" ] }, { "data": { "text/plain": [ "'hello'" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "user_input = input()\n", "\n", "user_input" ] }, { "cell_type": "markdown", "id": "abe76f19-f3ea-483b-9977-fa75c5614193", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `open` file and `write` strings" ] }, { "cell_type": "code", "execution_count": 34, "id": "3d6d7926-e730-4036-beb4-f08fac6dea17", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "117" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "file = open(\"/tmp/example.txt\", \"w\")\n", "\n", "file.write(multiline)" ] }, { "cell_type": "markdown", "id": "5feb70d1-bc47-4d42-8398-ebb2b2737cb4", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `open` file and `read` strings" ] }, { "cell_type": "code", "execution_count": 35, "id": "5e4745b7-6ae9-4d3c-82ec-642c1eaf57d1", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'This is the first line.\\nThis is the seco'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "file = open(\"/tmp/example.txt\")\n", "\n", "file.read(40)" ] }, { "cell_type": "markdown", "id": "08fa6f44-905f-464c-a27b-a4b749e2c573", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `open` file and `write` bytes" ] }, { "cell_type": "code", "execution_count": 36, "id": "8987d2cf-aade-4881-b043-1efd39d9c8fa", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "file = open(\"/tmp/example.txt\", \"wb\")\n", "\n", "file.write(b\"A = \\xcf\\x80r\\xc2\\xb2\")" ] }, { "cell_type": "markdown", "id": "174624a8-d569-4b93-8139-67aa7d697f69", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `open` file and `read` bytes" ] }, { "cell_type": "code", "execution_count": 37, "id": "addcc03a-a6ed-4f0e-acd0-060071df92d3", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "b'A = \\xcf\\x80r\\xc2\\xb2'" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "file = open(\"/tmp/example.txt\", \"rb\")\n", "\n", "file.read(40)" ] }, { "cell_type": "markdown", "id": "286613bd-1d6b-4178-8f61-30409cbc755d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Branching control flow" ] }, { "cell_type": "markdown", "id": "b0c816d3-badc-4dda-ae99-3f523b7776ca", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `if` statement" ] }, { "cell_type": "code", "execution_count": 38, "id": "19a9423e-7e82-4b54-a89b-f3d34ca56edc", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yes, x is 5\n" ] } ], "source": [ "x = 5\n", "\n", "if x == 5:\n", " print(\"yes, x is 5\")\n", "\n", "if x == 6:\n", " print(\"no, x is not 6\")" ] }, { "cell_type": "markdown", "id": "319eff07-9b62-485d-8b4d-ffcbb313df07", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `if`-`else` statement" ] }, { "cell_type": "code", "execution_count": 39, "id": "1158238a-343a-4ac4-9a9d-8ee68894da24", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yes, x is something else\n" ] } ], "source": [ "if x == 4:\n", " print(\"no, x is not 4\")\n", "else:\n", " print(\"yes, x is something else\")" ] }, { "cell_type": "markdown", "id": "2305a2b8-d60b-4183-917a-c25ecba794ed", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `if`-`elif`-`else` statement" ] }, { "cell_type": "code", "execution_count": 40, "id": "ff400eeb-7b36-4a84-8615-6bfb6ca79887", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yes, x is less than 10\n" ] } ], "source": [ "if x < 0:\n", " print(\"no, x is not less than zero\")\n", "elif x < 10:\n", " print(\"yes, x is less than 10\")\n", "else:\n", " print(\"no, x is not something else\")" ] }, { "cell_type": "markdown", "id": "d018c6b4-fc46-4b1d-81ba-07e3716222b0", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Looping control flow" ] }, { "cell_type": "markdown", "id": "6f5fbdee-c9a4-4dab-bc67-3e28594c3646", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `for` loop with `range`" ] }, { "cell_type": "code", "execution_count": 41, "id": "492f4272-b08b-405d-bd61-046a7b719af2", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for x in range(10):\n", " print(x)" ] }, { "cell_type": "markdown", "id": "fb4a6fce-d252-45d9-81ab-8712442d8489", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### `for` loop with low-high `range`" ] }, { "cell_type": "code", "execution_count": 42, "id": "138eb212-06a7-4909-8f3b-59330d856e69", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for x in range(5, 10):\n", " print(x)" ] }, { "cell_type": "markdown", "id": "77c59f66-cb15-42fb-a813-fa4c8d1ac936", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `for` loop over the lines of a file" ] }, { "cell_type": "code", "execution_count": 43, "id": "5708fc1d-efb5-4318-b435-ffb2de1f140d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "line.rstrip() = 'This is the first line.'\n", "line.rstrip() = 'This is the second line.'\n", "line.rstrip() = 'This is the third line.'\n", "line.rstrip() = 'The fourth line does not end with a newline.'\n" ] } ], "source": [ "open(\"/tmp/example.txt\", \"w\").write(multiline)\n", "\n", "for line in open(\"/tmp/example.txt\"):\n", " print(f\"{line.rstrip() = }\")" ] }, { "cell_type": "markdown", "id": "035b3cc5-db59-4c6c-bb57-47a5d271dce1", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `while` loop" ] }, { "cell_type": "code", "execution_count": 44, "id": "7909da7e-f91c-4d1e-804d-08554d6b5ee5", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "x = 0\n", "\n", "while x < 10:\n", " print(x)\n", " x += 1" ] }, { "cell_type": "markdown", "id": "99c3ea71-d17b-4e24-a1fb-792a5139994f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `break`" ] }, { "cell_type": "code", "execution_count": 45, "id": "ddd3d651-520f-4ef8-8f5a-f1e1f0e01de7", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for x in range(10):\n", " if x >= 5:\n", " break\n", " print(x)" ] }, { "cell_type": "markdown", "id": "c1fd52c6-19df-499d-b6ed-0554108379c7", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `continue`" ] }, { "cell_type": "code", "execution_count": 46, "id": "e0f20819-8b33-4b7d-a898-fd171f588076", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "3\n", "5\n", "7\n", "9\n" ] } ], "source": [ "for x in range(10):\n", " if x % 2 == 0:\n", " continue\n", " print(x)" ] }, { "cell_type": "markdown", "id": "ece7c9ad-c36f-4af1-8bb9-082fc07e13de", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Function definitions" ] }, { "cell_type": "markdown", "id": "2857148e-3008-4906-8711-7420a82f9805", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Defining functions" ] }, { "cell_type": "code", "execution_count": 47, "id": "bc183ee4-49c6-4977-8856-8edfd052c10a", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def f(x, y):\n", " return x**2 + y**2\n", "\n", "f(3, 4)" ] }, { "cell_type": "markdown", "id": "2a5b6dde-565d-44bd-b5e1-a18b1577792f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Multiple `return` statements" ] }, { "cell_type": "code", "execution_count": 48, "id": "b9b2580e-a7f5-45ad-b119-11d932f9dd0e", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'positive'" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def f(x):\n", " if x < 0:\n", " return \"negative\"\n", " elif x == 0:\n", " return \"zero\"\n", " else:\n", " return \"positive\"\n", "\n", "f(3)" ] }, { "cell_type": "markdown", "id": "5299ca0a-7035-40f9-b12d-b597729bbb4c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Recursive function" ] }, { "cell_type": "code", "execution_count": 49, "id": "6d25b5e2-d825-46b0-a4fd-95bb48de9912", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "55" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def fibonacci(n):\n", " if n == 0:\n", " return 0\n", " elif n == 1:\n", " return 1\n", " else:\n", " return fibonacci(n - 1) + fibonacci(n - 2)\n", "\n", "fibonacci(10)" ] }, { "cell_type": "markdown", "id": "65ca03fd-cb8a-44b2-a9cb-694a0e890324", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Keyword argument defaults" ] }, { "cell_type": "code", "execution_count": 50, "id": "f2c44577-7797-43c3-984b-a15ba955a415", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x: 3, y: 4\n" ] }, { "data": { "text/plain": [ "25" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def f(x, y, debug=False):\n", " if debug:\n", " print(f\"x: {x}, y: {y}\")\n", " return x**2 + y**2\n", "\n", "f(3, 4, debug=True)" ] }, { "cell_type": "markdown", "id": "a682af46-c262-49aa-9fda-e3ac4f935b33", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Getting `*args` and `**kwargs`" ] }, { "cell_type": "code", "execution_count": 51, "id": "e47b1486-5dfa-42f3-8ae1-6075b21e1ff1", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "args: (1, 2)\n", "kwargs: {'three': 3, 'four': 4}\n" ] } ], "source": [ "def dynamic(*args, **kwargs):\n", " print(\"args:\", args)\n", " print(\"kwargs:\", kwargs)\n", "\n", "dynamic(1, 2, three=3, four=4)" ] }, { "cell_type": "markdown", "id": "1f3941fc-26fd-4890-9cc2-7c239c8c1849", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Function with a doc string" ] }, { "cell_type": "code", "execution_count": 52, "id": "7faa6ebb-cb86-46a4-ac2f-a8b86094e071", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function documented in module __main__:\n", "\n", "documented(x, y)\n", " This function is well-documented.\n", " \n", " As all functions should be.\n", "\n" ] } ], "source": [ "def documented(x, y):\n", " \"\"\"\n", " This function is well-documented.\n", "\n", " As all functions should be.\n", " \"\"\"\n", " return x**2 + y**2\n", "\n", "help(documented)" ] }, { "cell_type": "markdown", "id": "d31b4757-5a4c-4565-817c-e6969c392554", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Using a variable defined outside the function" ] }, { "cell_type": "code", "execution_count": 53, "id": "9d4d852f-8a97-459a-9238-e5584b2f487e", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "250" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "defined_outside = 10\n", "\n", "def f(x, y):\n", " return defined_outside * (x**2 + y**2)\n", "\n", "f(3, 4)" ] }, { "cell_type": "markdown", "id": "f4960fcc-97e3-4981-992f-4c4629cd24b1", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Changing a variable defined outside the function" ] }, { "cell_type": "code", "execution_count": 54, "id": "0f0d9e4b-e413-4142-800f-37a3a23dde29", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "2500" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "defined_outside = 10\n", "\n", "def f(x, y):\n", " global defined_outside\n", "\n", " defined_outside *= 10\n", " \n", " return defined_outside * (x**2 + y**2)\n", "\n", "f(3, 4)" ] }, { "cell_type": "markdown", "id": "cc996b95-1179-4590-a385-5d1915fd036c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Nested function definitions" ] }, { "cell_type": "code", "execution_count": 55, "id": "1e21fdb6-764b-4351-a98b-0b54f9e3a5e9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def f(x, y):\n", " def sqr(z):\n", " return z**2\n", "\n", " def add(x, y):\n", " return x + y\n", "\n", " return add(sqr(x), sqr(y))\n", "\n", "f(3, 4)" ] }, { "cell_type": "markdown", "id": "02bd637d-3467-4007-a864-a3dfd9b70401", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Returning a function from a function" ] }, { "cell_type": "code", "execution_count": 56, "id": "4d6a221a-5d3a-4891-a37f-5ee6dfe0cea3", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def incrementor(by):\n", " def increment(x):\n", " return x + by\n", "\n", " return increment\n", "\n", "increment_by_one = incrementor(1)\n", "\n", "increment_by_one(10)" ] }, { "cell_type": "markdown", "id": "cc49d9fd-902e-48d9-b0f8-eed95f8d79d6", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Passing a function to a function" ] }, { "cell_type": "code", "execution_count": 57, "id": "30eae250-ffa9-4d8d-ac89-f23fda16e10c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "340" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def integrate(function, a, b):\n", " result = 0\n", " for x in range(a, b):\n", " result += function(x)\n", " return result\n", "\n", "def linear(x):\n", " return 5 + 2*x\n", "\n", "integrate(linear, 10, 20)" ] }, { "cell_type": "markdown", "id": "46ef77ee-88d0-408a-8760-ab72bae309d9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `lambda`" ] }, { "cell_type": "code", "execution_count": 58, "id": "ddd0f1f9-95bc-411f-8675-b2d4dd0daf12", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "linear = lambda x: 5 + 2*x\n", "\n", "linear(2)" ] }, { "cell_type": "markdown", "id": "83684ba5-9f8e-4da2-bb41-a5f8852587d9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Passing a `lambda` to a function" ] }, { "cell_type": "code", "execution_count": 59, "id": "c6e3c7e7-fc7c-49e8-801b-c3c5a1daf95d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "340" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "integrate(lambda x: 5 + 2*x, 10, 20)" ] }, { "cell_type": "markdown", "id": "d6691a36-0479-4aa7-991e-4a7893b07156", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Using modules" ] }, { "cell_type": "markdown", "id": "10994164-d4e4-4713-9760-027fd714060e", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Importing modules" ] }, { "cell_type": "code", "execution_count": 60, "id": "5e753df2-d55e-4d7d-a977-775c9e8d4250", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "\n", "math.sqrt(25)" ] }, { "cell_type": "markdown", "id": "e7033bd8-34f2-4bd3-95c0-d2292d07449a", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Importing and renaming a module" ] }, { "cell_type": "code", "execution_count": 61, "id": "b50919b1-9373-4958-9757-73babbb0d4b5", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "np.sqrt(25)" ] }, { "cell_type": "markdown", "id": "c46fc922-5bcb-45b3-8cd5-26d6d560006a", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Importing a function from a module" ] }, { "cell_type": "code", "execution_count": 62, "id": "eafc43ed-5890-4aab-b2e4-fc7e1fb405b9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "120" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from math import factorial\n", "\n", "factorial(5)" ] }, { "cell_type": "markdown", "id": "6794b83d-4840-4285-9049-012bef0d45ca", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Distinction between installing and importing" ] }, { "cell_type": "code", "execution_count": 63, "id": "7c9a45ab-a48e-4393-9ed7-234dc277de76", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'package_not_installed_on_this_system'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[63], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mpackage_not_installed_on_this_system\u001b[39;00m\n", "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'package_not_installed_on_this_system'" ] } ], "source": [ "import package_not_installed_on_this_system" ] }, { "cell_type": "markdown", "id": "d5c2d5aa-579d-4f01-a900-86ca104fc8aa", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Data types" ] }, { "cell_type": "markdown", "id": "a7e762d5-bfa4-472d-bdb1-58505a6bafb0", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Adding two strings" ] }, { "cell_type": "code", "execution_count": 64, "id": "09bb11f7-bace-4e6c-a34a-00018c44fc00", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'strings can be added together'" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"strings can \" + \"be added together\"" ] }, { "cell_type": "markdown", "id": "a9c23687-61f3-4022-81f7-28dca55e9d3c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Adding an integer and a string (type error)" ] }, { "cell_type": "code", "execution_count": 65, "id": "161dbdf0-3dea-482c-af60-3c29a61d0710", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "TypeError", "evalue": "can only concatenate str (not \"int\") to str", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[65], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mbut not a string and \u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m123\u001b[39;49m\n", "\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str" ] } ], "source": [ "\"but not a string and \" + 123" ] }, { "cell_type": "markdown", "id": "34e0fb74-2b6f-4eea-8c9a-f2a21362caef", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `type`" ] }, { "cell_type": "code", "execution_count": 66, "id": "276bc144-b301-4e29-8ded-f03ed75edd28", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(\"every value has a type\")" ] }, { "cell_type": "markdown", "id": "f742833f-b0b9-4d08-84ee-2badf38a241b", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Type objects as constructors (type coersion)" ] }, { "cell_type": "code", "execution_count": 67, "id": "a98d4029-eb2b-4031-9053-d3abc32479dc", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'123'" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(123)" ] }, { "cell_type": "markdown", "id": "02d217c1-6ae3-4d75-bc19-63eb039d5dd8", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Using `float` to make infinity" ] }, { "cell_type": "code", "execution_count": 68, "id": "0338247b-06ce-4c36-bdab-2ab112db0f42", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "inf" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "123 + float(\"inf\")" ] }, { "cell_type": "markdown", "id": "ce7856d2-5358-499a-8d85-0f60fa97e980", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `isinstance`" ] }, { "cell_type": "code", "execution_count": 69, "id": "84b635e1-fa66-40e6-9357-15b150885a5d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isinstance(123, int)" ] }, { "cell_type": "markdown", "id": "6a657388-5130-45de-87b2-a4ab92f0a6c1", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `issubclass`" ] }, { "cell_type": "code", "execution_count": 70, "id": "fae4efc1-cf27-4e6f-8ffa-4ec1119e5f63", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "issubclass(np.int32, np.number)" ] }, { "cell_type": "markdown", "id": "be4518b7-736f-49bf-b2ed-8fbbd91cf886", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `mro` to list all supertypes" ] }, { "cell_type": "code", "execution_count": 71, "id": "f9c48f76-c06c-4d70-a7c7-9096ce397188", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[numpy.int32,\n", " numpy.signedinteger,\n", " numpy.integer,\n", " numpy.number,\n", " numpy.generic,\n", " object]" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.int32.mro()" ] }, { "cell_type": "markdown", "id": "9c027bf4-7e53-4b25-bde5-034d8b825cbc", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Container types" ] }, { "cell_type": "markdown", "id": "efae812f-bb6c-4cec-bd86-6ec026658c17", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Constructing a `list`" ] }, { "cell_type": "code", "execution_count": 72, "id": "8df5a6a4-f2b3-416b-ab5a-abddcf8e03f5", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9]" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_list = [0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9]\n", "\n", "some_list" ] }, { "cell_type": "markdown", "id": "78a692a9-31ec-490a-a55d-4413f8ba817b", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `type` of a `list`" ] }, { "cell_type": "code", "execution_count": 73, "id": "17619a80-d62c-4a58-8b0e-4bcaecdde995", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(some_list)" ] }, { "cell_type": "markdown", "id": "440d2caf-e70a-4842-8609-6f54afc9084d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `len` of a `list`" ] }, { "cell_type": "code", "execution_count": 74, "id": "5e750866-0819-4ba2-af32-7130a49ade41", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(some_list)" ] }, { "cell_type": "markdown", "id": "0bdf4f0c-9d33-464f-8147-c2872ebca525", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `in` for a `list`" ] }, { "cell_type": "code", "execution_count": 75, "id": "46c3c3d3-240e-4ac1-8f94-5671a96b5f9b", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.3 in some_list" ] }, { "cell_type": "markdown", "id": "b157ed1e-2edb-43ee-ac0c-f4ad4fb98e44", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Extracting an item from a `list`" ] }, { "cell_type": "code", "execution_count": 76, "id": "15986537-3935-4763-8e67-e390acdc3add", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "3.3" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_list[3]" ] }, { "cell_type": "markdown", "id": "b4530d15-fd0a-4bf0-84bf-e244a72614c8", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Extracting with a negative index" ] }, { "cell_type": "code", "execution_count": 77, "id": "2e4ecb6c-e48c-4bb9-8393-27c112ad77db", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "9.9" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_list[-1]" ] }, { "cell_type": "markdown", "id": "c7c08273-0804-4d76-b473-a3d06181aa71", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Slicing a range from a `list`" ] }, { "cell_type": "code", "execution_count": 78, "id": "3e3cbe5e-3cb8-4cb5-954a-12af7ea77f47", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[3.3, 4.4, 5.5, 6.6]" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_list[3:-3]" ] }, { "cell_type": "markdown", "id": "7acfb646-b67e-4a1e-b5b7-1458f2c0880d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Slicing a range with no starting point" ] }, { "cell_type": "code", "execution_count": 79, "id": "05a89cd5-4c23-420b-a3bd-6dea3dfa4409", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[0.0, 1.1, 2.2, 3.3, 4.4]" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_list[:5]" ] }, { "cell_type": "markdown", "id": "2843d76e-c539-4df0-9c7c-42c5e34d1b85", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Slicing a range that skips even items" ] }, { "cell_type": "code", "execution_count": 80, "id": "231f6365-bbc6-4352-83f5-95e292693309", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[0.0, 2.2, 4.4, 6.6, 8.8]" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_list[0::2]" ] }, { "cell_type": "markdown", "id": "5c55d1d9-4e34-4bb3-8ee3-53ac858e4839", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Using a slice to reverse a `list`" ] }, { "cell_type": "code", "execution_count": 81, "id": "bd1447ac-6878-4756-b76d-9b2ea042bdef", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[9.9, 8.8, 7.7, 6.6, 5.5, 4.4, 3.3, 2.2, 1.1, 0.0]" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_list[::-1]" ] }, { "cell_type": "markdown", "id": "f5762262-20bf-4880-912c-188784362ebe", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Slicing a sliced `list`" ] }, { "cell_type": "code", "execution_count": 82, "id": "827aca6f-3d6e-4c30-a741-d4c3146f6354", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "5.5" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_list[2:8][3]" ] }, { "cell_type": "markdown", "id": "39dd590d-4bee-4618-ba5d-6ad48df57b35", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Assigning to a `list` item" ] }, { "cell_type": "code", "execution_count": 83, "id": "2915b2b5-ec0a-4b1c-9b4c-03e9fee5d7f6", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[0.0, 1.1, 2.2, 33333, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9]" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_list[3] = 33333\n", "\n", "some_list" ] }, { "cell_type": "markdown", "id": "61c5e683-f32e-4e8e-b967-287c6fe2e1f4", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `del` of a `list` item" ] }, { "cell_type": "code", "execution_count": 84, "id": "ad85e59c-cd03-4645-9104-834741f42a91", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[0.0, 1.1, 2.2, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9]" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "del some_list[3]\n", "\n", "some_list" ] }, { "cell_type": "markdown", "id": "f73fbe86-cb97-40e6-b324-78bd34899a73", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `list.append` (with a different type)" ] }, { "cell_type": "code", "execution_count": 85, "id": "3214f8d7-dd1a-4caa-a8b8-c708d981022d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[0.0, 1.1, 2.2, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 'mixed types']" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_list.append(\"mixed types\")\n", "\n", "some_list" ] }, { "cell_type": "markdown", "id": "522304de-11ee-4ec6-aacc-c586e817784f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `list.extend`" ] }, { "cell_type": "code", "execution_count": 86, "id": "10a68ecf-10ce-4d68-b631-9f6567f50f15", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[0.0,\n", " 1.1,\n", " 2.2,\n", " 4.4,\n", " 5.5,\n", " 6.6,\n", " 7.7,\n", " 8.8,\n", " 9.9,\n", " 'mixed types',\n", " 'more',\n", " 'more',\n", " 'more!']" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_list.extend([\"more\", \"more\", \"more!\"])\n", "\n", "some_list" ] }, { "cell_type": "markdown", "id": "36779e6d-fc7e-40e9-b46c-70bdc3a833a5", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `list.index`" ] }, { "cell_type": "code", "execution_count": 87, "id": "58ee78b3-e920-44a3-afba-811bbf82879d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_list.index(\"more\")" ] }, { "cell_type": "markdown", "id": "b9079b1a-c588-4274-afde-448850766955", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `list.pop`" ] }, { "cell_type": "code", "execution_count": 88, "id": "2d55f43e-6dd1-47eb-b143-5e66d1a6faf1", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "more!\n", "more\n", "more\n", "mixed types\n" ] }, { "data": { "text/plain": [ "[0.0, 1.1, 2.2, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9]" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(some_list.pop())\n", "print(some_list.pop())\n", "print(some_list.pop())\n", "print(some_list.pop())\n", "\n", "some_list" ] }, { "cell_type": "markdown", "id": "a49b0a0d-d6c7-4cdd-9393-e2df92a3467b", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `sum` of a `list`" ] }, { "cell_type": "code", "execution_count": 89, "id": "d4314b7e-2b37-4df8-b1c3-e9fec0adae1b", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "46.199999999999996" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(some_list)" ] }, { "cell_type": "markdown", "id": "2b4ae21a-262a-4c79-9c15-cb9669147f6f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `sorted`" ] }, { "cell_type": "code", "execution_count": 90, "id": "15f96a33-566f-4a12-91d2-5b493b115437", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[-6, -1, -1, 2, 3, 4, 5, 7]" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "another_list = [3, -6, 5, 7, -1, -1, 2, 4]\n", "\n", "sorted(another_list)" ] }, { "cell_type": "markdown", "id": "d23a833b-4288-4602-b8a1-d56c46512ea5", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `sorted` with a `key` (`lambda`)" ] }, { "cell_type": "code", "execution_count": 91, "id": "beb7ca6a-da07-4d8a-ac2b-00838597e214", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[7, -6, 5, 4, 3, 2, -1, -1]" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(another_list, key=lambda x: -abs(x))" ] }, { "cell_type": "markdown", "id": "e7e7f839-dd07-4152-9b32-c19c066e6515", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `list` of `list`" ] }, { "cell_type": "code", "execution_count": 92, "id": "93e5a33d-4bc7-475a-8859-8d526487430d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[[1.1, 2.2, 3.3], [], [4.4, 5.5]]" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[[1.1, 2.2, 3.3], [], [4.4, 5.5]]" ] }, { "cell_type": "markdown", "id": "6a6e6a73-07f8-4af7-bbdd-7d8b8de87c7a", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `tuple`" ] }, { "cell_type": "code", "execution_count": 93, "id": "fc10024c-9c4c-4134-b085-2cbd0a16b558", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(1.1, 2.2, 3.3, 4.4, 5.5)" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "as_tuple = (1.1, 2.2, 3.3, 4.4, 5.5)\n", "\n", "as_tuple" ] }, { "cell_type": "markdown", "id": "108ff825-2032-4da9-bf68-8706c95480db", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `type` of a `tuple`" ] }, { "cell_type": "code", "execution_count": 94, "id": "c83ef4fa-8c8d-4bba-b51c-5fa2a9b40a6f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(as_tuple)" ] }, { "cell_type": "markdown", "id": "ec598736-14c1-40ce-b33b-e441e7dbf33d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Can't assign to a `tuple`" ] }, { "cell_type": "code", "execution_count": 95, "id": "a8ba8cf2-6d68-4354-aea1-799b556b99a1", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[95], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mas_tuple\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m33333\u001b[39m\n", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "source": [ "as_tuple[3] = 33333" ] }, { "cell_type": "markdown", "id": "20132eee-49bb-4321-abea-1d7e7e251d79", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `tuple` as an argument to a function" ] }, { "cell_type": "code", "execution_count": 96, "id": "0513f093-de68-409d-b396-fc9217a13fbf", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "4.4" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def function(takes_a_tuple):\n", " return takes_a_tuple[3]\n", "\n", "function((1.1, 2.2, 3.3, 4.4, 5.5))" ] }, { "cell_type": "markdown", "id": "142e6167-f09c-4a4c-b3e9-8c1e043a6d13", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `set`" ] }, { "cell_type": "code", "execution_count": 97, "id": "0ea7256b-bc9d-423a-94e1-451840d1667d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 5}" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{1, 1, 1, 2, 3, 3, 4, 4, 5}" ] }, { "cell_type": "markdown", "id": "fafa5221-a0cf-4cad-8edb-2cd8344524dc", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Constructing a `dict`" ] }, { "cell_type": "code", "execution_count": 98, "id": "cb3854da-b596-47cc-996a-faef22264b64", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "{'one': 1.1, 'two': 2.2, 'three': 3.3, 'four': 4.4, 'five': 5.5}" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_dict = {\"one\": 1.1, \"two\": 2.2, \"three\": 3.3, \"four\": 4.4, \"five\": 5.5}\n", "\n", "some_dict" ] }, { "cell_type": "markdown", "id": "4dff4df0-439d-4bfc-8153-de566fe193af", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `len` of a `dict`" ] }, { "cell_type": "code", "execution_count": 99, "id": "ba64c413-cff6-4210-8e44-e2d1516805eb", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(some_dict)" ] }, { "cell_type": "markdown", "id": "4c7f4e46-c512-4a54-9a09-cceffbe48a16", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `in` for a `dict`" ] }, { "cell_type": "code", "execution_count": 100, "id": "f00da2e9-0c7b-4054-a6bb-4ff1c1513ea0", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"two\" in some_dict" ] }, { "cell_type": "markdown", "id": "ae5c3fd8-0be4-4ca7-a31b-e95a01d9fe5c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Extracting a key from a `dict`" ] }, { "cell_type": "code", "execution_count": 101, "id": "1d4970de-f132-45ba-b3f1-38c0776d0d49", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "2.2" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_dict[\"two\"]" ] }, { "cell_type": "markdown", "id": "2afb409d-8b59-4ab7-844a-e6c47a3151df", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Extracting a key with `get`" ] }, { "cell_type": "code", "execution_count": 102, "id": "0d60fd55-5987-43b2-9daf-c70d9bbbd1b8", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'default'" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_dict.get(\"doesn't exist\", \"default\")" ] }, { "cell_type": "markdown", "id": "c2acff6c-9b2e-4460-ac57-7a49b62257a4", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Assigning to a `dict` key" ] }, { "cell_type": "code", "execution_count": 103, "id": "fbeecc06-da7c-4a1a-b676-9da733e294ef", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "{'one': 1.1, 'two': 22222, 'three': 3.3, 'four': 4.4, 'five': 5.5}" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_dict[\"two\"] = 22222\n", "\n", "some_dict" ] }, { "cell_type": "markdown", "id": "265f21f7-1cc0-4f5f-b84c-2528b68447ed", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `del` of a `dict` key" ] }, { "cell_type": "code", "execution_count": 104, "id": "85b1eabe-6df6-433b-9a43-8ca5dce930b9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "{'one': 1.1, 'three': 3.3, 'four': 4.4, 'five': 5.5}" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "del some_dict[\"two\"]\n", "\n", "some_dict" ] }, { "cell_type": "markdown", "id": "6a1a65df-3d50-4c8b-bbcf-9a04823fdb63", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `keys`" ] }, { "cell_type": "code", "execution_count": 105, "id": "5d87ff0b-aa6b-440e-afe5-6b17dde06cf8", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "dict_keys(['one', 'three', 'four', 'five'])" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_dict.keys()" ] }, { "cell_type": "markdown", "id": "8d03cb39-9245-42c4-b22c-b58b0fdaca79", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `values`" ] }, { "cell_type": "code", "execution_count": 106, "id": "5e8b3789-fee4-4872-a353-5d5178203d77", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "dict_values([1.1, 3.3, 4.4, 5.5])" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_dict.values()" ] }, { "cell_type": "markdown", "id": "d674ad03-c8d2-4fa9-b932-c3fa3f6839d2", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `items`" ] }, { "cell_type": "code", "execution_count": 107, "id": "056b6207-237c-4f35-9f41-8cff5c91255a", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "dict_items([('one', 1.1), ('three', 3.3), ('four', 4.4), ('five', 5.5)])" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_dict.items()" ] }, { "cell_type": "markdown", "id": "abe1a236-845e-405e-abc6-9a35279f6578", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `dict.pop`" ] }, { "cell_type": "code", "execution_count": 108, "id": "3d4cfafe-a8aa-4c53-b9c9-52d15b81b05d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.3\n" ] }, { "data": { "text/plain": [ "{'one': 1.1, 'four': 4.4, 'five': 5.5}" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(some_dict.pop(\"three\"))\n", "\n", "some_dict" ] }, { "cell_type": "markdown", "id": "f068799f-7e43-4821-84eb-c9f1fbdb2853", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Passing `*` of `list` to a function" ] }, { "cell_type": "code", "execution_count": 109, "id": "1f850847-e42d-48f8-8bd2-aaf09ed40861", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def f(x, y):\n", " return x**2 + y**2\n", "\n", "some_list = [3, 4]\n", "\n", "f(*some_list)" ] }, { "cell_type": "markdown", "id": "c059e020-bf2f-4a1c-a0f3-d35b98b4537f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Passing `**` of a `dict` to a function" ] }, { "cell_type": "code", "execution_count": 110, "id": "3408208f-7d79-4100-ad9e-6d2b861fb527", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_dict = {\"y\": 4, \"x\": 3}\n", "\n", "f(**some_dict)" ] }, { "cell_type": "markdown", "id": "8382e482-aad8-4d32-9cf0-58e549c34213", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Unpacking assignment with `*`" ] }, { "cell_type": "code", "execution_count": 111, "id": "b9b25d03-f36f-4686-8799-37381069f4da", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[3.3, 4.4, 5.5]" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_list = [1.1, 2.2, 3.3, 4.4, 5.5]\n", "\n", "first, second, *rest = some_list\n", "\n", "rest" ] }, { "cell_type": "markdown", "id": "f736080c-19a2-468f-a924-bc01fad56ed4", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Comprehensions" ] }, { "cell_type": "markdown", "id": "977cdb29-f7f5-4193-a48b-0626587336c2", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `list` comprehension" ] }, { "cell_type": "code", "execution_count": 112, "id": "ec081ccd-b87c-4174-90c0-3a1055049414", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[x**2 for x in range(10)]" ] }, { "cell_type": "markdown", "id": "e5ce2161-db6c-4926-a653-f8855795ce66", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `list` comprehension with an `if`-clause" ] }, { "cell_type": "code", "execution_count": 113, "id": "d6afd2ae-d0e0-4697-ab32-f12ae5816eb1", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[0, 4, 16, 36, 64]" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[x**2 for x in range(10) if x % 2 == 0]" ] }, { "cell_type": "markdown", "id": "b89d411a-2ad0-45a6-bccb-1d4bd6e5cbcd", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `list` comprehension with two `for`-clauses" ] }, { "cell_type": "code", "execution_count": 114, "id": "132ff9cb-bb8a-418e-92b6-b6990ff45eda", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nested_list = [[0, 1, 2], [], [3, 4], [5], [6, 7, 8, 9]]\n", "\n", "[inner for outer in nested_list for inner in outer]" ] }, { "cell_type": "markdown", "id": "2fb71346-90e2-4cd4-99ca-22fcfeb7bb96", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `list` comprehension on `dict` `items`" ] }, { "cell_type": "code", "execution_count": 115, "id": "57adec5a-5e7e-43b0-aed6-304dd08e3044", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[13, 23, 35, 44, 54]" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_dict = {\"one\": 10, \"two\": 20, \"three\": 30, \"four\": 40, \"five\": 50}\n", "\n", "[len(key) + value for key, value in some_dict.items()]" ] }, { "cell_type": "markdown", "id": "3228c88f-8326-448f-beb8-239b80b49969", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `list` comprehension with `zip`" ] }, { "cell_type": "code", "execution_count": 116, "id": "f531e299-21d8-4656-a79a-9380543cf2af", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[13, 22, 31]" ] }, "execution_count": 116, "metadata": {}, "output_type": "execute_result" } ], "source": [ "first_list = [10, 20, 30, 40, 50]\n", "second_list = [3, 2, 1]\n", "\n", "[x + y for x, y in zip(first_list, second_list)]" ] }, { "cell_type": "markdown", "id": "2d21a622-9bde-489f-a3be-8c1d8d7f8984", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `list` comprehension with `enumerate`" ] }, { "cell_type": "code", "execution_count": 117, "id": "82b6c47b-fba6-4c50-85ee-196713cfde85", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[4, 103, 203, 305, 404, 504]" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_list = [\"zero\", \"one\", \"two\", \"three\", \"four\", \"five\"]\n", "\n", "[100*i + len(word) for i, word in enumerate(some_list)]" ] }, { "cell_type": "markdown", "id": "2003534d-30a2-47f4-864d-629705b24cdb", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `dict` comprehension" ] }, { "cell_type": "code", "execution_count": 118, "id": "1a1d54e9-9eaf-43a4-a404-60af1f457609", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "{0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'}" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{i: word for i, word in enumerate(some_list)}" ] }, { "cell_type": "markdown", "id": "6cacae58-20e0-4957-8e39-431a6c2abf5e", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `set` comprehension" ] }, { "cell_type": "code", "execution_count": 119, "id": "9f776181-8936-4b56-9b4e-8db4ae516bb8", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "{1, 4, 9, 16, 25}" ] }, "execution_count": 119, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_list = [1, 1, 1, 2, 3, 3, 4, 4, 5]\n", "\n", "{x**2 for x in some_list}" ] }, { "cell_type": "markdown", "id": "b515a1a6-1963-4c12-ba43-8ed72636db3a", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `sum` of an iterator comprehension" ] }, { "cell_type": "code", "execution_count": 120, "id": "7da29236-ded8-4960-9380-d2b5dd072ca4", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 120, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(x for x in range(5))" ] }, { "cell_type": "markdown", "id": "2b1c3d19-480f-4e6c-add1-56e645539a4e", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Iterators" ] }, { "cell_type": "markdown", "id": "26c0bf3f-994b-4a3b-866f-9d7036b29f37", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Iterator comprehension, try to extract item" ] }, { "cell_type": "code", "execution_count": 121, "id": "2eeb649f-cf08-4b2f-b849-0fe730952f2b", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "TypeError", "evalue": "'generator' object is not subscriptable", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[121], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m iterator \u001b[38;5;241m=\u001b[39m (x\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m2\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m x \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m10\u001b[39m))\n\u001b[0;32m----> 3\u001b[0m \u001b[43miterator\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m]\u001b[49m\n", "\u001b[0;31mTypeError\u001b[0m: 'generator' object is not subscriptable" ] } ], "source": [ "iterator = (x**2 for x in range(10))\n", "\n", "iterator[3]" ] }, { "cell_type": "markdown", "id": "0ed54460-ae43-454a-9ceb-920bd5f46b0c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Iterator comprehension, get `next`" ] }, { "cell_type": "code", "execution_count": 122, "id": "1bb94064-5494-4436-839c-e8860c14aa50", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n" ] } ], "source": [ "print(next(iterator))\n", "print(next(iterator))\n", "print(next(iterator))" ] }, { "cell_type": "markdown", "id": "4c3f50c3-d437-4a13-b6f7-0a6d6520f72d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Iterator comprehension, use in `for` loop" ] }, { "cell_type": "code", "execution_count": 123, "id": "c757fcea-cde8-40ac-b5ea-6e39be604c32", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n", "16\n", "25\n", "36\n", "49\n", "64\n", "81\n" ] } ], "source": [ "for x in iterator:\n", " print(x)" ] }, { "cell_type": "markdown", "id": "ee3e2dde-8d83-430e-88a0-f8ef777c7061", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Create iterator with `yield`" ] }, { "cell_type": "code", "execution_count": 124, "id": "415d9971-f7f2-4b87-ada8-b3ffcbb7f581", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n" ] } ], "source": [ "def generator(n):\n", " for x in range(n):\n", " yield x**2\n", "\n", "iterator = generator(10)\n", "\n", "print(next(iterator))\n", "print(next(iterator))\n", "print(next(iterator))" ] }, { "cell_type": "markdown", "id": "145d3305-bf37-4eb2-8a50-9924b4e83203", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Restarting iteration" ] }, { "cell_type": "code", "execution_count": 125, "id": "87fa4568-9307-4a03-b124-0d79b21bfa3c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[9, 16, 25, 36, 49, 64, 81]\n", "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n" ] } ], "source": [ "print(list(iterator))\n", "\n", "iterator = generator(10)\n", "\n", "print(list(iterator))" ] }, { "cell_type": "markdown", "id": "20f11117-d083-4500-b198-33afe344c084", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Example of `yield from`" ] }, { "cell_type": "code", "execution_count": 126, "id": "26311242-4123-426c-ba07-fc3de422ce66", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4, 9, 16, 0, 1, 4, 9, 16, 0, 1, 4, 9, 16]" ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def another_generator(n):\n", " for x in range(3):\n", " yield from generator(n)\n", "\n", "list(another_generator(5))" ] }, { "cell_type": "markdown", "id": "6c9dac71-7c0a-4a52-8ce1-e15f369c0f21", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Exceptions" ] }, { "cell_type": "markdown", "id": "2f3c8b82-daf5-45af-97a7-f83f9b888cc1", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `raise` exception" ] }, { "cell_type": "code", "execution_count": 127, "id": "299365f9-6ea8-4f97-9da8-9c5bfe917385", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "Exception", "evalue": "intentionally", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mException\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[127], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mintentionally\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mException\u001b[0m: intentionally" ] } ], "source": [ "raise Exception(\"intentionally\")" ] }, { "cell_type": "markdown", "id": "39724f30-bb8d-4ec3-a0b9-825d64aca395", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `assert` statement" ] }, { "cell_type": "code", "execution_count": 128, "id": "b0bbb75e-3cbb-45cf-a4f0-7b0aa34712ab", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "AssertionError", "evalue": "oops, 3 != 4", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[128], line 4\u001b[0m\n\u001b[1;32m 1\u001b[0m expected_value \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m3\u001b[39m\n\u001b[1;32m 2\u001b[0m actual_value \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m4\u001b[39m\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m expected_value \u001b[38;5;241m==\u001b[39m actual_value, \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124moops, \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mexpected_value\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m != \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mactual_value\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n", "\u001b[0;31mAssertionError\u001b[0m: oops, 3 != 4" ] } ], "source": [ "expected_value = 3\n", "actual_value = 4\n", "\n", "assert expected_value == actual_value, f\"oops, {expected_value} != {actual_value}\"" ] }, { "cell_type": "markdown", "id": "3b22ebc3-b82a-4451-a71f-85155b262c0a", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `try`-`except`" ] }, { "cell_type": "code", "execution_count": 129, "id": "129f1ba5-5b0a-4287-ae54-63c165872664", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ignoring <class 'TypeError'>(expected integer, not string)\n" ] } ], "source": [ "def expects_integer(x):\n", " if not isinstance(x, int):\n", " raise TypeError(f\"expected integer, not {x}\")\n", " return x**2\n", "\n", "try:\n", " expects_integer(\"string\")\n", "except TypeError as err:\n", " print(f\"ignoring {type(err)}({err})\")" ] }, { "cell_type": "markdown", "id": "cb298623-c7d0-4716-a771-c0980c99b539", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `try`-`except`-`else`" ] }, { "cell_type": "code", "execution_count": 130, "id": "46943112-f3e9-4360-969f-f667845730ac", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "25\n" ] } ], "source": [ "try:\n", " output = expects_integer(5)\n", "except TypeError as err:\n", " print(f\"ignoring {type(err)}({err})\")\n", "else:\n", " print(output)" ] }, { "cell_type": "markdown", "id": "482607ed-eda6-429a-871e-d92ca36e1175", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `try`-`finally`" ] }, { "cell_type": "code", "execution_count": 131, "id": "29d3a4b7-3d55-4341-82a9-a4a2e1f689f7", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "we get here anyway\n" ] }, { "ename": "FileNotFoundError", "evalue": "[Errno 2] No such file or directory: 'file/that/does/not/exist'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[131], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfile/that/does/not/exist\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mwe get here anyway\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "File \u001b[0;32m~/mambaforge/lib/python3.10/site-packages/IPython/core/interactiveshell.py:324\u001b[0m, in \u001b[0;36m_modified_open\u001b[0;34m(file, *args, **kwargs)\u001b[0m\n\u001b[1;32m 317\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m}:\n\u001b[1;32m 318\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 319\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIPython won\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m by default \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 320\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 321\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myou can use builtins\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m open.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 322\u001b[0m )\n\u001b[0;32m--> 324\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mio_open\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'file/that/does/not/exist'" ] } ], "source": [ "try:\n", " open(\"file/that/does/not/exist\")\n", "finally:\n", " print(\"we get here anyway\")" ] }, { "cell_type": "markdown", "id": "0f67fe60-6b65-470a-9724-f39173b27c35", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Class definitions" ] }, { "cell_type": "markdown", "id": "82520684-7eab-437c-a4ca-9061e56d46fd", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Class as a bag" ] }, { "cell_type": "code", "execution_count": 132, "id": "b9d5e1d4-d891-4c3f-9837-2b3e6f4fd911", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Bag:\n", " pass\n", "\n", "bag = Bag()\n", "\n", "bag.x = 5\n", "\n", "bag.x" ] }, { "cell_type": "markdown", "id": "08eb0906-7d31-46f6-b66f-561424bebdba", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Class with method" ] }, { "cell_type": "code", "execution_count": 133, "id": "09460148-49c7-4f0e-adbe-08905bde5796", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Point:\n", " def magnitude(self):\n", " return math.sqrt(self.x**2 + self.y**2)\n", "\n", "point = Point()\n", "\n", "point.x = 3\n", "point.y = 4\n", "\n", "point.magnitude()" ] }, { "cell_type": "markdown", "id": "6ef9e2c7-45b7-4ba7-955b-e7f2ebe57270", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Private method (one `_`)" ] }, { "cell_type": "code", "execution_count": 134, "id": "fb01e4eb-13ec-4dae-9b9e-c643313d2d1d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Point:\n", " def _hidden(self):\n", " return math.sqrt(self.x**2 + self.y**2)\n", "point = Point()\n", "\n", "point.x = 3\n", "point.y = 4\n", "\n", "point._hidden()" ] }, { "cell_type": "markdown", "id": "0381bc74-2721-46f5-bfa1-2e828fc10542", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Really private method (two `_`)" ] }, { "cell_type": "code", "execution_count": 135, "id": "b0bce50e-d475-4853-93f5-6fc9c5044727", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "AttributeError", "evalue": "'Point' object has no attribute '__hidden'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[135], line 9\u001b[0m\n\u001b[1;32m 6\u001b[0m point\u001b[38;5;241m.\u001b[39mx \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m3\u001b[39m\n\u001b[1;32m 7\u001b[0m point\u001b[38;5;241m.\u001b[39my \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m4\u001b[39m\n\u001b[0;32m----> 9\u001b[0m \u001b[43mpoint\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__hidden\u001b[49m()\n", "\u001b[0;31mAttributeError\u001b[0m: 'Point' object has no attribute '__hidden'" ] } ], "source": [ "class Point:\n", " def __hidden(self):\n", " return math.sqrt(self.x**2 + self.y**2)\n", "point = Point()\n", "\n", "point.x = 3\n", "point.y = 4\n", "\n", "point.__hidden()" ] }, { "cell_type": "markdown", "id": "712bec34-2b5e-4bc3-941e-fe76b143baad", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Class method with an oddly named `self`" ] }, { "cell_type": "code", "execution_count": 136, "id": "72ab9fc5-33d8-4774-86aa-b9b6813651d5", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Point:\n", " def magnitude(can_have_any_name):\n", " return math.sqrt(can_have_any_name.x**2 + can_have_any_name.y**2)\n", "\n", "point = Point()\n", "\n", "point.x = 3\n", "point.y = 4\n", "\n", "point.magnitude()" ] }, { "cell_type": "markdown", "id": "bb20a9e8-8f6f-48fc-b1d7-4c0b164b0ece", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Class with `__init__`" ] }, { "cell_type": "code", "execution_count": 137, "id": "f96c5b00-15ba-489b-a363-ec7da6b12cbf", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Point:\n", " def __init__(self, x, y):\n", " self.x = x\n", " self.y = y\n", "\n", " def magnitude(self):\n", " return math.sqrt(self.x**2 + self.y**2)\n", "\n", "point = Point(3, 4)\n", "\n", "point.magnitude()" ] }, { "cell_type": "markdown", "id": "32586b14-df99-4a6d-9e02-f6165b7e3b27", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Class with `__repr__`" ] }, { "cell_type": "code", "execution_count": 138, "id": "4a4c23c5-eac8-4ad6-8aeb-00a0658733ca", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Point(3, 4)" ] }, "execution_count": 138, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Point:\n", " def __init__(self, x, y):\n", " self.x = x\n", " self.y = y\n", "\n", " def __repr__(self):\n", " return f\"Point({self.x}, {self.y})\"\n", "\n", "Point(3, 4)" ] }, { "cell_type": "markdown", "id": "6616e035-7704-4df8-8c7c-53ea9bee8fea", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Class with `__getattr__`" ] }, { "cell_type": "code", "execution_count": 139, "id": "71ec466a-783b-4315-a50e-6b452a2eb7e8", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(3, 3, 5, 4, 4)" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class AttributeLength:\n", " def __getattr__(self, name):\n", " return len(name)\n", "\n", "obj = AttributeLength()\n", "\n", "obj.one, obj.two, obj.three, obj.four, obj.five" ] }, { "cell_type": "markdown", "id": "20783b9a-55bb-4963-93a7-777e8d901436", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Class with `__getitem__`" ] }, { "cell_type": "code", "execution_count": 140, "id": "42f8217d-6c29-4f51-a48f-a47757b48c5e", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(1, 4, 9, 16, 25)" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Squaring:\n", " def __getitem__(self, item):\n", " return item**2\n", "\n", "obj = Squaring()\n", "\n", "obj[1], obj[2], obj[3], obj[4], obj[5]" ] }, { "cell_type": "markdown", "id": "47e48041-5953-492b-acea-3184dadeb416", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Class with `__call__`" ] }, { "cell_type": "code", "execution_count": 141, "id": "b1a58a7a-a8d4-4855-98de-73c78921ab41", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Incrementor:\n", " def __init__(self, by):\n", " self.by = by\n", "\n", " def __call__(self, x):\n", " return x + self.by\n", "\n", "incrementor = Incrementor(1)\n", "\n", "incrementor(10)" ] }, { "cell_type": "markdown", "id": "69de0f95-067f-4552-8f04-d0cb6eb6ef44", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Class with `__eq__`" ] }, { "cell_type": "code", "execution_count": 142, "id": "07314889-9085-4cf6-a002-df700fe97951", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 142, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Point:\n", " def __init__(self, x, y):\n", " self.x = x\n", " self.y = y\n", "\n", " def __eq__(self, other):\n", " return type(self) == type(other) and self.x == other.x and self.y == other.y\n", "\n", "one = Point(3, 4)\n", "two = Point(3, 4)\n", "\n", "one == two" ] }, { "cell_type": "markdown", "id": "b02c0e8c-1f62-4802-b6f1-eb7c028b8393", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Equality versus `is`" ] }, { "cell_type": "code", "execution_count": 143, "id": "6f991db7-8c42-4bd2-a5bd-c262dc836f4d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "one is two" ] }, { "cell_type": "markdown", "id": "74e33cda-cd6b-4807-92f0-44d31a4fab55", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `id`" ] }, { "cell_type": "code", "execution_count": 144, "id": "b593fa7f-93bf-457b-8732-c69eee6847f8", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(133038784424896, 133038784422256)" ] }, "execution_count": 144, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(one), id(two)" ] }, { "cell_type": "markdown", "id": "cef32675-0bcb-479b-bc38-f702e5d358d7", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Unhashable object" ] }, { "cell_type": "code", "execution_count": 145, "id": "bdcb1d39-4952-4557-b408-e1cc5ddb7020", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "TypeError", "evalue": "unhashable type: 'list'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[145], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m list_is_not_hashable \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m3\u001b[39m]\n\u001b[0;32m----> 3\u001b[0m {list_is_not_hashable: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mone\u001b[39m\u001b[38;5;124m\"\u001b[39m}\n", "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'" ] } ], "source": [ "list_is_not_hashable = [1, 2, 3]\n", "\n", "{list_is_not_hashable: \"one\"}" ] }, { "cell_type": "markdown", "id": "722b390e-9cfe-450d-8ede-4e3b9f7b7363", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Hashable object" ] }, { "cell_type": "code", "execution_count": 146, "id": "35e7bb2f-2636-4c29-8bf5-62f7e3011f71", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "{Point(3, 4): 'two', Point(4, 5): 'three'}" ] }, "execution_count": 146, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Point:\n", " def __init__(self, x, y):\n", " self.x = x\n", " self.y = y\n", "\n", " def __repr__(self):\n", " return f\"Point({self.x}, {self.y})\"\n", "\n", " def __eq__(self, other):\n", " return type(self) == type(other) and self.x == other.x and self.y == other.y\n", "\n", " def __hash__(self):\n", " return hash((type(self), self.x, self.y))\n", "\n", "{Point(3, 4): \"one\", Point(3, 4): \"two\", Point(4, 5): \"three\"}" ] }, { "cell_type": "markdown", "id": "d214f42c-aa18-4cd0-add6-dda1f174947f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Class with `@property`" ] }, { "cell_type": "code", "execution_count": 147, "id": "0406da1e-a43d-4044-ac48-341bde581999", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "AttributeError", "evalue": "can't set attribute 'x'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[147], line 16\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_hidden_y\n\u001b[1;32m 14\u001b[0m point \u001b[38;5;241m=\u001b[39m Point(\u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m4\u001b[39m)\n\u001b[0;32m---> 16\u001b[0m \u001b[43mpoint\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mx\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m33333\u001b[39m\n", "\u001b[0;31mAttributeError\u001b[0m: can't set attribute 'x'" ] } ], "source": [ "class Point:\n", " def __init__(self, x, y):\n", " self._hidden_x = x\n", " self._hidden_y = y\n", "\n", " @property\n", " def x(self):\n", " return self._hidden_x\n", "\n", " @property\n", " def y(self):\n", " return self._hidden_y\n", "\n", "point = Point(3, 4)\n", "\n", "point.x = 33333" ] }, { "cell_type": "markdown", "id": "9d1616f2-8351-4e69-983c-67619c292dbf", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Class with mutable `@property`" ] }, { "cell_type": "code", "execution_count": 148, "id": "81fc76ce-4e11-4423-86db-18166b3838c2", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "33333" ] }, "execution_count": 148, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Point:\n", " def __init__(self, x, y):\n", " self.x = x\n", " self.y = y\n", "\n", " @property\n", " def x(self):\n", " return self._hidden_x\n", "\n", " @x.setter\n", " def x(self, value):\n", " assert isinstance(value, int)\n", " self._hidden_x = value\n", "\n", " @property\n", " def y(self):\n", " return self._hidden_y\n", "\n", " @y.setter\n", " def y(self, value):\n", " assert isinstance(value, int)\n", " self._hidden_y = value\n", "\n", "point = Point(3, 4)\n", "\n", "point.x = 33333\n", "\n", "point.x" ] }, { "cell_type": "markdown", "id": "60ab4ce5-2822-4174-a48c-cf5a5f915707", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Class with `@classmethod`" ] }, { "cell_type": "code", "execution_count": 149, "id": "7118c67c-dcfd-498d-a4ef-b995dbf53937", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Point(1, 0)" ] }, "execution_count": 149, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Point:\n", " def __init__(self, x, y):\n", " self.x = x\n", " self.y = y\n", "\n", " @classmethod\n", " def unit(cls, axis):\n", " if axis == \"x\":\n", " return Point(1, 0)\n", " else:\n", " return Point(0, 1)\n", "\n", " def __repr__(self):\n", " return f\"Point({self.x}, {self.y})\"\n", "\n", "Point.unit(\"x\")" ] }, { "cell_type": "markdown", "id": "0d17040c-42ad-4f39-93e4-4a4404b0b8a6", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Class with `@staticmethod`" ] }, { "cell_type": "code", "execution_count": 150, "id": "fce7b043-8cf0-4907-89bc-a93c325a19cb", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'whatever'" ] }, "execution_count": 150, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Point:\n", " def __init__(self, x, y):\n", " self.x = x\n", " self.y = y\n", "\n", " @staticmethod\n", " def something_else():\n", " return \"whatever\"\n", "\n", "Point.something_else()" ] }, { "cell_type": "markdown", "id": "8dc43e3b-6fef-43c8-9c9b-e33545e559b0", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Class object attributes" ] }, { "cell_type": "code", "execution_count": 151, "id": "b4d9a828-37dd-48d9-a198-ec849a4c1637", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'whatever'" ] }, "execution_count": 151, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Point:\n", " def __init__(self, x, y):\n", " self.x = x\n", " self.y = y\n", "\n", " something_else = \"whatever\"\n", "\n", "Point.something_else" ] }, { "cell_type": "markdown", "id": "fe024885-9221-471c-b38e-30a78b1707f2", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `@dataclass`" ] }, { "cell_type": "code", "execution_count": 152, "id": "5d34c01b-d4fe-4f7a-9523-d926970fc7d1", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Point(x=3, y=4)" ] }, "execution_count": 152, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from dataclasses import dataclass\n", "\n", "@dataclass\n", "class Point:\n", " x: int\n", " y: int\n", "\n", "Point(3, 4)" ] }, { "cell_type": "markdown", "id": "89b3fe54-90d2-4300-ada1-5a3a3699b654", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Frozen `@dataclass`" ] }, { "cell_type": "code", "execution_count": 153, "id": "c4d4a28f-9f71-4edb-afc0-e316ebe9b8dd", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "FrozenInstanceError", "evalue": "cannot assign to field 'x'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mFrozenInstanceError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[153], line 10\u001b[0m\n\u001b[1;32m 6\u001b[0m y: \u001b[38;5;28mint\u001b[39m\n\u001b[1;32m 8\u001b[0m point \u001b[38;5;241m=\u001b[39m Point(\u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m4\u001b[39m)\n\u001b[0;32m---> 10\u001b[0m \u001b[43mpoint\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mx\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m33333\u001b[39m\n", "File \u001b[0;32m<string>:4\u001b[0m, in \u001b[0;36m__setattr__\u001b[0;34m(self, name, value)\u001b[0m\n", "\u001b[0;31mFrozenInstanceError\u001b[0m: cannot assign to field 'x'" ] } ], "source": [ "from dataclasses import dataclass\n", "\n", "@dataclass(frozen=True)\n", "class Point:\n", " x: int\n", " y: int\n", "\n", "point = Point(3, 4)\n", "\n", "point.x = 33333" ] }, { "cell_type": "markdown", "id": "4c5ee2bc-4c7a-4f28-b3d6-c9f41d545dcd", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Simple metaclass" ] }, { "cell_type": "code", "execution_count": 154, "id": "bf208b01-696c-4aca-a9dd-481e560136b7", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "defining class Something\n" ] } ], "source": [ "class mytype(type):\n", " def __new__(cls, name, base_classes, attributes):\n", " print(f\"defining class {name}\")\n", " return type.__new__(cls, name, base_classes, attributes)\n", "\n", "class Something(metaclass=mytype):\n", " pass" ] }, { "cell_type": "markdown", "id": "a6cec2e1-8100-4bdc-b21d-6191931afa45", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Inspecting classes" ] }, { "cell_type": "markdown", "id": "64501cb1-17bd-495d-b3d4-f8a128a5b303", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `dir`" ] }, { "cell_type": "code", "execution_count": 155, "id": "3274f9aa-17f5-4c8d-9714-6497335a631b", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['something_else', 'x', 'y']" ] }, "execution_count": 155, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Point:\n", " def __init__(self, x, y):\n", " self.x = x\n", " self.y = y\n", "\n", " something_else = \"whatever\"\n", "\n", "point = Point(3, 4)\n", "\n", "[x for x in dir(point) if not x.startswith(\"_\")]" ] }, { "cell_type": "markdown", "id": "38b3b7d4-3f96-4473-bd19-0a5544568555", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `hasattr`" ] }, { "cell_type": "code", "execution_count": 156, "id": "1f58f5a5-c3cf-47e5-b17b-0ce397e6cd8d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 156, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hasattr(point, \"z\")" ] }, { "cell_type": "markdown", "id": "88d8e1af-8b32-45a6-bc2c-4e2a3f82f862", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `getattr`" ] }, { "cell_type": "code", "execution_count": 157, "id": "afc723c6-17ab-4eb9-b69f-b6121ea4e6c4", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "AttributeError", "evalue": "'Point' object has no attribute 'z'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[157], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mgetattr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mpoint\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mz\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", "\u001b[0;31mAttributeError\u001b[0m: 'Point' object has no attribute 'z'" ] } ], "source": [ "getattr(point, \"z\")" ] }, { "cell_type": "markdown", "id": "0f4a7466-1bb0-439b-b467-b3127060b49d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `getattr` with default" ] }, { "cell_type": "code", "execution_count": 158, "id": "d0ff2624-07ad-4c2b-be51-a329f13e8bc7", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'default'" ] }, "execution_count": 158, "metadata": {}, "output_type": "execute_result" } ], "source": [ "getattr(point, \"z\", \"default\")" ] }, { "cell_type": "markdown", "id": "059282c8-aae7-49c3-bf57-aeccacee2bf9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `__dict__`" ] }, { "cell_type": "code", "execution_count": 159, "id": "1a25d369-2048-4806-bfdf-eba621dc8f8d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "{'x': 3, 'y': 4}" ] }, "execution_count": 159, "metadata": {}, "output_type": "execute_result" } ], "source": [ "point.__dict__" ] }, { "cell_type": "markdown", "id": "7b18efef-e5b5-48ed-9cca-c7a23de439e2", "metadata": {}, "source": [ "### Inheritance" ] }, { "cell_type": "markdown", "id": "68b98be8-59cf-434b-8c13-d28b22ab602f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Defining a subclass, inheriting method" ] }, { "cell_type": "code", "execution_count": 160, "id": "1e125734-1ddc-4863-bd44-a2dbc5fd1bcc", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(625, 5.0)" ] }, "execution_count": 160, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Superclass:\n", " def __init__(self, x):\n", " self.x = x\n", "\n", " def square(self):\n", " return self.x**2\n", "\n", "class Subclass(Superclass):\n", " def square_root(self):\n", " return math.sqrt(self.x)\n", "\n", "obj = Subclass(25)\n", "\n", "obj.square(), obj.square_root()" ] }, { "cell_type": "markdown", "id": "e440cf91-59c5-4291-961e-3c5f1bf1e7b3", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Defining a subclass, overloading method" ] }, { "cell_type": "code", "execution_count": 161, "id": "f89fced6-895b-40b0-ad05-9ca32850662f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'I am a square!'" ] }, "execution_count": 161, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Superclass:\n", " def __init__(self, x):\n", " self.x = x\n", "\n", " def square(self):\n", " return self.x**2\n", "\n", "class Subclass(Superclass):\n", " def square(self):\n", " return \"I am a square!\"\n", "\n", "obj = Subclass(25)\n", "\n", "obj.square()" ] }, { "cell_type": "markdown", "id": "aada6068-84de-4f87-ae75-dd485fff750c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `isinstance` on custom classes" ] }, { "cell_type": "code", "execution_count": 162, "id": "d1d5bd96-038c-498d-85ce-7e55b2b64a8c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 162, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isinstance(obj, Superclass)" ] }, { "cell_type": "markdown", "id": "7095182a-6161-4035-ae5b-c5f3d285742d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `issubclass` on custom classes" ] }, { "cell_type": "code", "execution_count": 163, "id": "5e7a40d7-4a6a-4f78-af1a-279b283b7bd6", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 163, "metadata": {}, "output_type": "execute_result" } ], "source": [ "issubclass(Subclass, Superclass)" ] }, { "cell_type": "markdown", "id": "1582e5e6-78d8-4ea3-acba-7e4c5a161546", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Multiple inheritance" ] }, { "cell_type": "code", "execution_count": 164, "id": "76dbe992-18d5-4b07-8427-8d5c219798a6", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(625, 5.0)" ] }, "execution_count": 164, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class HasSquare:\n", " def square(self):\n", " return self.x**2\n", "\n", "class HasSquareRoot:\n", " def square_root(self):\n", " return math.sqrt(self.x)\n", "\n", "class ActualClass(HasSquare, HasSquareRoot):\n", " def __init__(self, x):\n", " self.x = x\n", "\n", "obj = ActualClass(25)\n", "\n", "obj.square(), obj.square_root()" ] }, { "cell_type": "markdown", "id": "354d3045-586d-4d7d-a1ec-cc8a659e465b", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Decorators" ] }, { "cell_type": "markdown", "id": "1e0147c5-12c0-4cd1-97d6-c78c49c414e7", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Defining a decorator for a function" ] }, { "cell_type": "code", "execution_count": 165, "id": "f9c32e78-2756-4b20-b4c3-6034c1498743", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 165, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def squareify(function):\n", " return lambda x: function(x)**2\n", "\n", "@squareify\n", "def f(x):\n", " return x + 1\n", "\n", "f(2)" ] }, { "cell_type": "markdown", "id": "d58deeda-ceed-4288-8610-ccd25a331035", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Defining a decorator for a class" ] }, { "cell_type": "code", "execution_count": 166, "id": "a1591d16-c99c-4d25-b515-58e0df28bba9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "625" ] }, "execution_count": 166, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def mixin_square(cls):\n", " class Output(cls):\n", " def square(self):\n", " return self.x**2\n", "\n", " Output.__name__ = cls.__name__\n", " return Output\n", "\n", "@mixin_square\n", "class ActualClass:\n", " def __init__(self, x):\n", " self.x = x\n", "\n", "obj = ActualClass(25)\n", "\n", "obj.square()" ] }, { "cell_type": "markdown", "id": "c5166e8a-7235-444e-8389-00765250736f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Structural matching" ] }, { "cell_type": "markdown", "id": "3fa90c46-c15f-4931-a1df-81facc7e18c0", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Matching exactly" ] }, { "cell_type": "code", "execution_count": 167, "id": "4e8c9f69-6b90-405c-89e1-e4cd99d3eae3", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "two\n" ] } ], "source": [ "item = 2\n", "\n", "match item:\n", " case 1:\n", " print(\"one\")\n", " case 2:\n", " print(\"two\")" ] }, { "cell_type": "markdown", "id": "a7f074e6-540c-46a6-be7a-e48860bde6f9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Matching exactly with a default" ] }, { "cell_type": "code", "execution_count": 168, "id": "3f0a22fb-d5bc-4dc3-b1e8-db3d1e8e5dc0", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I don't recognize 3\n" ] } ], "source": [ "item = 3\n", "\n", "match item:\n", " case 1:\n", " print(\"one\")\n", " case 2:\n", " print(\"two\")\n", " case _:\n", " print(f\"I don't recognize {item}\")" ] }, { "cell_type": "markdown", "id": "bf58859e-28ea-40c4-aa0b-c96f2842cc38", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Matching with `tuple` unpacking" ] }, { "cell_type": "code", "execution_count": 169, "id": "c149e466-f099-4619-83b3-7ffea1da7b9c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "two: 1 2\n" ] } ], "source": [ "item = 1, 2\n", "\n", "match item:\n", " case (x, y, z):\n", " print(f\"three: {x} {y} {z}\")\n", " case (x, y):\n", " print(f\"two: {x} {y}\")\n", " case (x,):\n", " print(f\"one: {x}\")" ] }, { "cell_type": "markdown", "id": "a333c873-de26-4f03-84b6-93e663b01f73", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Matching with `dict` unpacking" ] }, { "cell_type": "code", "execution_count": 170, "id": "fe6ce3c1-df27-4f44-b35c-5d6d683ab9ad", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "two: 1 2\n" ] } ], "source": [ "item = {\"one\": 1, \"two\": 2}\n", "\n", "match item:\n", " case {\"one\": x, \"two\": y, \"three\": z}:\n", " print(f\"three: {x} {y} {z}\")\n", " case {\"one\": x, \"two\": y}:\n", " print(f\"two: {x} {y}\")\n", " case {\"one\": x}:\n", " print(f\"one: {x}\")" ] }, { "cell_type": "markdown", "id": "3691821b-d113-4420-9890-c7b137b6b92b", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Matching class with `__match_args__`" ] }, { "cell_type": "code", "execution_count": 171, "id": "86b20ff9-425f-4032-868a-4ca02b2942b8", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Point with x: 3 y: 4\n" ] } ], "source": [ "class Point:\n", " def __init__(self, x, y):\n", " self.x = x\n", " self.y = y\n", "\n", " __match_args__ = (\"x\", \"y\")\n", "\n", "item = Point(3, 4)\n", "\n", "match item:\n", " case Point(x, y):\n", " print(f\"Point with x: {x} y: {y}\")" ] }, { "cell_type": "markdown", "id": "4531238d-dfc1-45c9-b45b-8aa6731eff73", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Matching `@dataclass`" ] }, { "cell_type": "code", "execution_count": 172, "id": "f05949b7-a05b-4875-835c-ebffcd83e43d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Point with x: 3 y: 4\n" ] } ], "source": [ "@dataclass\n", "class Point:\n", " x: int\n", " y: int\n", "\n", "item = Point(3, 4)\n", "\n", "match item:\n", " case Point(x, y):\n", " print(f\"Point with x: {x} y: {y}\")" ] }, { "cell_type": "markdown", "id": "e9088cb6-90a9-4a3b-bf4a-f5225853feb5", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Matching with guards" ] }, { "cell_type": "code", "execution_count": 173, "id": "4a969ae5-e2a3-470e-8fbb-554e3b4272db", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Point with x: 3 y: 4\n" ] } ], "source": [ "match item:\n", " case Point(x, y) if x == 0 and y == 0:\n", " print(f\"zero Point\")\n", "\n", " case Point(x, y) if x == 1 and y == 0:\n", " print(f\"unit Point in x\")\n", "\n", " case Point(x, y) if x == 0 and y == 1:\n", " print(f\"unit Point in y\")\n", "\n", " case Point(x, y):\n", " print(f\"Point with x: {x} y: {y}\")" ] }, { "cell_type": "markdown", "id": "6bf5fa4b-f68e-48d6-8377-04bfa7b3e0d9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Matching multiple patterns" ] }, { "cell_type": "code", "execution_count": 174, "id": "1ba13870-1e8b-4e2f-86eb-e25d2fb01100", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.14 is either a float or an int\n" ] } ], "source": [ "item = 3.14\n", "\n", "match item:\n", " case float(x) | int(x):\n", " print(f\"{x} is either a float or an int\")" ] }, { "cell_type": "markdown", "id": "dfe0e069-1ab3-4c04-b253-9ac1b0d2c7cc", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Executing quoted code" ] }, { "cell_type": "markdown", "id": "89296f13-f73a-43bf-b9b5-e78e09b3bd06", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `exec`" ] }, { "cell_type": "code", "execution_count": 175, "id": "9428cce3-7bcb-427d-908a-e727001e8fa9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 175, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scope = {\"x\": 5}\n", "\n", "code = \"y = x**2\"\n", "\n", "exec(code, scope)\n", "\n", "scope[\"y\"]" ] }, { "cell_type": "markdown", "id": "9513b2ab-a2be-450b-83ac-c5a68dc2df11", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `eval`" ] }, { "cell_type": "code", "execution_count": 176, "id": "558d4a8a-07b9-4eec-9f91-8aa1d23e85cb", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 176, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scope = {\"x\": 5}\n", "\n", "code = \"x**2\"\n", "\n", "eval(code, scope)" ] }, { "cell_type": "markdown", "id": "9a3d5073-769f-46e6-ad1c-f0b863a92066", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `ast.literal_eval`" ] }, { "cell_type": "code", "execution_count": 177, "id": "e4a12d0e-a192-48c4-9100-23a792c654c4", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[1.1, 2.2, 3.3, 4.4, 5.5]" ] }, "execution_count": 177, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import ast\n", "\n", "code = \"[1.1, 2.2, 3.3, 4.4, 5.5]\"\n", "\n", "ast.literal_eval(code)" ] }, { "cell_type": "markdown", "id": "671bec9c-18f3-4647-a78e-655f3e7ddd40", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `ast.literal_eval` prevents execution" ] }, { "cell_type": "code", "execution_count": 178, "id": "df80c47a-83f1-40b8-b7b7-c997fee989ce", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "ValueError", "evalue": "malformed node or string on line 1: <ast.BinOp object at 0x78ff80d4d030>", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[178], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m code \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m[2 + 2]\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m----> 3\u001b[0m \u001b[43mast\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mliteral_eval\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcode\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/mambaforge/lib/python3.10/ast.py:110\u001b[0m, in \u001b[0;36mliteral_eval\u001b[0;34m(node_or_string)\u001b[0m\n\u001b[1;32m 108\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m left \u001b[38;5;241m-\u001b[39m right\n\u001b[1;32m 109\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _convert_signed_num(node)\n\u001b[0;32m--> 110\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_convert\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnode_or_string\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/mambaforge/lib/python3.10/ast.py:90\u001b[0m, in \u001b[0;36mliteral_eval.<locals>._convert\u001b[0;34m(node)\u001b[0m\n\u001b[1;32m 88\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mtuple\u001b[39m(\u001b[38;5;28mmap\u001b[39m(_convert, node\u001b[38;5;241m.\u001b[39melts))\n\u001b[1;32m 89\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(node, List):\n\u001b[0;32m---> 90\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mlist\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mmap\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m_convert\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnode\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43melts\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 91\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(node, Set):\n\u001b[1;32m 92\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mset\u001b[39m(\u001b[38;5;28mmap\u001b[39m(_convert, node\u001b[38;5;241m.\u001b[39melts))\n", "File \u001b[0;32m~/mambaforge/lib/python3.10/ast.py:109\u001b[0m, in \u001b[0;36mliteral_eval.<locals>._convert\u001b[0;34m(node)\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 108\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m left \u001b[38;5;241m-\u001b[39m right\n\u001b[0;32m--> 109\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_convert_signed_num\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnode\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/mambaforge/lib/python3.10/ast.py:83\u001b[0m, in \u001b[0;36mliteral_eval.<locals>._convert_signed_num\u001b[0;34m(node)\u001b[0m\n\u001b[1;32m 81\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 82\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241m-\u001b[39m operand\n\u001b[0;32m---> 83\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_convert_num\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnode\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/mambaforge/lib/python3.10/ast.py:74\u001b[0m, in \u001b[0;36mliteral_eval.<locals>._convert_num\u001b[0;34m(node)\u001b[0m\n\u001b[1;32m 72\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_convert_num\u001b[39m(node):\n\u001b[1;32m 73\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(node, Constant) \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mtype\u001b[39m(node\u001b[38;5;241m.\u001b[39mvalue) \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m (\u001b[38;5;28mint\u001b[39m, \u001b[38;5;28mfloat\u001b[39m, \u001b[38;5;28mcomplex\u001b[39m):\n\u001b[0;32m---> 74\u001b[0m \u001b[43m_raise_malformed_node\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnode\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 75\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m node\u001b[38;5;241m.\u001b[39mvalue\n", "File \u001b[0;32m~/mambaforge/lib/python3.10/ast.py:71\u001b[0m, in \u001b[0;36mliteral_eval.<locals>._raise_malformed_node\u001b[0;34m(node)\u001b[0m\n\u001b[1;32m 69\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m lno \u001b[38;5;241m:=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(node, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlineno\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[1;32m 70\u001b[0m msg \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m on line \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mlno\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m---> 71\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mnode\u001b[38;5;132;01m!r}\u001b[39;00m\u001b[38;5;124m'\u001b[39m)\n", "\u001b[0;31mValueError\u001b[0m: malformed node or string on line 1: <ast.BinOp object at 0x78ff80d4d030>" ] } ], "source": [ "code = \"[2 + 2]\"\n", "\n", "ast.literal_eval(code)" ] }, { "cell_type": "markdown", "id": "8632aa54-2ffc-45d7-b763-8babd8624d76", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Assignment in expressions `:=`" ] }, { "cell_type": "code", "execution_count": 179, "id": "ba2f59a3-beb3-48c7-ae5d-009b7d6457d1", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "line = 'This is the first line.\\n'\n", "line = 'This is the second line.\\n'\n", "line = 'This is the third line.\\n'\n" ] } ], "source": [ "file = open(\"/tmp/example.txt\")\n", "\n", "while (line := file.readline()).endswith(\"\\n\"):\n", " print(f\"{line = }\")" ] }, { "cell_type": "markdown", "id": "6f361149-c089-4340-87f1-8c72f1e8e84a", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Context manager" ] }, { "cell_type": "markdown", "id": "df571612-7275-4ec8-af66-7b2973b7da95", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Using `with` with `open`" ] }, { "cell_type": "code", "execution_count": 180, "id": "d11fca8c-fcd4-42c1-ab0a-20beb8d108ac", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 180, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open(\"/tmp/example.txt\") as file:\n", " text = file.read()\n", "\n", "file.closed" ] }, { "cell_type": "markdown", "id": "c8943b03-1a66-42f5-b3cd-c17f435b7635", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Defining a context manager" ] }, { "cell_type": "code", "execution_count": 181, "id": "fcafadca-4cee-4c0f-972a-e79fe87ad1ce", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "starting the 'with' block\n", "leaving the 'with' block with exception_type = None\n" ] } ], "source": [ "class Scoped:\n", " def __enter__(self):\n", " print(\"starting the 'with' block\")\n", "\n", " def __exit__(self, exception_type, exception_value, exception_traceback):\n", " print(f\"leaving the 'with' block with {exception_type = }\")\n", "\n", "with Scoped() as obj:\n", " pass" ] }, { "cell_type": "markdown", "id": "45a730a5-ee60-4f74-b4e8-ce6ef60d5587", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Coroutines" ] }, { "cell_type": "markdown", "id": "ac1b3d76-c632-4baf-8fe0-856b696b9c18", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Defining and using a function with `async`" ] }, { "cell_type": "code", "execution_count": 182, "id": "b3e70ebf-81d0-4068-b655-4442321f8311", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A\n", "BB\n", "CCC\n", "DDDD\n", "EEEEE\n" ] }, { "data": { "text/plain": [ "['A', 'EEEEE', 'DDDD', 'BB', 'CCC']" ] }, "execution_count": 182, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import asyncio\n", "\n", "async def fake_read_file(filename):\n", " await asyncio.sleep(len(filename))\n", " print(filename)\n", " return filename\n", "\n", "async def get_all_files(filenames):\n", " coroutines = [fake_read_file(filename) for filename in filenames]\n", "\n", " return await asyncio.gather(*coroutines)\n", "\n", "await get_all_files([\"A\", \"EEEEE\", \"DDDD\", \"BB\", \"CCC\"])" ] }, { "cell_type": "markdown", "id": "ba8db85d-6ec2-4792-bcf3-ee4b787e903f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Threading and multiprocessing" ] }, { "cell_type": "markdown", "id": "23025c6e-983d-4ca5-a797-057c41180fc8", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Running a thread, order of execution" ] }, { "cell_type": "code", "execution_count": 183, "id": "be33db7d-c82d-4cf3-881f-3cc6188ef656", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "starting all threads\n", "waiting for all threads to finish\n", "A\n", "BB\n", "CCC\n", "DDDD\n", "EEEEE\n" ] } ], "source": [ "import time\n", "import threading\n", "\n", "def fake_read_file(filename):\n", " time.sleep(len(filename))\n", " print(filename)\n", " return filename\n", "\n", "filenames = [\"A\", \"EEEEE\", \"DDDD\", \"BB\", \"CCC\"]\n", "threads = [threading.Thread(target=fake_read_file, args=(filename,)) for filename in filenames]\n", "\n", "print(\"starting all threads\")\n", "for thread in threads:\n", " thread.start()\n", "\n", "print(\"waiting for all threads to finish\")\n", "for thread in threads:\n", " thread.join()" ] }, { "cell_type": "markdown", "id": "8a46382f-d036-4317-b51a-808af3d35399", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `multiprocessing.Process`" ] }, { "cell_type": "code", "execution_count": 184, "id": "e137e97f-385b-4b2b-bb8a-29c0de37f518", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "starting all processes\n", "waiting for all processes to finish\n", "A\n", "BB\n", "CCC\n", "DDDD\n", "EEEEE\n" ] } ], "source": [ "import multiprocessing\n", "\n", "processes = [multiprocessing.Process(target=fake_read_file, args=(filename,)) for filename in filenames]\n", "\n", "print(\"starting all processes\")\n", "for process in processes:\n", " process.start()\n", "\n", "print(\"waiting for all processes to finish\")\n", "for process in processes:\n", " process.join()" ] }, { "cell_type": "markdown", "id": "5e55113d-3aef-4f40-b750-9cb3be36a986", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Python standard library" ] }, { "cell_type": "markdown", "id": "50e49857-1933-4fb6-b63f-a9926a38ba12", "metadata": {}, "source": [ "### Module `re`" ] }, { "cell_type": "markdown", "id": "6175253a-fafe-4ab1-be6d-f027f68803ad", "metadata": {}, "source": [ "#### `re.match`" ] }, { "cell_type": "markdown", "id": "3d814ef0-a23a-484d-9ba7-9425915f7e67", "metadata": {}, "source": [ "#### `re.finditer`" ] }, { "cell_type": "markdown", "id": "0b0aef5a-2751-4687-9d14-ca472e56c306", "metadata": {}, "source": [ "#### `re.findall`" ] }, { "cell_type": "markdown", "id": "c9f43769-ab32-420b-a15d-b7c6ef92b0f9", "metadata": {}, "source": [ "### Module `json`" ] }, { "cell_type": "markdown", "id": "0c57f695-541c-4810-ac97-5094a5fe72bf", "metadata": {}, "source": [ "#### `json.loads`" ] }, { "cell_type": "markdown", "id": "a2e7a958-8943-4cc8-8bfc-4b5899b854eb", "metadata": {}, "source": [ "#### `json.dumps`" ] }, { "cell_type": "markdown", "id": "0f0cf913-6ae8-4864-a74e-370abdd5c5bf", "metadata": {}, "source": [ "### Module `pickle`" ] }, { "cell_type": "markdown", "id": "2f42b63a-4a2d-4aa6-9014-85793e663af3", "metadata": {}, "source": [ "#### `pickle.load`" ] }, { "cell_type": "markdown", "id": "ec40582e-ee99-4831-a26d-2ef272c3eee3", "metadata": {}, "source": [ "#### `pickle.dump`" ] }, { "cell_type": "markdown", "id": "4b3a6959-78b7-4288-908c-01b2ff6ceb73", "metadata": {}, "source": [ "### Module `shelve`" ] }, { "cell_type": "markdown", "id": "b95c4d08-10fa-4159-af30-f0935351992e", "metadata": {}, "source": [ "#### `shelve.open` in a `with` statement" ] }, { "cell_type": "markdown", "id": "7a74bffc-2654-4563-9587-bb311e10834d", "metadata": {}, "source": [ "### Module `collections`" ] }, { "cell_type": "markdown", "id": "a8614d1e-c7ed-441c-bd24-2e77ca337203", "metadata": {}, "source": [ "#### `collections.Counter`" ] }, { "cell_type": "markdown", "id": "52414f7f-34bf-43d1-92be-ab579ac4c15d", "metadata": {}, "source": [ "### Module `itertools`" ] }, { "cell_type": "markdown", "id": "e84c0481-0c09-4874-91a8-8373409ee40b", "metadata": {}, "source": [ "#### `itertools.chain`" ] }, { "cell_type": "markdown", "id": "ad88b272-3c8a-4e7e-8154-37b1655bffe5", "metadata": {}, "source": [ "#### `itertools.combinations`" ] }, { "cell_type": "markdown", "id": "599e5922-9233-4d70-852f-7f92f05b10f9", "metadata": {}, "source": [ "#### `itertools.permutations`" ] }, { "cell_type": "markdown", "id": "5627f7e7-a160-435a-8717-586cb8084fa4", "metadata": {}, "source": [ "### Module `random`" ] }, { "cell_type": "markdown", "id": "eb79ace3-1333-414e-81bd-26ca68c29614", "metadata": {}, "source": [ "#### `random.choice`" ] }, { "cell_type": "markdown", "id": "d955017f-9a7e-4633-ac49-069097eb1494", "metadata": {}, "source": [ "#### `random.shuffle`" ] }, { "cell_type": "markdown", "id": "f43c01eb-1762-496b-a733-a647d668e3ad", "metadata": {}, "source": [ "### Module `logging`" ] }, { "cell_type": "markdown", "id": "9c14199e-b088-4a2f-97ab-1c433499e116", "metadata": {}, "source": [ "#### `logging.basicConfig`" ] }, { "cell_type": "markdown", "id": "a8464352-70f5-41cc-8297-7714999ba4e9", "metadata": {}, "source": [ "#### `logging.getLogger`" ] }, { "cell_type": "markdown", "id": "1f1e1c9d-75f3-49a6-8422-1c8b1476f043", "metadata": {}, "source": [ "#### `Logger.debug`, `Logger.info`, `Logger.warning`" ] }, { "cell_type": "markdown", "id": "f46ad307-7bf2-4085-9f04-b457de337494", "metadata": {}, "source": [ "### Module `sys`" ] }, { "cell_type": "markdown", "id": "e96b2829-e74c-459c-8dfc-903c5adf39c1", "metadata": {}, "source": [ "#### `sys.argv`" ] }, { "cell_type": "markdown", "id": "6a253388-420a-41f6-9dad-fcd82dd1c115", "metadata": {}, "source": [ "#### `sys.exit`" ] }, { "cell_type": "markdown", "id": "3ee3b851-b46e-483f-b5ac-1984a8449f2b", "metadata": {}, "source": [ "#### `sys.path`" ] }, { "cell_type": "markdown", "id": "fb8e6244-3b8e-43c0-b445-2996d62ed6a6", "metadata": {}, "source": [ "#### `sys.stderr`" ] }, { "cell_type": "markdown", "id": "f804c976-06bd-45c0-be30-d54cfa4c92a0", "metadata": {}, "source": [ "### Module `atexit`" ] }, { "cell_type": "markdown", "id": "15ad2f9c-3d38-4633-aa8c-f984cc68715c", "metadata": {}, "source": [ "#### `atexit.register`" ] }, { "cell_type": "markdown", "id": "b6492f8c-f20c-459d-8628-d38f6b84e162", "metadata": {}, "source": [ "### Module `os`" ] }, { "cell_type": "markdown", "id": "087b96ea-82e8-41fe-a30f-557d9d0972e6", "metadata": {}, "source": [ "#### `os.environ`" ] }, { "cell_type": "markdown", "id": "b2b3b067-877e-4bdd-acd5-bdb66e46d0b9", "metadata": {}, "source": [ "#### `os.path.exists`" ] }, { "cell_type": "markdown", "id": "050ba75c-d40d-4cb2-b66f-d60ad3391607", "metadata": {}, "source": [ "#### `os.getcwd`" ] }, { "cell_type": "markdown", "id": "d42cbdfa-4993-4f1b-910c-323e20c6f2e2", "metadata": {}, "source": [ "#### `os.listdir`" ] }, { "cell_type": "markdown", "id": "37fb22d3-0786-4ade-b5fd-fcbf3a2ea57c", "metadata": {}, "source": [ "#### `os.mkdir`" ] }, { "cell_type": "markdown", "id": "5c0ddf5e-3fb7-4aa3-8c3e-e9c8a43b2fe0", "metadata": {}, "source": [ "#### `os.rename`" ] }, { "cell_type": "markdown", "id": "59325ac1-6700-46e0-9fa9-13cf077a8374", "metadata": {}, "source": [ "#### `os.remove`" ] }, { "cell_type": "markdown", "id": "9fcea55e-9666-4df6-9fb7-8f7d85939bb2", "metadata": {}, "source": [ "### Module `shutil`" ] }, { "cell_type": "markdown", "id": "913c78bb-279c-4147-97ec-0fdf650dfd8b", "metadata": {}, "source": [ "#### `shutil.copy`" ] }, { "cell_type": "markdown", "id": "63c1fb5d-8bf6-4def-9eb1-abae9186d691", "metadata": {}, "source": [ "#### `shutil.copytree`" ] }, { "cell_type": "markdown", "id": "280bbc60-5537-406c-82ea-a0364728c079", "metadata": {}, "source": [ "#### `shutil.rmtree`" ] }, { "cell_type": "markdown", "id": "9316669c-dd90-48b1-a988-57b631b36e57", "metadata": {}, "source": [ "### Module `glob`" ] }, { "cell_type": "markdown", "id": "f85c89a2-9a60-431a-bc4c-b57ce848f54a", "metadata": {}, "source": [ "#### `glob.glob`" ] }, { "cell_type": "markdown", "id": "f2a81de8-d11f-422a-b894-5f14d494c09e", "metadata": {}, "source": [ "### Module `subprocess`" ] }, { "cell_type": "markdown", "id": "fd181521-980a-4d1a-a755-7b9aba375ea7", "metadata": {}, "source": [ "#### `subprocess.run`" ] }, { "cell_type": "markdown", "id": "0fcb2876-4f60-43ba-8c11-d8020c937c66", "metadata": {}, "source": [ "### Module `time`" ] }, { "cell_type": "markdown", "id": "51375bfb-eacd-43dd-83ed-20e81c8f4e17", "metadata": {}, "source": [ "#### `time.sleep`" ] }, { "cell_type": "markdown", "id": "9316fe8a-32a9-4507-b915-1fa3f8582341", "metadata": {}, "source": [ "#### `time.time`" ] }, { "cell_type": "markdown", "id": "5e1e2590-f52b-4aa5-8687-1c9fcb41f4b8", "metadata": {}, "source": [ "#### `time.perf_counter`" ] }, { "cell_type": "markdown", "id": "469c909f-e1eb-40fb-ba2f-b6296d90a7d7", "metadata": {}, "source": [ "### Module `datetime`" ] }, { "cell_type": "markdown", "id": "1e328ee9-5f75-40d5-9f12-aa3812f6c4e3", "metadata": {}, "source": [ "#### `datetime.datetime.now`" ] }, { "cell_type": "markdown", "id": "a7a8965f-5959-4e50-842a-b12f39afc43c", "metadata": {}, "source": [ "#### `datetime.datetime.fromtimestamp`" ] }, { "cell_type": "markdown", "id": "fe82a8a5-ff89-4193-9f41-94e19fe3b6f3", "metadata": {}, "source": [ "#### `datetime.datetime.fromisoformat`" ] }, { "cell_type": "markdown", "id": "fa65b8cc-57ce-49f4-97c0-ba97b555fba4", "metadata": {}, "source": [ "#### `datetime.datetime.strptime`" ] }, { "cell_type": "markdown", "id": "31b4fadf-1c40-429d-a32b-3f44b5ff1f0c", "metadata": {}, "source": [ "#### `datetime.datetime.strftime`" ] }, { "cell_type": "markdown", "id": "95893e4c-41b5-47d2-88ad-645a587ba35f", "metadata": {}, "source": [ "### Module `sqlite3`" ] }, { "cell_type": "markdown", "id": "aa7a9eca-a765-41e1-915a-4f188a3d4075", "metadata": {}, "source": [ "#### `sqlite3.connect`" ] }, { "cell_type": "markdown", "id": "06a12602-56b3-4d93-bd3f-2c49916779de", "metadata": {}, "source": [ "#### `Connection.execute`" ] }, { "cell_type": "markdown", "id": "b4eeab73-f0d4-4a93-935f-8e8b3aa67fee", "metadata": {}, "source": [ "#### `Connection.executemany`" ] }, { "cell_type": "markdown", "id": "db96987d-838a-4c12-b942-935a9de7b190", "metadata": {}, "source": [ "#### `Connection.fetchall`" ] }, { "cell_type": "markdown", "id": "991215e7-b606-4932-8ac2-3376de5fe49a", "metadata": {}, "source": [ "## Static type hinting" ] }, { "cell_type": "markdown", "id": "f2e9313a-a53c-4076-a2bc-a7a87f9ff1b5", "metadata": {}, "source": [ "### Annotations" ] }, { "cell_type": "markdown", "id": "94d00012-335f-43c2-910d-223c694fdb95", "metadata": {}, "source": [ "#### Annotating arguments and return types of functions" ] }, { "cell_type": "markdown", "id": "349d107c-1576-4cbb-9137-13fa0325f61a", "metadata": {}, "source": [ "#### Annotating variables" ] }, { "cell_type": "markdown", "id": "0c8a9c55-2a98-4d32-80e1-71ecae3f1277", "metadata": {}, "source": [ "#### Annotating class members" ] }, { "cell_type": "markdown", "id": "e304a552-ecc7-4fab-8d9f-353d0afdb1b9", "metadata": {}, "source": [ "### Common types" ] }, { "cell_type": "markdown", "id": "86057347-d87e-421e-a8b8-ab17a98b4b8e", "metadata": {}, "source": [ "#### `Any`" ] }, { "cell_type": "markdown", "id": "824d9f9f-d328-438e-b600-c4f3f3d0da61", "metadata": {}, "source": [ "#### `None`" ] }, { "cell_type": "markdown", "id": "853fca58-46cb-4d9e-9256-3cef9ba98c94", "metadata": {}, "source": [ "#### `int`" ] }, { "cell_type": "markdown", "id": "d9309bb5-49a0-4d97-ab80-7b2bc202bce6", "metadata": {}, "source": [ "#### `str`" ] }, { "cell_type": "markdown", "id": "29c88184-b955-4fc0-8e58-def08595b3a0", "metadata": {}, "source": [ "#### `list[T]`" ] }, { "cell_type": "markdown", "id": "33ac2743-4170-4a34-9464-4395a7e836c6", "metadata": {}, "source": [ "#### `dict[K, V]`" ] }, { "cell_type": "markdown", "id": "bce4bf19-1792-4ced-a03a-12cc96309ddd", "metadata": {}, "source": [ "#### `callable[[T1, T2], T3]`" ] }, { "cell_type": "markdown", "id": "0d8a922e-e397-4d7a-b972-80c9f323bc71", "metadata": {}, "source": [ "#### `Optional[T]`" ] }, { "cell_type": "markdown", "id": "8238e8a1-d243-4f7b-96f2-951f7124acb4", "metadata": {}, "source": [ "#### `Union[T1, T2, T3]`" ] }, { "cell_type": "markdown", "id": "0c735365-b079-4dbc-adf6-080dfe0274ea", "metadata": {}, "source": [ "### Advanced typing features" ] }, { "cell_type": "markdown", "id": "4e1795b4-ba86-4a2d-8835-d0159024d56a", "metadata": {}, "source": [ "#### `Self`" ] }, { "cell_type": "markdown", "id": "c9bb7bf7-55d5-45e8-8aca-dc655cb3e22a", "metadata": {}, "source": [ "#### `TypeVar`" ] }, { "cell_type": "markdown", "id": "bc83d6f1-466f-405f-83b9-ed712f5afe48", "metadata": {}, "source": [ "#### `TypeVar` with constraints" ] }, { "cell_type": "markdown", "id": "51e3fd40-d272-4fc2-b478-d0c29f21c470", "metadata": {}, "source": [ "#### `Generic`" ] }, { "cell_type": "markdown", "id": "3008e300-779a-4867-a0e4-06237f3881b2", "metadata": {}, "source": [ "#### Forward references" ] }, { "cell_type": "markdown", "id": "4cea46bc-19b6-4e95-bb9c-5d51c45fa51b", "metadata": {}, "source": [ "### Protocols" ] }, { "cell_type": "markdown", "id": "5cd6191a-5f92-4a65-be9c-08fd6f63f45f", "metadata": {}, "source": [ "#### Example protocol and instantiation" ] }, { "cell_type": "markdown", "id": "58a4d630-23cd-46f6-a933-17bd85f43b2d", "metadata": {}, "source": [ "## NumPy" ] }, { "cell_type": "markdown", "id": "41af9e11-e886-4c64-9ec2-0c2b270c7073", "metadata": {}, "source": [ "### Array creation" ] }, { "cell_type": "markdown", "id": "bfcc72f0-30d8-4e16-ba3a-cd0d218cc047", "metadata": {}, "source": [ "#### One-dimensional `np.array`" ] }, { "cell_type": "markdown", "id": "20c702e3-24e5-4db7-9058-49cea793277c", "metadata": {}, "source": [ "#### Two-dimensional `np.array`" ] }, { "cell_type": "markdown", "id": "0f0a3ac3-9b75-4fb6-8c57-57d93cb10289", "metadata": {}, "source": [ "#### Three-dimensional `np.zeros`" ] }, { "cell_type": "markdown", "id": "5dd74deb-e3ec-44fa-a089-cef59e495500", "metadata": {}, "source": [ "#### `np.linspace`" ] }, { "cell_type": "markdown", "id": "770702ce-215f-45df-8c08-89682b2986d4", "metadata": {}, "source": [ "#### `np.meshgrid`" ] }, { "cell_type": "markdown", "id": "9f6aed39-f204-4e77-a6fe-73a2c5f2217b", "metadata": {}, "source": [ "#### `np.arange`" ] }, { "cell_type": "markdown", "id": "aa7f9acf-2614-4a89-a794-5cb4961e2c98", "metadata": {}, "source": [ "### Random array creation" ] }, { "cell_type": "markdown", "id": "c830da2c-e564-466e-9b83-71fc8f30abfa", "metadata": {}, "source": [ "#### `np.random.randint`" ] }, { "cell_type": "markdown", "id": "b7b6ae74-1dbe-4f9c-bbd5-79878d924293", "metadata": {}, "source": [ "#### `np.random.normal`" ] }, { "cell_type": "markdown", "id": "5ac75c01-5a7f-46a3-9fca-00d0e4c36c02", "metadata": {}, "source": [ "#### `np.random.poisson`" ] }, { "cell_type": "markdown", "id": "a537ff5f-ba85-43cf-a6dc-f24d4e155bbe", "metadata": {}, "source": [ "### Array math" ] }, { "cell_type": "markdown", "id": "20ff0d0e-2a8e-4ee0-8d74-ac58043aedb1", "metadata": {}, "source": [ "#### Mathematical formula" ] }, { "cell_type": "markdown", "id": "0155e5c5-a0d7-49cf-875b-daf6b91be42b", "metadata": {}, "source": [ "#### Universal functions (ufuncs)" ] }, { "cell_type": "markdown", "id": "f48b4205-bd17-42a0-80bc-8b504ca1e97f", "metadata": {}, "source": [ "#### Elementwise comparison" ] }, { "cell_type": "markdown", "id": "990bbfa6-e83d-4a77-bb84-5504caca8a70", "metadata": {}, "source": [ "#### Boolean operators `&`, `|`, `~`" ] }, { "cell_type": "markdown", "id": "311538a7-9492-4ffc-ba0f-102b6bb240bf", "metadata": {}, "source": [ "#### Statistics" ] }, { "cell_type": "markdown", "id": "dcf4164f-0d4c-4441-948c-e0c1b030e4b8", "metadata": {}, "source": [ "### Array slicing" ] }, { "cell_type": "markdown", "id": "942902e9-044c-4216-876a-dfd18bf34937", "metadata": {}, "source": [ "#### One-dimensional range slicing" ] }, { "cell_type": "markdown", "id": "01e48f90-8de4-4ea6-962d-a9e5b0880e11", "metadata": {}, "source": [ "#### Three-dimensional range slicing" ] }, { "cell_type": "markdown", "id": "e64cfc63-ac5b-4dc6-a20c-9b431bb7e3c2", "metadata": {}, "source": [ "#### Boolean array slicing" ] }, { "cell_type": "markdown", "id": "55ced44d-8745-48eb-a4c6-3d837b381b63", "metadata": {}, "source": [ "#### Integer array slicing" ] }, { "cell_type": "markdown", "id": "168a0362-30ee-46e6-a443-929a98503ce2", "metadata": {}, "source": [ "### Array manipulation" ] }, { "cell_type": "markdown", "id": "fa3c6747-d519-4ead-b617-fe587ec68499", "metadata": {}, "source": [ "#### Changing `shape`" ] }, { "cell_type": "markdown", "id": "5b163111-04ea-491c-9c52-a46fc491f81c", "metadata": {}, "source": [ "#### Converting `dtype`" ] }, { "cell_type": "markdown", "id": "1f9c49db-4540-4f77-a571-64fea437f1dc", "metadata": {}, "source": [ "#### Reinterpreting `dtype`" ] }, { "cell_type": "markdown", "id": "f4b1fa45-7a95-4792-aa27-6b2aef498c44", "metadata": {}, "source": [ "#### `np.ravel`" ] }, { "cell_type": "markdown", "id": "ccc81a1e-0176-4011-a396-cc41d5be3bf0", "metadata": {}, "source": [ "#### `np.concatenate`" ] }, { "cell_type": "markdown", "id": "e6d8af66-a760-4e5b-b365-4176810ec596", "metadata": {}, "source": [ "#### `np.where`" ] }, { "cell_type": "markdown", "id": "6155df89-aecd-4edf-9c6e-2ff8711db139", "metadata": {}, "source": [ "#### `np.unique`" ] }, { "cell_type": "markdown", "id": "04f5b497-a4ef-401c-be47-f5c9c18f5e5f", "metadata": {}, "source": [ "## Pandas" ] }, { "cell_type": "markdown", "id": "08558d9a-fba3-4f84-8df0-ca3f6885252c", "metadata": {}, "source": [ "### DataFrame I/O" ] }, { "cell_type": "markdown", "id": "f6bb2a7e-07f1-48b7-90a4-153ada553ac2", "metadata": {}, "source": [ "#### `pd.DataFrame` constructor" ] }, { "cell_type": "markdown", "id": "38d94bff-01ca-4e06-bba4-c231cb2de07e", "metadata": {}, "source": [ "#### `pd.read_csv`" ] }, { "cell_type": "markdown", "id": "1d77c9c4-ff31-4584-8e65-0c13d3160509", "metadata": {}, "source": [ "#### `pd.to_csv`" ] }, { "cell_type": "markdown", "id": "3fdf92f8-550f-4b34-a392-8b2914d85967", "metadata": {}, "source": [ "### Data summary" ] }, { "cell_type": "markdown", "id": "25050549-428a-4fc5-b374-36119d833998", "metadata": {}, "source": [ "#### `DataFrame.describe`" ] }, { "cell_type": "markdown", "id": "b0b30961-a289-44f3-a090-7906a2eb47bc", "metadata": {}, "source": [ "### Data math" ] }, { "cell_type": "markdown", "id": "6e620c3c-6b07-4702-a786-39a37820d9c0", "metadata": {}, "source": [ "#### Mathematical formula" ] }, { "cell_type": "markdown", "id": "5b8d81f5-997f-418d-81e9-b56f05d746db", "metadata": {}, "source": [ "#### Universal functions (ufuncs)" ] }, { "cell_type": "markdown", "id": "fb0b4b24-6308-48a4-81bc-845faffdd411", "metadata": {}, "source": [ "#### Elementwise comparison" ] }, { "cell_type": "markdown", "id": "1b817364-6c1d-40f4-a4e8-b169e8a59389", "metadata": {}, "source": [ "#### Boolean operators `&`, `|`, `~`" ] }, { "cell_type": "markdown", "id": "da369b5e-9eb3-42f2-894a-e8d2d339176d", "metadata": {}, "source": [ "### Data cleaning" ] }, { "cell_type": "markdown", "id": "df435c4b-14b9-466b-a2d6-b6efcdac6a05", "metadata": {}, "source": [ "#### `DataFrame.dropna`" ] }, { "cell_type": "markdown", "id": "ca41ce45-6747-42ce-b571-fe1e1ea410eb", "metadata": {}, "source": [ "#### `DataFrame.fillna`" ] }, { "cell_type": "markdown", "id": "8acbbefd-3d9b-4beb-ad99-d0b68c293c8f", "metadata": {}, "source": [ "#### `DataFrame.drop_duplicates`" ] }, { "cell_type": "markdown", "id": "7c31991a-8700-4f10-a203-989ff02758da", "metadata": {}, "source": [ "#### `DataFrame.sort_values`" ] }, { "cell_type": "markdown", "id": "74544688-abb9-4900-bb6a-66f75e1a6149", "metadata": {}, "source": [ "### Data access" ] }, { "cell_type": "markdown", "id": "dca43ed6-b757-43b2-8a85-5386b0c19e4a", "metadata": {}, "source": [ "#### `DataFrame.loc`" ] }, { "cell_type": "markdown", "id": "355a826d-08cf-4fd0-93ec-ed3dfde91d25", "metadata": {}, "source": [ "#### `DataFrame[\"column_name\"]`" ] }, { "cell_type": "markdown", "id": "a2439e24-1380-4dd2-bc20-4523df2ae74d", "metadata": {}, "source": [ "### Adding and removing columns" ] }, { "cell_type": "markdown", "id": "0bfd8f53-4d96-4ec9-973d-c0a6065d5725", "metadata": {}, "source": [ "#### `DataFrame[\"column_name\"]` assignment" ] }, { "cell_type": "markdown", "id": "f0e8297c-883d-4a24-98d3-7a6b017a9aa2", "metadata": {}, "source": [ "#### `DataFrame.drop`" ] }, { "cell_type": "markdown", "id": "1efe6e5d-5c76-44ad-8823-30fd0d5c8a73", "metadata": {}, "source": [ "### Joining and concatenating" ] }, { "cell_type": "markdown", "id": "59cef7f3-b36f-49e9-ad80-67b791a4bd46", "metadata": {}, "source": [ "#### Inner-join `pd.merge`" ] }, { "cell_type": "markdown", "id": "0c7c682d-95bc-450b-b324-ef5ff2b93712", "metadata": {}, "source": [ "#### Outer-join `pd.merge`" ] }, { "cell_type": "markdown", "id": "e8d0f72c-8600-4346-b4b6-95c49926d75d", "metadata": {}, "source": [ "#### Row `pd.concat`" ] }, { "cell_type": "markdown", "id": "075a0722-1c8b-4397-88dc-2af3fcfb8086", "metadata": {}, "source": [ "### Group by" ] }, { "cell_type": "markdown", "id": "7a71ca75-8d07-4a81-87bc-98efa76dcc00", "metadata": {}, "source": [ "#### `DataFrame.groupby` with `sum`" ] }, { "cell_type": "markdown", "id": "6063fd17-7214-41ce-b6b2-940c27925671", "metadata": {}, "source": [ "#### `DataFrame.groupby` with `max`" ] }, { "cell_type": "markdown", "id": "feded4e6-e48e-41dc-829a-f1febfab3ad8", "metadata": {}, "source": [ "#### `DataFrame.groupby` with `first`" ] }, { "cell_type": "markdown", "id": "40bcab00-a7ed-4b8b-bce9-87eb87d6756d", "metadata": {}, "source": [ "### Plotting" ] }, { "cell_type": "markdown", "id": "2d0af4e5-7fe5-4d1e-9f81-a7fe657e5cdf", "metadata": {}, "source": [ "#### `DataFrame.plot`" ] }, { "cell_type": "markdown", "id": "0fa9f4cf-dd8c-40e3-808b-e3df496f2ef5", "metadata": {}, "source": [ "#### `DataFrame.plot.scatter`" ] }, { "cell_type": "markdown", "id": "baa0a18f-8587-4315-b1a5-f83105bd4e54", "metadata": {}, "source": [ "## Matplotlib" ] }, { "cell_type": "markdown", "id": "a7217e2e-f9ea-46c5-bd9a-5f82f62edc7c", "metadata": {}, "source": [ "### Setting up a plot" ] }, { "cell_type": "markdown", "id": "f2a34cbd-7273-400f-adad-bafee3ee6c34", "metadata": {}, "source": [ "#### `plt.subplots`" ] }, { "cell_type": "markdown", "id": "8728b01d-1c4d-4a13-a4af-ae978603d19f", "metadata": {}, "source": [ "#### `plt.subplots` with `figsize`" ] }, { "cell_type": "markdown", "id": "9390a6ea-c015-489d-822c-69b1e85987d9", "metadata": {}, "source": [ "#### Splitting the frame with `plt.subplots`" ] }, { "cell_type": "markdown", "id": "55e55509-93d6-4553-970d-79168675f81c", "metadata": {}, "source": [ "#### Overlay with `ax`" ] }, { "cell_type": "markdown", "id": "57d7f5cd-fe62-4440-93bd-c96fcf0b5f31", "metadata": {}, "source": [ "### Basic plots" ] }, { "cell_type": "markdown", "id": "ad0d550e-125d-41ed-afb7-bf508ec55eca", "metadata": {}, "source": [ "#### `plot`" ] }, { "cell_type": "markdown", "id": "e44c9c78-37cc-4060-af36-22c5a08faba3", "metadata": {}, "source": [ "#### `scatter`" ] }, { "cell_type": "markdown", "id": "a92cabee-2a0f-4bcb-aa62-868c4f59d527", "metadata": {}, "source": [ "#### `imshow` with `plt.gca().invert_yaxis()`" ] }, { "cell_type": "markdown", "id": "01843f73-9637-4093-bee5-6fcdd6109816", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Styles" ] }, { "cell_type": "markdown", "id": "355ec252-9e40-4d72-a7f3-cb0c0ebd2e41", "metadata": {}, "source": [ "#### Line style" ] }, { "cell_type": "markdown", "id": "7697693a-8ea0-4fe1-ba81-939130f29775", "metadata": {}, "source": [ "#### Line color" ] }, { "cell_type": "markdown", "id": "6313fe35-b56a-4066-9428-ea3fb3cea3c2", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Plot coordinates" ] }, { "cell_type": "markdown", "id": "8ce24b88-9ffc-4adf-a634-affc1dbb2556", "metadata": {}, "source": [ "#### `set_xlim`, `set_ylim`" ] }, { "cell_type": "markdown", "id": "6b595ff4-fd1e-4d8e-90e0-89395d6522c0", "metadata": {}, "source": [ "#### `set_xscale`, `set_yscale`" ] }, { "cell_type": "markdown", "id": "d2dfb2b1-6f9a-42a7-bab3-f1788f26f7a4", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "### Labeling" ] }, { "cell_type": "markdown", "id": "1887a98c-f338-4c62-b9a9-b7cdd741f8af", "metadata": {}, "source": [ "#### `set_xticks`, `set_yticks`" ] }, { "cell_type": "markdown", "id": "a89abbe6-8fbd-4a43-804e-c6a01b558ea7", "metadata": {}, "source": [ "#### `set_xlabel`, `set_ylabel`" ] }, { "cell_type": "markdown", "id": "b5c235d8-0300-487a-9d40-ebe069282af3", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### `legend`" ] } ], "metadata": { "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.10.14" } }, "nbformat": 4, "nbformat_minor": 5 }