{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3c0aa95e-2643-4cc3-ae5a-cbbc9bf282f7",
   "metadata": {},
   "source": [
    "## Sequences and Collections"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f57580a6-7c10-456f-ba0d-a1255f88fb11",
   "metadata": {},
   "source": [
    "### Strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2ba0ae68-4794-4cc4-9b58-3f63f99c0b3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'This is a string'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"This is a string\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b9957874-c179-4f1f-89f8-f4d09fff3305",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(\"This is a string\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "67020ab8-bb43-4bec-8b03-adf2f50f6be1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'This is also a string'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'This is also a string'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cb78cd53-47f1-40d0-890c-4628f0682e98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'This is a\\nmultiline\\nstring but also a comment'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"This is a\n",
    "multiline\n",
    "string but also a comment\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "de58ee06-8644-4ed5-8738-3a80b452ac9c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is a\n",
      "multiline\n",
      "string\n"
     ]
    }
   ],
   "source": [
    "print(\"\"\"This is a\n",
    "multiline\n",
    "string\"\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18bf26fc-44f5-4830-9009-d706be3b50ce",
   "metadata": {},
   "source": [
    "Note: Python does not have a character data type, a single character is simply a string with a length of 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7676966b-7578-4191-89ad-8f04e18d3381",
   "metadata": {},
   "source": [
    "Lexicographic comparison:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2c6c9f0b-b416-4112-bf4f-19938f12ec0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"abcrt\" < \"acb\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fe0cf4a5-c49f-4a83-aab5-0c559b4ede69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'42 is the answer to life the universe and everything'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"42\" + \" is the answer to life the universe and everything\" # string concatenation"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfcb6831-0356-4f8f-bc2e-98ea7b363011",
   "metadata": {},
   "source": [
    "#### Indexing and Slicing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "97921087-244c-451b-8f0e-b712182df5b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'s'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = \"This is a string\"\n",
    "s[3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "730e221b-95cb-4108-aaba-3bce77558aa5",
   "metadata": {},
   "source": [
    "Access time is O(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63058f89-9649-44da-8b70-d0627bb508cf",
   "metadata": {},
   "source": [
    "Strings are **immutable**!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "35f82c50-3d0c-4f4c-9474-2065cccbb88e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'str' 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[9], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m s \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThis is a string\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m----> 2\u001b[0m \u001b[43ms\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;124m'\u001b[39m\u001b[38;5;124ma\u001b[39m\u001b[38;5;124m'\u001b[39m\n",
      "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "s = \"This is a string\"\n",
    "s[3] = 'a'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1212a4b0-e55c-4a24-807b-59f0f2c0b3cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'g'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "02836e30-3375-4969-a9ed-5e9f42c1a453",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'n'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c01d8fad-a9a8-4684-b12d-65c1364789d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'s is a string'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[3:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "adad20c6-7ba9-426d-b003-5bb7bf42875e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is a string\n"
     ]
    }
   ],
   "source": [
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f0aa8322-0300-4704-a0ad-6861e66c32c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'This is'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[:7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1ba6aaab-133c-467a-8dca-3ce132160b36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'s is'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[3:7]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fea68fae-7d2d-4931-b565-f744763a17ef",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary>Hint!</summary>\n",
    "Returns the index range [3,7)\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "daae2341-f420-45f8-8276-c55ccd39cf4e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'si'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[3:7:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "13218532-9e4d-4451-b0f5-df437d03d666",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'s a si '"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[10:3:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5a0c96d4-ee70-4417-a276-12ddea62d7ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'gnirts a si sihT'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "92dc2ac5-0124-45d8-9978-9ab9e64d48ae",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'str' 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[19], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43ms\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m7\u001b[39;49m\u001b[43m]\u001b[49m\u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mz\u001b[39m\u001b[38;5;124m'\u001b[39m\n",
      "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "s[7]= 'z'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b69f9d0f-e85b-4394-b5b9-8e809294c4ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'This is a string'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "aef2f8d1-b66a-4728-ba4e-21b2fe394a23",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = s[0:6] + 'z' + s[7:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5d46ae0f-e2b2-4951-a896-7ef169fe68a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This iz a string\n"
     ]
    }
   ],
   "source": [
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b88db498-7e48-40b2-898c-f11075256423",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary>Hint!</summary>\n",
    "Doesn't change the initial string, but creates a new string object.\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "5e37cd86-4fc4-42d3-adba-7a497c0f4353",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'si s'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = \"This is a string\"\n",
    "s[6:2:-1] # From index 6 (included) to index 2 (not included) with step -1 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "921ae0cc-ea2d-42ce-ac57-13e8e3fe686f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'gnirts a si sihT'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[::-1] # From the end to the beginning with step -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "34e2c3b2-3582-4cdb-8c69-5efa85fe365b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0369\n"
     ]
    }
   ],
   "source": [
    "s = \"0123456789\"\n",
    "print(s[::3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06609031-194e-4b20-80bd-93765e813343",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary>Hint!</summary>\n",
    "Pick every item that's a multiple of 3.\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "5e46e060-d574-4612-b607-cd758cd4c653",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0369\n"
     ]
    }
   ],
   "source": [
    "s = \"..0123456789..\"\n",
    "print(s[2::3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2238c7a5-fc63-4002-a4ed-529458110e08",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary>Hint!</summary>\n",
    "Pick every item that's a multiple of 3, starting from index 2.\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2364d7b3-c7d1-49e5-8f99-503fe0aacd60",
   "metadata": {},
   "source": [
    "Slice operations create a **new** string object!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f6d587e-322f-486e-9240-4c1b71d98c96",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary>Slice complexity?</summary>\n",
    "O(n)\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "2f4d3b06-50f6-4fac-931b-6f5787e57a36",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "THIS IS A STRING\n",
      "this is a string\n"
     ]
    }
   ],
   "source": [
    "s = \"this is a string\"\n",
    "print(s.upper())\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "9632c315-286d-4163-a7e0-92278277cbd9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['this', 'is', 'a', 'string']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"this is a string\".split(' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "9e717379-53d6-4f0b-afd2-dfe0a6ea6da0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this\n",
      "is\n",
      "a\n",
      "string\n"
     ]
    }
   ],
   "source": [
    "for x in \"this is a string\".split(' '):\n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a451715-a915-48c3-9c95-28c75a2bf5c8",
   "metadata": {},
   "source": [
    "Write a function that checks if a string is a pallindrome"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "6d3db74f-1290-4c09-bb42-8bc4c78ccf70",
   "metadata": {},
   "outputs": [],
   "source": [
    "def ispalindrome(s):\n",
    "    return s == s[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "2625a01e-5952-4428-9db9-77b075b5f37b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ispalindrome(\"hahah\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b14f0d36-0f18-4a7a-b6e7-6f60f74d77f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ispalindrome(\"haha\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "371ea2df-61ad-4ab4-971f-d9cd0a6c5f54",
   "metadata": {},
   "outputs": [],
   "source": [
    "def ispalindrome2(s):\n",
    "    N = len(s)\n",
    "\n",
    "    for i in range(N//2):\n",
    "        if s[N-i-1] == s[i]: continue\n",
    "        return False\n",
    "\n",
    "    return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "d154e7d1-4fe2-4fef-97f9-6e8bc8a3bc7e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ispalindrome2(\"hahah\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "ace0fedc-d039-492e-8c0d-530f6ac88b6a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ispalindrome2(\"haha\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6feab4ca-036b-4043-a470-0a0be0f960e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "ispalindrome2(\"habbah\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "a8ce8ed2-d1cc-4117-a31c-c67e11b5dac8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "h\n",
      "e\n",
      "l\n",
      "l\n",
      "o\n",
      " \n",
      "w\n",
      "o\n",
      "r\n",
      "l\n",
      "d\n",
      "!\n"
     ]
    }
   ],
   "source": [
    "s = \"hello world!\"\n",
    "for c in s: print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24578f70-2db2-4d59-8b2f-c761ce4558b1",
   "metadata": {},
   "source": [
    "### Tuples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "a4749be9-84fd-4c9b-bcea-eff0bcba0f3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 3)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1,2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "6ea02053-6b0a-4634-9495-21fa5825f65c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type((1,2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "a0581ace-615e-49e8-af22-ec317752bf1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(42, 'Python', True, ('cats', 'dogs'))"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = (42, \"Python\", True, (\"cats\", \"dogs\"))\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "c6514ec8-a4ca-4a55-ad7e-9f118eaa17fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "e3cf7a5b-f275-444a-aacd-457ee8b54f3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "42"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "ef1e4860-a1e8-4d76-8e58-ed8cda03b868",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'t'"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[3][0][2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28dba5e1-5691-4a12-b92d-de66b6739d1f",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary>Hint!</summary>\n",
    "Tuples are zero-indexed.\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "21361c6f-1b4e-434a-920e-dd53dc44bae8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'dogs'"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[3][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "3640de0e-adc7-4c12-ab47-15f69c93a464",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Python'"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "1c86a216-05dc-4e1d-912d-782b5961f761",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('cats', 'dogs')"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i = 2\n",
    "t[i + 1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3664584-17e0-4c81-8573-746257220fe7",
   "metadata": {},
   "source": [
    "Indexes of tuples can be **dynamic**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "70dea25c-c6b9-410d-a70a-b83351a9f9c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42\n",
      "Python\n",
      "True\n",
      "('cats', 'dogs')\n"
     ]
    }
   ],
   "source": [
    "for i in [0,1,2, 3]:\n",
    "    print(t[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "a2c0efd1-adff-4eae-af6d-4b38b323afd1",
   "metadata": {},
   "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[47], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mt\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m43\u001b[39m\n",
      "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "t[0] = 43"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "591ab374-03b4-4e6c-9915-902bbd678459",
   "metadata": {},
   "source": [
    "Tuples are also **immutable**!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "7e34e3b6-66a0-44d9-a2bd-812a2fdddb00",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(42, 'Python', True, ('cats', 'dogs'))"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1c2865a-efb6-43f8-829c-fe35fe738cd3",
   "metadata": {},
   "source": [
    "Slices work just as in strings:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "28f752c1-b697-4411-98d3-391faef3459f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Python', True)"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "6f4bbeb9-9317-4e2d-8dc9-4a6fef12def4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(('cats', 'dogs'), True, 'Python', 42)"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "19af8bf7-45fe-4f94-a30d-ce2bfe2a05ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 3, 4)"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1,2)+(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "9abc02bf-4f0d-4f43-bd43-087abc5baf9e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2\n"
     ]
    }
   ],
   "source": [
    "(a, b) = (1, 2)\n",
    "\n",
    "print(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "cbbb1efd-b985-4f2c-8763-39e177bf13d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 5, 4)"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1,2 + 3,4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "17a96f95-7dd9-4195-999d-b67ce783fa27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(42, 'Python', True, ('cats', 'dogs'), 42, 'Python', True, ('cats', 'dogs'))"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t + t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "07854e4d-dc82-4570-ad42-7ea22fdedef5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(42, 'Python', True, ('cats', 'dogs'), 42, 'Python', True, ('cats', 'dogs'))"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 * t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "0420ce16-562e-4285-900f-66029a9643bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8, 8)"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(3 + 5,) * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "b8957c2d-4d12-4b22-b9b0-247949a4158f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(3 + 5) * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "0492eff0-6f43-419e-8118-ecbc0e361d37",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can only concatenate tuple (not \"str\") to tuple",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[58], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mt\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mis this a singleton tuple?\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# NO!\u001b[39;00m\n\u001b[1;32m      2\u001b[0m \u001b[38;5;66;03m# the same as t + \"is this a singleton tuple?\"\u001b[39;00m\n",
      "\u001b[0;31mTypeError\u001b[0m: can only concatenate tuple (not \"str\") to tuple"
     ]
    }
   ],
   "source": [
    "t + (\"is this a singleton tuple?\") # NO!\n",
    "# the same as t + \"is this a singleton tuple?\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "dc30c0a2-befd-4965-949d-c66fc2f07249",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(42, 'Python', True, ('cats', 'dogs'), 'this is a singleton tuple')"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t + (\"this is a singleton tuple\",)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "a2e0a53a-ec21-4afa-804c-f4270eee8af1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(42, 'Python', True, ('cats', 'dogs'))"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t + () # the empty tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "41c04d19-c22b-431c-bf1e-5ea3309f2cd5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('my tuple', 42, 'my tuple', 42, 'my tuple', 42)"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 * (\"my tuple\", 42)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de997f32-4470-437d-8f89-6f2732442855",
   "metadata": {},
   "source": [
    "Lexicographic Comparison"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "50ef861d-bb79-400c-a036-20529fc4a65d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1, 2, 3, 4) < (1, 4, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "2260af23-3a54-4fab-ba35-09a1b6c6a5da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(42, 'Python', True, ('cats', 'dogs'))"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91cce889-16bc-4708-8761-f4b534890608",
   "metadata": {},
   "source": [
    "Quiz: Create a tuple same as `t` but with `False` instead of `True`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "3aa0432d-1eda-45ba-9eb3-71e49746f97e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(42, 'Python', False, ('cats', 'dogs'))"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[:2] + (False,) + t[3:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9469bf5-dd5d-40ad-b309-a43277448005",
   "metadata": {},
   "source": [
    "### Lists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "1adddd4b-012a-4873-b0bb-5b8711c14022",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = [1, 2, 3, 4]\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "11bdbd4b-6ce5-4788-b564-c9be80cd5f04",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 'string', 3.14, [4, 3], ([1, 2], 42)]"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = [1, 2, \"string\", 3.14, [4,3], ([1,2], 42)]\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "737a45ad-f43b-4f43-ba89-9ba69fc1c216",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[42, 2, 'string', 3.14, [4, 3], ([1, 2], 42)]\n",
      "[42, 2, 'string', 3.14, [4, 3], 'hihi']\n"
     ]
    }
   ],
   "source": [
    "l[0] = 42\n",
    "print(l)\n",
    "l[5] = \"hihi\"\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "5510d8cc-0a8f-4e68-9ff2-391a2a3fcb30",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = list(range(1,5))\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "534b0256-5b3e-4f3b-a6a7-103159bd7d9d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(range(1,2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93849f91-6f71-4bb3-a163-57a9ae3a0673",
   "metadata": {},
   "source": [
    "Indexing and slices work just as in strings and tuples:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "cb7d2645-0a06-47f7-8a77-cf54395ddcb2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.14"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = [1, 2, \"string\", 3.14, 4]\n",
    "l[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "660bcbd2-3a3c-4c27-9f90-db3fda21a2cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.14"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i = 3\n",
    "l[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "1639faaf-2d0e-406d-b019-7bd9581bd10a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[72], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43ml\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m42\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[0;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "l[42]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99dde150-c506-4a50-a787-15e5e62cd37a",
   "metadata": {},
   "source": [
    "But..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b308dec-cb23-42af-b228-541766c8b39e",
   "metadata": {},
   "source": [
    "Lists **are muttable**!!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "240339b5-54c0-4df5-975f-8e4427b97ab3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 'string', 3.14, 4]"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "d932cdad-df08-4bde-9558-9698fab0444e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, [3, 4, 5], 'string', 3.14, 4]"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l[1] = [3,4,5]\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "d5348164-9832-42ed-ba36-b52acee0dc24",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['string', 3.14]"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = [1, 2, \"string\", 3.14, 4]\n",
    "l[2:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "d9cef643-a0c7-48d1-b75b-6297ef536c48",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 3.14, 'string', 2, 1]"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "64a39b5d-6f5b-47e1-832d-e219e6207935",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 'string', 3.14, 4]"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del l[1]\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "f0a96f43-b975-4768-bb78-948d9cbdedb1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type([1,\"a\", True])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "ddebd914-6f43-458c-90e9-443a14507ae7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "4 in [1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "9d65e8d0-919a-401b-b931-822a71f4c722",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[4,5,6] in [1,2,3,[4,5,7]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "9ee7f3fa-e8d3-49c1-b2c8-f0a80dc7a661",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 'string', 3.14, 4, 4]"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l.append(4)\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "31d651f8-d24e-4809-99a6-60e560f0a9a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "4 in l"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d542800d-7e8f-42e4-9e02-d7927ace1906",
   "metadata": {},
   "source": [
    "| Operation                  | Time Complexity | Notes                                  |\n",
    "|---------------------------|------------------|----------------------------------------|\n",
    "| Access (`x[i]`)           | O(1)             | Direct index access                    |\n",
    "| Delete (`del x[i]`)       | O(n)             | Requires shifting elements             |\n",
    "| Containment (`x in list`) | O(n)             | Linear search, not O(1)                |\n",
    "| Append (`list.append(x)`) | O(1) amortized   | Occasionally O(n) due to resizing      |\n",
    "\n",
    "In Python, list are essentially dynamic arrays."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "79c9e8d2-632b-4d46-bf4f-5f465f5455ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l.pop() # O(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "c7334c17-0183-4fc5-8a6f-283360f97457",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 'string', 3.14, 4]"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "1a020912-284b-4b7b-80d7-752dba711775",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "a1619a49-ba6d-46e5-8c34-95906076052b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 'string', 3.14]"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6de653de-f9d1-4be9-bd25-2c3c428757d0",
   "metadata": {},
   "source": [
    "Lists can be used as stacks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "dbdfdae3-b16d-408a-bb3a-a361b4ec8945",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 1, 2, 3]"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[1,2,3] + [1,2,3] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "32de7319-9b9b-44b7-8e5b-a9e8a84bbc92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 2, 3], [4, 5, 6]]"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l2d = [[1,2,3],[4,5,6]]\n",
    "l2d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "dd3c2abc-518b-44a6-81a5-4a536aa5768b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l2d[0][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "7edeb3d6-4b24-4390-988d-a12b925f0436",
   "metadata": {},
   "outputs": [],
   "source": [
    "L = [1,2,3,4,5,6,42]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "68d5a7a1-219a-4b18-9755-78d4b12dc3ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "42\n"
     ]
    }
   ],
   "source": [
    "for x in L: print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "928d58ae-32a7-4164-89dd-352eb97cdf94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "42"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L[-1] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "a8e6f81e-6a96-40fe-86ad-db444b0de65c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "dc419bcb-4709-4748-a300-e057f1d66afd",
   "metadata": {},
   "outputs": [],
   "source": [
    "I = [[1,0,0],\n",
    "     [0,1,0],\n",
    "     [0,0,1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "8a4d59a0-f3d1-4406-8970-ff727e417dcc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "I[1][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "2988ba1c-9f3a-4bbf-91dc-3fb8dfe29608",
   "metadata": {},
   "outputs": [],
   "source": [
    "Z1 = [[0,0,0],\n",
    "      [0,0,0],\n",
    "      [0,0,0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "7637d534-5fbd-4edd-8ade-a5b027bbdccd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0, 0, 0], [0, 0, 0], [0, 0, 0]]"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Z1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "af69ab46-7017-456f-b124-19ec9fcc6a69",
   "metadata": {},
   "outputs": [],
   "source": [
    "Z2 = [[0,0,0]]*3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "ee30ad5b-2992-44a6-ab72-8684d8b3cb4b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0, 0, 0], [0, 0, 0], [0, 0, 0]]"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Z2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "b5fec725-304c-4003-a8e2-0c07e6ea8b78",
   "metadata": {},
   "outputs": [],
   "source": [
    "Z3 = [[0]*3]*3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "cb0c718a-5fba-4e8c-a530-8238066e27e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0, 0, 0], [0, 0, 0], [0, 0, 0]]"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Z3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "ba076c06-01fe-45b2-9e76-93833209d4ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Z3[1][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "c1d36fd9-a9df-448b-b8be-a69d752c18c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0, 0, 0], [0, 0, 0], [0, 0, 0]]"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Z1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "59a55db2-9fea-49d2-9fb5-de80ddf8a172",
   "metadata": {},
   "outputs": [],
   "source": [
    "Z3[1][1] = 42\n",
    "Z1[1][1] = 42"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "bd77b0b5-888d-41ee-a4b3-4de4f5c97bf4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Z1[0] is Z1[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "0a129040-0c16-4af6-ac91-e703590f7c6d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Z3[0] is Z3[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "d4b0b9c8-ef34-4497-af6b-d52c776366df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0, 42, 0], [0, 42, 0], [0, 42, 0]]\n",
      "[[0, 0, 0], [0, 42, 0], [0, 0, 0]]\n"
     ]
    }
   ],
   "source": [
    "print(Z3)\n",
    "print(Z1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "41cde147-a284-42dc-988f-8b92865a9ecf",
   "metadata": {},
   "outputs": [],
   "source": [
    "l1 = [1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "6105bb13-4ab1-475d-ba02-95132c7388f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 2, 3], [1, 2, 3], [1, 2, 3]]"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l2 = [l1, l1, l1]\n",
    "l2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "e9a345f1-d160-4ff5-9ff7-602a99c02fa9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 2, 3], [1, 2, 3], [1, 2, 3]]"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l3 = [l1[:], l1[:], l1[:]]\n",
    "l3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "67a239c5-1a32-4590-b447-96a58ae4e009",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 42, 3], [1, 42, 3], [1, 42, 3]]"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l2[0][1] = 42\n",
    "l2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "4194cd19-3343-4d98-98bb-bcd38c63dd86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 43, 3], [1, 2, 3], [1, 2, 3]]"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l3[0][1] = 43\n",
    "l3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "fbb71723-4980-4e4f-92bb-b4ebfe18b445",
   "metadata": {},
   "outputs": [],
   "source": [
    "l = [1,2,3]\n",
    "for x in l: x = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "35925010-59df-44b3-a341-4ed2016ea86c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09c09b3c-81cd-42cf-b94a-4b18c9fb41ef",
   "metadata": {},
   "source": [
    "### Conversions Between Sequence Types"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "e0675c0e-2e24-4296-922f-60096b2e5771",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'c']"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(\"abc\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "18579cd8-5dcd-42ca-ba70-d081ab8c3930",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('a', 'b', 'c')"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuple(list(\"abc\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "61aca9c3-7c8a-4f13-93fa-fdeabd7434e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[1, 2, 3]'"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str([1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "7cdc05dc-293e-4744-98ce-3eded8c32c01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'(1, 2, [True, False])'"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str((1,2,[True, False]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "d47b0069-d943-4c65-bf4f-ebd2dcfa5f82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(tuple(\"abc\")) == \"abc\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b2609a3-5389-4d89-9f0b-bb12d7ea5204",
   "metadata": {},
   "source": [
    "## Sets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "4e01d9ae-44ad-40ea-a4b1-204a0610b193",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = {1, 2, 4, 6}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "ae7234e7-12c7-4cbd-900b-854dc1697d86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{1,2,4,6} == {2,1,6,4}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "ed8c7568-2d55-4f98-a9d7-49bc7f833b8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "fbac07ce-3e9e-4077-8358-33875a0a7095",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 in s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "af463d7b-45dc-4d12-8eb3-aa61ddca5a44",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 in s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "397e803e-f8a4-4baa-bea8-59b279e7aaa1",
   "metadata": {},
   "source": [
    "| Operation                  | Time Complexity      | Notes                                               |\n",
    "|----------------------------|----------------------|-----------------------------------------------------|\n",
    "| Containment (`x in set`)   | O(1) average         | Hash-based lookup                                   |\n",
    "| Insert (`set.add(x)`)      | O(1) average         | May degrade to O(n) in worst case (hash collisions) |\n",
    "| Delete (`set.remove(x)`)   | O(1) average         | Raises `KeyError` if `x` not present                |\n",
    "| Set union (`s \\| t`)       | O(len(s) + len(t))   |                                                     |\n",
    "| Set intersection (`s & t`) | O(min(len(s),len(t)))|                                                     |\n",
    "| Set difference (`s - t`)   | O(len(s))            |                                                     |\n",
    "\n",
    "\n",
    "Sets in Python are implemented with hashtables. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "97de7c27-589c-47f2-b61f-feda6dda7afa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "e = set() # the empty set \n",
    "e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "a1d1b9de-a46a-4037-89ed-eb0e6a3afb14",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 16, 4, 9, 'abc'}"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = {0, 1, 4, 9, 16, 'abc'}\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "570086f6-5041-4f02-91c6-969d85970756",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 16, 25, 4, 9, 'abc'}"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.add(25)\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "cde6a660-3ec5-4b48-8191-7677fd7fa9ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 25, 4, 9, 'abc'}"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.remove(16)\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "58295edc-adc2-43d9-a10c-97755875e9a0",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "42",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[135], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremove\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m42\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mKeyError\u001b[0m: 42"
     ]
    }
   ],
   "source": [
    "t.remove(42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "ae322562-ee87-461b-a9bd-757745c9605e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "c05f2773-fe54-4379-93d6-7e839725ea24",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 4, 'abc', 9, 25}\n"
     ]
    }
   ],
   "source": [
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "8a997c62-cb38-4a35-8ab1-60312cef344c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 4, 6}\n"
     ]
    }
   ],
   "source": [
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "070988c6-60c8-460a-8f44-ae9c0e56d473",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 4}"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t & s # set intersection"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cad325c0-47de-4075-a72e-6ecc38ad5df7",
   "metadata": {},
   "source": [
    "Complexity is O(min(len(t), len(s)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "e0112962-2695-4a62-9f09-5b3f115d7dfa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 2, 25, 4, 6, 9, 'abc'}"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t | s # set union"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43104619-0956-4971-8056-b25db032f704",
   "metadata": {},
   "source": [
    "Complexity is O(len(t) + len(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "id": "849945df-56eb-4a8e-90b1-82c0340e3053",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 25, 9, 'abc'}"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t - s # set difference"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25a98396-b714-4d54-8a85-ee8ea671a853",
   "metadata": {},
   "source": [
    "Complexity is O(len(t))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5a08ded-c6d7-418b-9470-babd4489d9db",
   "metadata": {},
   "source": [
    "`in` can be used for other types of collections:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "id": "59f41241-71b7-48a1-ad1d-d5adfd44c38e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 in [1,2,4,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "92cdd32c-011e-4671-a530-445728ef1920",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'d' in \"hello world!\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e62dac9c-f35e-4961-8467-f81ee0de0184",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary>But...</summary>\n",
    "complexity is O(n)\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "id": "3f4a3dd6-f2bc-473e-83ee-65ca71ecbbcd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t <= s # is subset?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "9fb096e3-18b4-45dc-adf6-89a28d8c0d63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{1,2,3,4} < {1,2,3,4} # strict subset "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "3f8b29ec-7817-4797-a817-52997defa659",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "5\n",
      "100\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for x in {0,4,100,5}: print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "a249f412-ff94-4715-bb2e-90cd8456bb08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 4, 5, 100]"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted({0,4,100,5})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6a52738-92df-4c70-add8-30daaa4f2d5d",
   "metadata": {},
   "source": [
    "Can be used for other collection types:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "id": "5b109075-1d6a-4ebe-ab19-e20300bbc868",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 5]"
      ]
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted((1,5,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "id": "d45afc55-7db8-4485-b225-84147dbaf9f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 4, 5, 100]"
      ]
     },
     "execution_count": 149,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted([0,4,100,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "2a54266f-3083-4131-9786-ceac57b19c3f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "4\n",
      "5\n",
      "100\n"
     ]
    }
   ],
   "source": [
    "for x in sorted({0,4,100,5}): print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "id": "9e764889-766d-43c1-8f0e-498fd07611fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = {\"cats\", \"dogs\", 42, True}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "id": "83dab64b-b45c-4b01-88c4-723110e28b4f",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'<' not supported between instances of 'int' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[152], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43msorted\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43ms\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: '<' not supported between instances of 'int' and 'str'"
     ]
    }
   ],
   "source": [
    "sorted(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "id": "6574d940-a67d-4d13-9a2a-bb57f04c8155",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(1, 2), 42, True, 'cats', 'dogs'}"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = {\"cats\", \"dogs\", 42, True, (1,2)}\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "id": "b4b1c8b7-29ca-4a30-93de-7f0b9a4491b8",
   "metadata": {},
   "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[154], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m l \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m s \u001b[38;5;241m=\u001b[39m {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcats\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdogs\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m42\u001b[39m, \u001b[38;5;28;01mTrue\u001b[39;00m, l}\n",
      "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
     ]
    }
   ],
   "source": [
    "l = [1,2]\n",
    "s = {\"cats\", \"dogs\", 42, True, l}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "id": "8b454126-42ec-4dee-bff1-5224829d16b9",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unhashable type: 'set'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[155], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m s \u001b[38;5;241m=\u001b[39m {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcats\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdogs\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m42\u001b[39m, \u001b[38;5;28;01mTrue\u001b[39;00m, {\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m}}\n",
      "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'set'"
     ]
    }
   ],
   "source": [
    "s = {\"cats\", \"dogs\", 42, True, {1,2}}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a85de1ee-cf70-49e5-9095-23bee8fcf36b",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary>Why..?</summary>\n",
    "Mutable objects cannot be added to a set!\n",
    "    <details>\n",
    "    <summary>Why..?</summary>\n",
    "    Because their hash value may change!\n",
    "    </details>\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "id": "10f2b8f5-2537-42d2-b6a7-19474f0dd32c",
   "metadata": {},
   "outputs": [],
   "source": [
    "f = frozenset({1,2,3,4}) # an immutable set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "id": "4ad0eadb-b0eb-4282-bc8e-c3cc161dc813",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'frozenset' object has no attribute 'add'",
     "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[43mf\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43madd\u001b[49m(\u001b[38;5;241m5\u001b[39m)\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'frozenset' object has no attribute 'add'"
     ]
    }
   ],
   "source": [
    "f.add(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "927d631e-2d6b-470b-b227-abc1146e8768",
   "metadata": {},
   "source": [
    "`frozenset` is immutable and can be added to a set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "id": "882808f3-32e6-4dbb-b320-61174d79deec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{42, True, 'cats', 'dogs', frozenset({1, 2})}"
      ]
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = {\"cats\", \"dogs\", 42, True, frozenset({1,2})}\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b10537e8-567c-43a2-94a9-b96631e677b7",
   "metadata": {},
   "source": [
    "## Dictionaries"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b7c33df-a504-41bb-b0ca-04f201bc82e2",
   "metadata": {},
   "source": [
    "Dictionaries are key-value stores."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "id": "e45f8dea-e3d9-4e58-83f0-d83ac7a9bb51",
   "metadata": {},
   "outputs": [],
   "source": [
    "d = {} # the empty dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "id": "59cf87f2-abb1-4356-884c-c06af4d4774c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: 42, 2: 17}"
      ]
     },
     "execution_count": 160,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d[1] = 42\n",
    "d[2] = 17\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "id": "cf34a477-45d2-4c8f-9c47-6d9dc92cb8ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: 42, 2: 17}"
      ]
     },
     "execution_count": 161,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {1: 42, 2: 17}\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "id": "0c133fe9-ffe7-4fab-b55d-a8d151450ef9",
   "metadata": {},
   "outputs": [],
   "source": [
    "d[3] = \"cats\"\n",
    "d[\"dog\"] = \"no cats\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "id": "215bf5ca-de16-46ac-8e78-d6d9ef059988",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: 42, 2: 17, 3: 'cats', 'dog': 'no cats'}"
      ]
     },
     "execution_count": 163,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59f7c1f5-658a-498a-a310-275907a52cd2",
   "metadata": {},
   "source": [
    "Key and values can have arbitrary types with the restriction that indices must be of hashable types."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "id": "d060908e-8d51-4d1e-ac70-a413c4225bae",
   "metadata": {},
   "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[164], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43md\u001b[49m\u001b[43m[\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mlist [1, 2]\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;66;03m# indices must be hashable\u001b[39;00m\n",
      "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
     ]
    }
   ],
   "source": [
    "d[[1,2]] = \"list [1, 2]\" # indices must be hashable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "id": "2eaa7598-d71a-4789-9ead-10d9cd09d69e",
   "metadata": {},
   "outputs": [],
   "source": [
    "d[\"list\"] = [1,2] # values need not be hashable\n",
    "d\n",
    "d[(1,2)] = \"tuple\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "id": "c848e40a-b7bd-4a25-9554-5710bdd9a8e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 maps to 42\n",
      "2 maps to 17\n",
      "3 maps to cats\n",
      "dog maps to no cats\n",
      "list maps to [1, 2]\n",
      "(1, 2) maps to tuple\n"
     ]
    }
   ],
   "source": [
    "for key in d: print(key, \"maps to\", d[key])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "id": "3ce05030-c134-463b-a7c2-314cc9f7970e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 'dog', 'list', (1, 2)]"
      ]
     },
     "execution_count": 167,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(d.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "id": "d4cc63e5-59ca-4e7a-84a9-01c3e7b4c47e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[42, 17, 'cats', 'no cats', [1, 2], 'tuple']"
      ]
     },
     "execution_count": 168,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(d.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "id": "f25230c9-6d82-4cee-bc6d-8002b2e68ea1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 42),\n",
       " (2, 17),\n",
       " (3, 'cats'),\n",
       " ('dog', 'no cats'),\n",
       " ('list', [1, 2]),\n",
       " ((1, 2), 'tuple')]"
      ]
     },
     "execution_count": 169,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(d.items())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "id": "14464aac-685d-4ddc-ab24-893a5b2eb2eb",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "incomplete input (1691096933.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  Cell \u001b[0;32mIn[170], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m    for (key,value) in d.items(): print(key, \"maps to\", value)(\u001b[0m\n\u001b[0m                                                               ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m incomplete input\n"
     ]
    }
   ],
   "source": [
    "for (key,value) in d.items(): print(key, \"maps to\", value)("
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "id": "111a420a-b2a7-4437-ab4a-4ac11f9382ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "(a,b,c) = (1,2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "id": "9fddb5d0-0d57-4a7c-8397-0f200b196f74",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: 42, 2: 17, 3: 'cats', 'dog': 'no cats', 'list': [1, 2], (1, 2): 'tuple'}"
      ]
     },
     "execution_count": 172,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "id": "2a6faeb4-328e-4b4c-9755-2219b99bee3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 173,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 in d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0acd90b4-6be3-4b5f-bc2d-d614226bf44a",
   "metadata": {},
   "source": [
    "The avarage complexity of dictionary containment is O(1) and the worst O(n) ([reference](https://wiki.python.org/moin/TimeComplexity))."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "id": "9b81686d-9d27-413a-bc02-29aace1fef0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 174,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "42 in d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "id": "3fd68b2c-0ff1-4775-a338-e19f3c5db3ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 175,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "42 in d.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "id": "8ede420a-0402-4e9e-92b2-50b3c8599179",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 176,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "id": "19f35889-9974-4e86-ac83-fd88b116ed8b",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "4",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[177], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43md\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m4\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[0;31mKeyError\u001b[0m: 4"
     ]
    }
   ],
   "source": [
    "d[4]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c2595d8-0ee9-4f67-a7a2-b9bfeca65fc5",
   "metadata": {},
   "source": [
    "## Exception Handling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "id": "5cc57647-19cb-42c5-8579-aa3b04f13d90",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: 42, 2: 17, 3: 'cats', 'dog': 'no cats', 'list': [1, 2], (1, 2): 'tuple'}"
      ]
     },
     "execution_count": 178,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "id": "82b37f60-9e5a-41c8-b476-ed94ec5aacb7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "42"
      ]
     },
     "execution_count": 179,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "id": "a584643e-4925-4bfe-87bf-0d7ef3574f3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "42"
      ]
     },
     "execution_count": 180,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d[True]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "id": "0a5a0d15-cfb7-4c22-b40a-d9041af1238d",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'hello'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[181], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43md\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mhello\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[0;31mKeyError\u001b[0m: 'hello'"
     ]
    }
   ],
   "source": [
    "d['hello']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "id": "12f024ee-cc2f-4a2d-b747-c01601cdb320",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "no cats\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    print(d[\"dog\"])\n",
    "except KeyError:\n",
    "    print(\"Not found!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "id": "28a5035f-9986-41b8-a2ed-10ab2091c629",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Not found!\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    print(d[42])\n",
    "except KeyError:\n",
    "    print(\"Not found!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "id": "d506a9cb-d67d-4b50-abe9-55bac41ac679",
   "metadata": {},
   "outputs": [],
   "source": [
    "def find(k):\n",
    "    print (\"Found: \", d[k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "id": "4172373b-6b53-4164-8ee2-0ff303ce6e03",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "42",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[185], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mfind\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m42\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[0;32mIn[184], line 2\u001b[0m, in \u001b[0;36mfind\u001b[0;34m(k)\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mfind\u001b[39m(k):\n\u001b[0;32m----> 2\u001b[0m     \u001b[38;5;28mprint\u001b[39m (\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFound: \u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[43md\u001b[49m\u001b[43m[\u001b[49m\u001b[43mk\u001b[49m\u001b[43m]\u001b[49m)\n",
      "\u001b[0;31mKeyError\u001b[0m: 42"
     ]
    }
   ],
   "source": [
    "find(42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "id": "8e6f56f3-76b2-468e-93fd-5dbaed4e4a06",
   "metadata": {},
   "outputs": [],
   "source": [
    "def find(k):\n",
    "    if k in d:\n",
    "        print (\"Found: \", d[k])\n",
    "    else: \n",
    "        print(\"Not found\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "id": "e9b5ba70-d25f-4985-8274-1d61b8a975c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Not found\n"
     ]
    }
   ],
   "source": [
    "find(42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "id": "5626d4d4-1912-4944-8bf5-c7c91261df8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def find(k):\n",
    "    try:\n",
    "        print(\"Found: \", d[k])\n",
    "    except q: \n",
    "        print(\"Not found\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "id": "17ce057a-2e88-4ef6-9640-c98a9e14937f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found:  42\n"
     ]
    }
   ],
   "source": [
    "find(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 190,
   "id": "c13faa84-229e-47a6-8cc3-6dabf219c50a",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'q' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[188], line 3\u001b[0m, in \u001b[0;36mfind\u001b[0;34m(k)\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m----> 3\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFound: \u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[43md\u001b[49m\u001b[43m[\u001b[49m\u001b[43mk\u001b[49m\u001b[43m]\u001b[49m)\n\u001b[1;32m      4\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m q: \n",
      "\u001b[0;31mKeyError\u001b[0m: 4",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[190], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mfind\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m4\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[0;32mIn[188], line 4\u001b[0m, in \u001b[0;36mfind\u001b[0;34m(k)\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m      3\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFound: \u001b[39m\u001b[38;5;124m\"\u001b[39m, d[k])\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[43mq\u001b[49m: \n\u001b[1;32m      5\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNot found\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[0;31mNameError\u001b[0m: name 'q' is not defined"
     ]
    }
   ],
   "source": [
    "find(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "id": "5f9ce12d-1704-43d7-99d4-9e5fc698acde",
   "metadata": {},
   "outputs": [
    {
     "ename": "ZeroDivisionError",
     "evalue": "division by zero",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[191], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m k \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m10\u001b[39m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;241;43m10\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\n",
      "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
     ]
    }
   ],
   "source": [
    "k = 10\n",
    "10/0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "id": "e4a769bd-a0d9-44ff-905f-e9d63738bbff",
   "metadata": {},
   "outputs": [],
   "source": [
    "def find(k):\n",
    "    try:\n",
    "        print(\"Found: \", d[k/0])\n",
    "    except KeyError: \n",
    "        print(\"Not found\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 193,
   "id": "8c55bc7c-1f83-4a37-9be4-212498ae9285",
   "metadata": {},
   "outputs": [
    {
     "ename": "ZeroDivisionError",
     "evalue": "division by zero",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[193], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mfind\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[0;32mIn[192], line 3\u001b[0m, in \u001b[0;36mfind\u001b[0;34m(k)\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mfind\u001b[39m(k):\n\u001b[1;32m      2\u001b[0m     \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m----> 3\u001b[0m         \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFound: \u001b[39m\u001b[38;5;124m\"\u001b[39m, d[\u001b[43mk\u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m])\n\u001b[1;32m      4\u001b[0m     \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m: \n\u001b[1;32m      5\u001b[0m         \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNot found\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
     ]
    }
   ],
   "source": [
    "find(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 194,
   "id": "66057ec5-b3a3-4dd2-9c22-6fc45530b13d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def find(k):\n",
    "    try:\n",
    "        print(\"Found: \", d[k/0])\n",
    "    except:\n",
    "        print(\"Not found\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 195,
   "id": "c7ecf466-9a7e-4271-b641-b887772670d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Not found\n"
     ]
    }
   ],
   "source": [
    "find(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 197,
   "id": "26422272-a0f4-4bd5-9e0d-fb40687873e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def find(k):\n",
    "    try:\n",
    "        print(\"Found: \", d[k/0])\n",
    "    except Exception as e:\n",
    "        print(\"Error happened: \", e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "id": "ff9ee933-4dc8-4930-8849-0c3a894ac1ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error happened:  division by zero\n"
     ]
    }
   ],
   "source": [
    "find(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbdd805a-8cb5-4d96-989e-44ac28e77bd7",
   "metadata": {},
   "source": [
    "## Comprehensions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5726546c-30fd-45ed-ad15-c1a7d26eebbb",
   "metadata": {},
   "source": [
    "Recall { x \\in Z | 0 <= x < 5 } from math notation. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "id": "d8820f47-02c1-4a42-8104-987a229b00ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 2, 3, 4}"
      ]
     },
     "execution_count": 199,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{x for x in range(0,5)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "id": "451c6a93-31d5-4c40-9527-f4e3437a9740",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 4, 9, 16, 25, 36, 49, 64, 81}"
      ]
     },
     "execution_count": 200,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{i * i for i in range(10)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 201,
   "id": "678cb754-3daa-4720-a3a8-b6bee65e2493",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]"
      ]
     },
     "execution_count": 201,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[i*i for i in range(10)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 202,
   "id": "e49a75b1-0d87-402e-b8d7-57e99815aaa8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0, 1, 4, 9, 16, 25, 36, 49, 64, 81)"
      ]
     },
     "execution_count": 202,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuple(i*i for i in range(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 203,
   "id": "0ec55865-eeb1-43ec-9f6c-71982d869174",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}"
      ]
     },
     "execution_count": 203,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{i: i*i for i in range(10)}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a337e9d1-46ad-46df-bcae-e310ad8c614b",
   "metadata": {},
   "source": [
    "Comprehension with filtering:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 204,
   "id": "3a2fbe40-fea2-46e0-952f-56b6967b0e9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 4, 16, 36, 64}"
      ]
     },
     "execution_count": 204,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{i*i for i in range(10) if i % 2 == 0}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 205,
   "id": "ccb72a6b-6397-4369-980f-6acb457eda44",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 0, 0]"
      ]
     },
     "execution_count": 205,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[0 for i in range(3)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 206,
   "id": "d44fa509-2963-4e0e-8a48-70f127bf2ad1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 0, 0]"
      ]
     },
     "execution_count": 206,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[0 for _ in range(3)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 207,
   "id": "46c205cc-89a9-4e06-a0e6-4ea3f2b68986",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0, 0, 0], [0, 0, 0], [0, 0, 0]]"
      ]
     },
     "execution_count": 207,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = [[0 for _ in range(3)] for _ in range(3)]\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "id": "c8c336a9-064a-4e2d-8c61-538fc6c80a4c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0, 0, 0], [0, 42, 0], [0, 0, 0]]"
      ]
     },
     "execution_count": 208,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[1][1] = 42\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85720ba1-9f74-4ef5-afbf-de4405065bb6",
   "metadata": {},
   "source": [
    "## Generator objects"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 209,
   "id": "bfbfa3e8-280e-4ad6-846d-f1d9b8e5c506",
   "metadata": {},
   "outputs": [],
   "source": [
    "g = (i * i for i in range(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 210,
   "id": "ff530199-d738-426a-b482-e25921e6989b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "generator"
      ]
     },
     "execution_count": 210,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 211,
   "id": "f5fa7ee8-0f94-49de-946f-c2fd65d59853",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 211,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 212,
   "id": "afc84f7f-faed-474b-8472-3d55f400bd8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 212,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 213,
   "id": "e02755ff-ae33-4ad1-aa21-dfe3189c4a03",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "9\n",
      "16\n",
      "25\n",
      "36\n",
      "49\n",
      "64\n",
      "81\n"
     ]
    }
   ],
   "source": [
    "for x in g: print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 214,
   "id": "556fb7dd-95dd-48d1-9e5d-c22334a3b3e8",
   "metadata": {},
   "outputs": [
    {
     "ename": "StopIteration",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mStopIteration\u001b[0m                             Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[214], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mg\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mStopIteration\u001b[0m: "
     ]
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2874ea02-b166-47a0-99b5-62a370dfa21c",
   "metadata": {},
   "source": [
    "Generator items are consumed..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 215,
   "id": "491cf41e-16ac-4ab8-9123-263d9d63f8e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "g = (i * i for i in range(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 216,
   "id": "0fb60369-b56a-48d7-9b16-ce8f13a725d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 216,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 217,
   "id": "47087430-f564-4a69-8407-f51a4e09b084",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 217,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "279f9bcd-4837-44cb-acb9-e31229ccbee0",
   "metadata": {},
   "source": [
    "- A generator is special function that returns an stream whose items are consumed one at a time\n",
    "- The contents of a genertor are not expanded in memory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 218,
   "id": "23d0ba60-e425-4cdc-be90-fab50403715a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 218,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 219,
   "id": "7312f1b1-db9e-437c-9d97-bf9530c23f50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 16, 25, 36, 49, 64, 81]"
      ]
     },
     "execution_count": 219,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(g)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8f42fe2-29d5-43a0-9b06-90062ab7bbb0",
   "metadata": {},
   "source": [
    "### A Simple Generator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 220,
   "id": "5b5ce5b8-d309-430d-b8c5-951361821e79",
   "metadata": {},
   "outputs": [],
   "source": [
    "def simple_gen():\n",
    "    yield 1\n",
    "    yield 17\n",
    "    yield 42"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "id": "0318daa8-48aa-4cc6-8b8d-a738e0e796a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "generator"
      ]
     },
     "execution_count": 221,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = simple_gen()\n",
    "type(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 222,
   "id": "65bddcce-aa9d-497f-bc17-40bdd27ab1b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 222,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 223,
   "id": "bd327208-e220-4dbc-b4b5-793a0a65c37d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 223,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 224,
   "id": "6120a258-1fd6-4507-96f1-81137830dcf1",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "42"
      ]
     },
     "execution_count": 224,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 225,
   "id": "533400ab-4320-4ba7-84ff-cb759baf73ba",
   "metadata": {},
   "outputs": [
    {
     "ename": "StopIteration",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mStopIteration\u001b[0m                             Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[225], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mg\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mStopIteration\u001b[0m: "
     ]
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 226,
   "id": "ea71231b-77e2-43b4-95cb-04d9b988d81a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "17\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "g1 = simple_gen()\n",
    "g2 = simple_gen()\n",
    "print(next(g1))\n",
    "print(next(g1))\n",
    "print(next(g2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 227,
   "id": "ebf99ace-92c8-4505-9694-a9e93d9d49bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "17\n",
      "42\n"
     ]
    }
   ],
   "source": [
    "for i in simple_gen(): print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 228,
   "id": "5538a6c5-bbc4-40cb-bab0-44581e974bfd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 17, 42]"
      ]
     },
     "execution_count": 228,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x for x in simple_gen()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 229,
   "id": "cea3275c-9bdc-440d-a35c-64ef66da4b95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 17, 42]"
      ]
     },
     "execution_count": 229,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(simple_gen())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40f39a06-c80b-4d8d-9e8b-69b50aab4f52",
   "metadata": {},
   "source": [
    "Quiz: write a range generator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 230,
   "id": "ae044195-6a36-421f-b92d-d4eb7f449d12",
   "metadata": {},
   "outputs": [],
   "source": [
    "def rng(n1, n2):\n",
    "    while n1 < n2: \n",
    "        yield n1\n",
    "        n1 += 1    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 231,
   "id": "444a52a0-eb50-400a-943e-16473d33d788",
   "metadata": {},
   "outputs": [],
   "source": [
    "g = range(1,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 232,
   "id": "a3393361-0ff7-428b-9aeb-d43a91dd69fe",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'range' object is not an iterator",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[232], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mg\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: 'range' object is not an iterator"
     ]
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 233,
   "id": "ecc2579a-dc4a-44f8-af62-99d6d345f369",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'range' object is not an iterator",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[233], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mg\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: 'range' object is not an iterator"
     ]
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 234,
   "id": "129e413a-7de2-4f95-b057-6c349c6cc464",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 234,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 235,
   "id": "2399bdc1-0de7-41c8-8492-419deedf6a16",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'range' object is not an iterator",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[235], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mg\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: 'range' object is not an iterator"
     ]
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff521cf2-4b9e-4af7-92f9-8d8773d7f3ab",
   "metadata": {},
   "source": [
    "### A Fibonacci Generator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 236,
   "id": "b24be3cb-7b3a-48ee-b3ac-fd96838421d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib_gen(n):\n",
    "    yield 0\n",
    "    yield 1\n",
    "    a,b = 0,1\n",
    "    for i in range(n-1):\n",
    "        a, b = b, a + b\n",
    "        yield b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 237,
   "id": "d2f8b89d-96d7-4aa0-b2ee-71d07d2a4d2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "g = fib_gen(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 238,
   "id": "f30c2da0-5dca-4650-a2a9-757cf0664c60",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 238,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 239,
   "id": "7a16a960-03b4-408c-ae35-be14931a0522",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 239,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 240,
   "id": "8c388173-b3ad-43b6-9193-c6c9a791a63f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 240,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 241,
   "id": "179b5079-ea22-47df-bd54-440f6d08a3e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 5, 8, 13, 21, 34, 55]"
      ]
     },
     "execution_count": 241,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 242,
   "id": "ee9e3d53-1d64-46d1-acd1-995b854a966d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0,\n",
       " 1,\n",
       " 1,\n",
       " 2,\n",
       " 3,\n",
       " 5,\n",
       " 8,\n",
       " 13,\n",
       " 21,\n",
       " 34,\n",
       " 55,\n",
       " 89,\n",
       " 144,\n",
       " 233,\n",
       " 377,\n",
       " 610,\n",
       " 987,\n",
       " 1597,\n",
       " 2584,\n",
       " 4181,\n",
       " 6765,\n",
       " 10946,\n",
       " 17711,\n",
       " 28657,\n",
       " 46368,\n",
       " 75025,\n",
       " 121393,\n",
       " 196418,\n",
       " 317811,\n",
       " 514229,\n",
       " 832040,\n",
       " 1346269,\n",
       " 2178309,\n",
       " 3524578,\n",
       " 5702887,\n",
       " 9227465,\n",
       " 14930352,\n",
       " 24157817,\n",
       " 39088169,\n",
       " 63245986,\n",
       " 102334155,\n",
       " 165580141,\n",
       " 267914296,\n",
       " 433494437,\n",
       " 701408733,\n",
       " 1134903170,\n",
       " 1836311903,\n",
       " 2971215073,\n",
       " 4807526976,\n",
       " 7778742049,\n",
       " 12586269025]"
      ]
     },
     "execution_count": 242,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(fib_gen(50))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 243,
   "id": "d723a23a-90a4-4608-a796-af350de74129",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0,\n",
       " 1,\n",
       " 1,\n",
       " 2,\n",
       " 3,\n",
       " 5,\n",
       " 8,\n",
       " 13,\n",
       " 21,\n",
       " 34,\n",
       " 55,\n",
       " 89,\n",
       " 144,\n",
       " 233,\n",
       " 377,\n",
       " 610,\n",
       " 987,\n",
       " 1597,\n",
       " 2584,\n",
       " 4181,\n",
       " 6765,\n",
       " 10946,\n",
       " 17711,\n",
       " 28657,\n",
       " 46368,\n",
       " 75025,\n",
       " 121393,\n",
       " 196418,\n",
       " 317811,\n",
       " 514229,\n",
       " 832040,\n",
       " 1346269,\n",
       " 2178309,\n",
       " 3524578,\n",
       " 5702887,\n",
       " 9227465,\n",
       " 14930352,\n",
       " 24157817,\n",
       " 39088169,\n",
       " 63245986,\n",
       " 102334155,\n",
       " 165580141,\n",
       " 267914296,\n",
       " 433494437,\n",
       " 701408733,\n",
       " 1134903170,\n",
       " 1836311903,\n",
       " 2971215073,\n",
       " 4807526976,\n",
       " 7778742049,\n",
       " 12586269025]"
      ]
     },
     "execution_count": 243,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x for x in fib_gen(50)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 244,
   "id": "38a99f23-92d1-4b61-a031-7f5f4376b63b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(n):\n",
    "    \"\"\" This is the fibonacci function! \"\"\"\n",
    "    if n == 0: return 0\n",
    "    a, b = 0, 1\n",
    "    for i in range(n-1):\n",
    "        a, b = b, a + b\n",
    "    return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 245,
   "id": "fa336590-f2b1-40d1-a9de-7140c3befa30",
   "metadata": {},
   "outputs": [],
   "source": [
    "g = (fib(n) for n in range(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 246,
   "id": "419bf590-2739-4840-8998-8d4e88283c56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n",
      "8\n",
      "13\n",
      "21\n",
      "34\n"
     ]
    }
   ],
   "source": [
    "for i in g: print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38433c99-d7e0-4fc6-8fcd-57df3a23f7ca",
   "metadata": {},
   "source": [
    "What is the problem..?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 247,
   "id": "7c1381ee-b791-4b8b-9dc6-4028570d95d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(n):\n",
    "    print(0)\n",
    "    a, b = 0, 1\n",
    "    for i in range(n-1):\n",
    "        print(b)\n",
    "        a,b = b,a + b    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 248,
   "id": "f6059014-f44e-4931-9a69-15e62726cf8c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n",
      "8\n",
      "13\n",
      "21\n",
      "34\n"
     ]
    }
   ],
   "source": [
    "fib(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a0f5953-7e88-43dd-a02c-5e73d39b1ec5",
   "metadata": {},
   "source": [
    "Can we build a fibonacci generator?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 249,
   "id": "639bf3cb-b4d7-48c3-b7f0-be109971a8a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def gen_fib(n):\n",
    "    a, b = 0, 1\n",
    "    yield(a)\n",
    "    for i in range(n-1):\n",
    "        yield(b)\n",
    "        a, b = b, a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 250,
   "id": "c316870a-bb99-4f26-af7c-c646ebe5c5ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]"
      ]
     },
     "execution_count": 250,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(gen_fib(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 251,
   "id": "6db0b233-05e1-49d3-88ce-bfd18dac71db",
   "metadata": {},
   "outputs": [],
   "source": [
    "g = gen_fib(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 252,
   "id": "f2e2f108-ea76-4c19-9cfd-1b4bf3073eca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "generator"
      ]
     },
     "execution_count": 252,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 253,
   "id": "61f8d65c-8f7a-4d99-aa37-3a93c966148d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 253,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 254,
   "id": "5011abf0-ab98-453b-a0e8-13173748b7e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 254,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 255,
   "id": "a48c452c-2957-4940-8693-97a162089f44",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 5, 8, 13, 21, 34]"
      ]
     },
     "execution_count": 255,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 256,
   "id": "30c78257-b775-4bbd-b50b-7b534414d52a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<generator object gen_fib at 0x119ecab20>\n",
      "<generator object gen_fib at 0x119ecaa40>\n"
     ]
    }
   ],
   "source": [
    "g1 = gen_fib(10)\n",
    "print(g)\n",
    "print(g1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 257,
   "id": "f6b1a988-84a5-43bd-a055-345db17d121c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n",
      "8\n",
      "13\n",
      "21\n",
      "34\n"
     ]
    }
   ],
   "source": [
    "for x in g1: print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 258,
   "id": "37e0d318-5e56-45df-94a6-5517a75a052d",
   "metadata": {},
   "outputs": [],
   "source": [
    "for x in g: print(x) # all elements are consumed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 259,
   "id": "69f34733-d537-438d-89b4-548d504a9289",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib():\n",
    "    a, b = 0, 1\n",
    "    yield(a)\n",
    "    while True:\n",
    "        yield(b)\n",
    "        a, b = b, a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 260,
   "id": "d55d9bea-3d1a-49a0-a8ad-caa9eb9e2c61",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n",
      "8\n",
      "13\n",
      "21\n",
      "34\n"
     ]
    }
   ],
   "source": [
    "g = fib()\n",
    "for _ in range(10): print(next(g))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 261,
   "id": "c4ee7b52-8702-4b4a-800b-d7ffc3a8c233",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "55\n",
      "89\n",
      "144\n",
      "233\n",
      "377\n",
      "610\n",
      "987\n",
      "1597\n",
      "2584\n",
      "4181\n"
     ]
    }
   ],
   "source": [
    "for _ in range(10): print(next(g))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 262,
   "id": "a9466942-bc55-4434-b032-2ede64f5cd76",
   "metadata": {},
   "outputs": [],
   "source": [
    "# don't try this at home...\n",
    "# for x in g: print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 263,
   "id": "d722745d-cd72-483f-b0f6-0d797f38e9f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(0, 'a'), (1, 'b'), (2, 'c')]"
      ]
     },
     "execution_count": 263,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(enumerate(['a','b','c']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 264,
   "id": "9b10623b-af9b-4474-b264-93613cd7eee6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n",
      "8\n",
      "13\n",
      "21\n",
      "34\n",
      "55\n"
     ]
    }
   ],
   "source": [
    "g = fib()\n",
    "for (i, x) in enumerate(g):\n",
    "    print(x)\n",
    "    if i >= 10: break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 265,
   "id": "cf84c328-1b1c-4a93-9e94-445e29b0975e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "89\n",
      "144\n",
      "233\n",
      "377\n",
      "610\n",
      "987\n"
     ]
    }
   ],
   "source": [
    "for i, x in enumerate(g):\n",
    "    print(x)\n",
    "    if i >= 5: break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 266,
   "id": "c75019fa-fac7-4dd6-b916-f15367193a03",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1597"
      ]
     },
     "execution_count": 266,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 267,
   "id": "003fc7ce-f3f0-48fd-8ac5-b4b2b8d40149",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2584"
      ]
     },
     "execution_count": 267,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(g)"
   ]
  }
 ],
 "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.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
