{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "composite-saturday",
   "metadata": {},
   "source": [
    "# Lecture 2: Quick(ish) Introduction to Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "15396b40-a2f3-48a3-86c9-a16794f93e87",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.12.1\n"
     ]
    }
   ],
   "source": [
    "import platform\n",
    "print(platform.python_version())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "velvet-helping",
   "metadata": {},
   "source": [
    "The goal of this lecture is to give you some of the basics. It's not possible for us to cover **everything** you'll need to know ahead of time. As graduate students, you are expected to be able to do some research and self-teaching on your own to build your coding skills, and of course you can *always* come ask me for help!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "hourly-patch",
   "metadata": {},
   "source": [
    "A nice reference for a lot of the computational skills we'll be covering (coding, unix command line, git) is the \"Software Carpentry\" set of lessons: https://software-carpentry.org/lessons/. You should seriously considering checking their tutorials for extra practice and more in-depth lessons!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f162de59",
   "metadata": {},
   "source": [
    "This document is a \"Jupyter Notebook\". It's kind of like interactive mode, but also lets you intersperse text, html, etc, among it. (It's free and easy to set up.)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "tough-fleece",
   "metadata": {},
   "source": [
    "<hr style=\"border:1px solid black\"> </hr>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "entertaining-wallace",
   "metadata": {},
   "source": [
    "Python is a **whitespace-based language**. In C, C++, Java, and many other languages, you use braces to group code:\n",
    "```java\n",
    "if (x == 1) {\n",
    "    do_something();\n",
    "}\n",
    "```\n",
    "and the spacing is just for readability. For example, the following code does the same thing:\n",
    "```java\n",
    "if (x == 1) { do_something();\n",
    "         }\n",
    "```\n",
    "\n",
    "In Python you use indenting, and colons (`:`) to have the same effect. You also do not use semicolons (`;`) to end commands.\n",
    "```py\n",
    "if x == 1:\n",
    "    do_something()\n",
    "    do_something_else()\n",
    "third_command()\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "narrative-turtle",
   "metadata": {},
   "source": [
    "<hr style=\"border:1px solid black\"> </hr>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "desperate-stream",
   "metadata": {},
   "source": [
    "You have to be *really* careful to be consistent by either\n",
    "- always using tabs, or\n",
    "- always using spaces (and the same number)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "noted-staff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "x = 2\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "clear-spank",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    }
   ],
   "source": [
    "if x == 1:\n",
    "    print(\"hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "synthetic-filter",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    }
   ],
   "source": [
    "if x == 1:\n",
    "            print(\"hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "broke-responsibility",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "unexpected indent (151846787.py, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  Cell \u001b[0;32mIn[8], line 3\u001b[0;36m\u001b[0m\n\u001b[0;31m    print(\"world\")\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unexpected indent\n"
     ]
    }
   ],
   "source": [
    "if x == 1:\n",
    "    print(\"hello\")\n",
    "     print(\"world\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0bd0e573-cc7b-442f-b02d-8cabe218b5db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "B\n"
     ]
    }
   ],
   "source": [
    "if x == 1:\n",
    "    print(\"A\")\n",
    "if x > 0:\n",
    "           print(\"B\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "sixth-bearing",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "world\n"
     ]
    }
   ],
   "source": [
    "if x == 1:\n",
    "    print(\"hello\")\n",
    "    print(\"world\") # if you use a tab, Jupyter will fix it for you automatically! Your code editor might not."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "union-nursery",
   "metadata": {},
   "source": [
    "<hr style=\"border:1px solid black\"> </hr>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "catholic-weapon",
   "metadata": {},
   "source": [
    "Python uses `if`, `for`, and `while` statements like many other languages."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "legal-microphone",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "5\n",
      "7\n",
      "9\n",
      "11\n"
     ]
    }
   ],
   "source": [
    "x = 1\n",
    "while x < 10:\n",
    "    x = x + 2\n",
    "\n",
    "\n",
    "    \n",
    "    print(x)\n",
    "x = \"apple\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "central-authority",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for y in range(3, 6):  # 3, 4, 5\n",
    "    print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "variable-founder",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "p\n",
      "p\n",
      "l\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "for letter in \"apple\":\n",
    "    print(letter)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "professional-colleague",
   "metadata": {},
   "source": [
    "In a `for` loop, you can iterate over many different types of objects (lists, sets, tuples, dictionaries, strings, etc.)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "macro-groove",
   "metadata": {},
   "source": [
    "`range(a,b)` is a way of looping over all of the integers between `a` (inclusive) and `b` (exclusive). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "white-decision",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "h\n",
      "e\n",
      "l\n",
      "l\n",
      "o\n"
     ]
    }
   ],
   "source": [
    "for z in \"hello\":\n",
    "    print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "medieval-entry",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n",
      "-99\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "can only concatenate str (not \"int\") to str",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[5], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m z \u001b[38;5;129;01min\u001b[39;00m [\u001b[38;5;241m19\u001b[39m, \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m100\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mbanana\u001b[39m\u001b[38;5;124m\"\u001b[39m]:\n\u001b[0;32m----> 2\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[43mz\u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m)\n",
      "\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
     ]
    }
   ],
   "source": [
    "for z in [19, -100, \"banana\"]:\n",
    "    print(z+1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "lasting-birth",
   "metadata": {},
   "source": [
    "<hr style=\"border:1px solid black\"> </hr>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "textile-delight",
   "metadata": {},
   "source": [
    "You may have noticed that Python is not a **statically-typed** language, which means you do not need to tell it whether a variable you are defining is an integer or a string or a list, etc. You just define it, and it figures it out."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "voluntary-pollution",
   "metadata": {},
   "source": [
    "But, there are still different types! You can always use the `type` function to check what type an object has."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "blank-chrome",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L = [1, 2, 3]\n",
    "type(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "awful-chapel",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L = (1,2,3)\n",
    "type(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "announced-prisoner",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L = {1,2,3}\n",
    "type(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "outstanding-malaysia",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum([1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "blind-christopher",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "builtin_function_or_method"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "egyptian-kazakhstan",
   "metadata": {},
   "source": [
    "Now we're going to discuss a bunch of the fundamental types in Python."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "classified-style",
   "metadata": {},
   "source": [
    "## Integers, Floating Point Numbers, and Complex Numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "wired-truck",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = 7\n",
    "type(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "explicit-hostel",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "math.pi\n",
    "type(math.pi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "elementary-sugar",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7.0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = 7.0\n",
    "print(x)\n",
    "type(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "persistent-nightlife",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.0\n"
     ]
    }
   ],
   "source": [
    "x = 3.000000000000000000000000000000000000000001\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b65141f4-bed0-479a-adb4-51ca4236ac39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.111111111111111"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3.1111111111111111199999999999999999"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "bigger-degree",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.1\n"
     ]
    }
   ],
   "source": [
    "y = 0.1\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "functioning-jerusalem",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.551115123125783e-17"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0.1 + 0.1 + 0.1 - 0.3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "faced-enlargement",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0.1 + 0.1 + 0.1 == 0.3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "assumed-intention",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.142857142857143"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "15 / 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "authentic-romantic",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "15 // 7 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "a4336e58-049b-425e-9a2d-7473b3e592a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "635260041814039028930000076439182293672939514890842150130249020746875626743189843362624980569812898097233756942"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "4892742987239847293874239847238974239874298743298742398742398742398742398743982 * 129837198371298371298739812739281"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "1c1cf00d-8b23-400e-9502-94a9aa87e285",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "4^2  # not exponential functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "cf306a61-a78e-4fc0-b7a0-efa14d48b080",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "4**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "f9181682-d57e-4008-9316-146b668b5bd0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.972398738712822e+29"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "497239873871282182873476832763 * 1.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "desirable-exhaust",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3+5j)\n",
      "<class 'complex'>\n",
      "(8+2j)\n"
     ]
    }
   ],
   "source": [
    "z = complex(3, 5)\n",
    "t = complex(1,-1)\n",
    "print(z)\n",
    "print(type(z))\n",
    "print(z*t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa3ce999-ee51-4ec6-9ac3-22f977a01fb0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4bf234e4-4644-45db-a6a2-1a9554b17ea3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "128e143b-633c-4234-b01b-6c6b2a0c0d65",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "short-timothy",
   "metadata": {},
   "source": [
    "## Boolean"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "unable-candidate",
   "metadata": {},
   "source": [
    "A boolean is just a `True` or `False` value. That's it!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "plain-cherry",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bool"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = True\n",
    "type(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "criminal-harrison",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    }
   ],
   "source": [
    "if b:\n",
    "    print(\"hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "significant-completion",
   "metadata": {},
   "outputs": [],
   "source": [
    "if not b:\n",
    "    print(\"hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "6b761b4f-2c4d-4caf-8c3e-ee50654898e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "if b == False:\n",
    "    print(\"b is false\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "b5f4bbad-661d-4547-b42e-da8ba1ae90ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "if b is False:\n",
    "    print(\"b is false\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "hourly-wildlife",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "bool"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = (10 + 10 == 20) # Use \"==\" to test equality, and \"=\" to actually set something equal\n",
    "print(t)\n",
    "type(t)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "asian-rally",
   "metadata": {},
   "source": [
    "## None"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "typical-catering",
   "metadata": {},
   "source": [
    "There is a weird object in Python called `None`. It's just a useful thing to have around, often as a default value until you assign something."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "pointed-resource",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "y has the value None\n"
     ]
    }
   ],
   "source": [
    "y = None\n",
    "print(y)\n",
    "if y is None:\n",
    "    print(\"y has the value None\")\n",
    "y = 3\n",
    "if y is None:\n",
    "    print(\"y has the value None\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "2e738f21-0f07-44c4-b0ae-00fe108935ae",
   "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[44], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;241;43m1\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": [
    "1/0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bizarre-belle",
   "metadata": {},
   "source": [
    "## Strings"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "nuclear-vermont",
   "metadata": {},
   "source": [
    "A string is just a sequence of characters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "compatible-swiss",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(\"banana\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "activated-regression",
   "metadata": {},
   "source": [
    "You can do a million things with strings."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "distinct-measure",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['ba', 'a', 'a']"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = \"banana\"\n",
    "s.split(\"n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "3ea70e17-9585-4e30-8d03-475c7c1121e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ba\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "L = s.split(\"n\")\n",
    "print(L[0])\n",
    "print(type(L[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "74ec9f0f-3bb3-4264-9e27-2c7ca0b047a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"The dog says 'woof'.\""
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"The dog says 'woof'.\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "selective-transfer",
   "metadata": {},
   "source": [
    "Use `len` to find the length of a string and `+` to concatenate two strings together."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "republican-dryer",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11\n",
      "hello world\n"
     ]
    }
   ],
   "source": [
    "one = \"hello\"\n",
    "two = \"world\"\n",
    "three = one + \" \" + two\n",
    "print(len(three))\n",
    "print(three)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "shared-defensive",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'str' object has no attribute 'len'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[51], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mthree\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlen\u001b[49m()\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'str' object has no attribute 'len'"
     ]
    }
   ],
   "source": [
    "three.len()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "technological-prayer",
   "metadata": {},
   "source": [
    "## Lists\n",
    "A list is an **ordered sequence** of things."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "biological-genius",
   "metadata": {},
   "outputs": [],
   "source": [
    "L = [15, \"banana\", 7, False, [1, 2, 3]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "improving-exhibit",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[15, 'banana', 7, False, [1, 2, 3]]\n"
     ]
    }
   ],
   "source": [
    "print(L)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "renewable-prescription",
   "metadata": {},
   "source": [
    "Elements of lists are accessed with bracket notation, starting from 0."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "future-operator",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "local-cradle",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'banana'"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "approved-boxing",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "single-bermuda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "advance-organization",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L[4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "violent-headquarters",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(L[4])[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "stupid-examination",
   "metadata": {},
   "source": [
    "Use `len(L)` to get the length of a list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "moderate-london",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "living-aurora",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(L[4])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "coupled-tattoo",
   "metadata": {},
   "source": [
    "You can set elements of the list manually as well."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "intermediate-texas",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[15, 'banana', 7, False, [1, 2, 3]]"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "designed-tobago",
   "metadata": {},
   "outputs": [],
   "source": [
    "L[1] = \"apple\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "incident-contribution",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[15, 'apple', 7, False, [1, 2, 3]]"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "buried-jimmy",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list assignment 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[67], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mL\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m8\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcan\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt set this\u001b[39m\u001b[38;5;124m\"\u001b[39m\n",
      "\u001b[0;31mIndexError\u001b[0m: list assignment index out of range"
     ]
    }
   ],
   "source": [
    "L[8] = \"can't set this\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "pacific-sheep",
   "metadata": {},
   "source": [
    "You can sort lists:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "statistical-millennium",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-20, 0, 15]\n"
     ]
    }
   ],
   "source": [
    "R = [15, -20, 0]\n",
    "R.sort()\n",
    "print(R)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "collective-alliance",
   "metadata": {},
   "source": [
    "Notice the `.` in the notation above. We'll talk about this more when we cover object-oriented programming, but what we're basically doing here is telling the list `R` to perform its `sort()` operation on itself."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "partial-realtor",
   "metadata": {},
   "source": [
    "You may wonder why we did `len(R)` instead of `R.len()`... it's kind of just a quirk. You get used to it."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "clear-aaron",
   "metadata": {},
   "source": [
    "A few more quick list functions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "medical-academy",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-20, 0, 15]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "R"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "strong-algorithm",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-20, 0, 15, 17]\n"
     ]
    }
   ],
   "source": [
    "R.append(17)\n",
    "print(R)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "transsexual-writer",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-20, 0, 15, 17, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "R.extend([7, 8, 9])\n",
    "print(R)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "statutory-solid",
   "metadata": {},
   "source": [
    "Lastly (for now) you can concatenate two lists together with the `+` sign."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "indonesian-possession",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_list = [1,2,3] + [4,5,6]\n",
    "new_list\n",
    "x=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "26c26c7d-f100-456a-854f-4a6bc4ca8bdf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 1, 'banana']"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[1,1,1] + [\"banana\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "a8eb56f6-798e-429e-b3b0-7cf0691fbc66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[[1,2,3], [4,5,6], [7,8,9]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "capable-collect",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-20, 0, 15, 17, 7, 8, 9]\n",
      "[100, -20, 0, 15, 17, 7, 8, 9]\n",
      "[-20, 0, 15, 17, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "print(R)\n",
    "M = [100] + R\n",
    "print(M)\n",
    "print(R)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "necessary-ethernet",
   "metadata": {},
   "source": [
    "## Sets"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "hidden-elements",
   "metadata": {},
   "source": [
    "A list was an ordered sequence of things. A set is an **unordered sequence** of things with no repeats (just like in math)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "seeing-dairy",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4}\n"
     ]
    }
   ],
   "source": [
    "S = {1, 2, 3, 4}\n",
    "print(S)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "conventional-binding",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4}\n"
     ]
    }
   ],
   "source": [
    "T = {3, 1, 4, 2}\n",
    "print(T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "educated-verification",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S == T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dress-simulation",
   "metadata": {},
   "source": [
    "You can't access elements using the bracket notation because there is no first element, second element, etc. You should never assume that you know the order Python will internally store your list in!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "related-basis",
   "metadata": {},
   "outputs": [],
   "source": [
    "S[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "egyptian-zambia",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for element in S:\n",
    "    print(element)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "severe-geometry",
   "metadata": {},
   "source": [
    "Here are some functions you can do with sets:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "chief-academy",
   "metadata": {},
   "outputs": [],
   "source": [
    "first = {1,5,6}\n",
    "second = {2,4,5}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "copyrighted-physiology",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 4, 5, 6}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first.union(second)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "direct-veteran",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 4, 5, 6}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second.union(first)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "played-induction",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{5}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first.intersection(second)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "major-percentage",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 6}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first.difference(second) # all of the things IN first, and NOT IN second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3eb6a8a0-63c4-4628-89a7-0629445c7198",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 6}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first.difference(first.intersection(second))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "fdb5d4ba-4621-4b29-987d-2eb9bc49ce79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2, 4}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second.difference(first)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c0d8cdf5-ceeb-4296-af34-b570a02fd17d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 in first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f8cec5b6-591e-4164-9cec-5fe7b1f01acc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 in first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "feddb47d-01c4-4a46-9ea1-e37dd9de3201",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5 in first.difference(second)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "critical-martial",
   "metadata": {},
   "outputs": [],
   "source": [
    "# By the way, you write comments in python by just starting the line with the pound key."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f877c59-6d0c-41ed-ad39-c6895e46c5e9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "executive-hudson",
   "metadata": {},
   "outputs": [],
   "source": [
    "first = {1, 5, 6}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "detailed-topic",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 5, 6, 9}\n"
     ]
    }
   ],
   "source": [
    "first.add(9)\n",
    "print(first)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "infrared-vulnerability",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 5, 6, 9}\n"
     ]
    }
   ],
   "source": [
    "first.add(5)\n",
    "print(first) # No duplicates!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "seventh-hotel",
   "metadata": {},
   "outputs": [],
   "source": [
    "first.remove(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "duplicate-summit",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 6, 9}\n"
     ]
    }
   ],
   "source": [
    "print(first)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "periodic-strand",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "5",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[29], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mfirst\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremove\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mKeyError\u001b[0m: 5"
     ]
    }
   ],
   "source": [
    "first.remove(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "b87dd265-08e3-4dbc-9db3-90211cd612e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "first.discard(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ac5861c5-bfe3-42ff-8b4f-31b10700f47c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "({1, 6, 9}, {2, 4, 5})"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first, second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "373cd4c3-cc34-4dcc-a05e-ddcee6cd3f49",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for +: 'set' and 'set'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[40], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mfirst\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43msecond\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'set' and 'set'"
     ]
    }
   ],
   "source": [
    "first + second"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "falling-convenience",
   "metadata": {},
   "source": [
    "## Tuples"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "parallel-appearance",
   "metadata": {},
   "source": [
    "It starts to get a little tricky here! A tuple is an **ordered sequence** of things."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "identical-synthetic",
   "metadata": {},
   "source": [
    "Wait... isn't that what a list was?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "described-warning",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 4)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "T = (1,2,3,4)\n",
    "print(T)\n",
    "type(T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "massive-piece",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L = [1,2,3,4]\n",
    "L == T  # They are different types of objects, so they can't be equal."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "funded-language",
   "metadata": {},
   "source": [
    "The key is that a tuple is what we call **immutable**. Once it's defined, it *cannot* be changed, ever, at all."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "noble-grill",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 4)\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "print(T)\n",
    "print(T[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "willing-merchant",
   "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[34], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mT\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m17\u001b[39m\n",
      "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "T[2] = 17"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "vital-aquarium",
   "metadata": {},
   "source": [
    "It is still possible to do things like concatenate two tuples to make a new bigger tuple, but it's a **new** bigger tuple, and the original one is still unchanged."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "involved-munich",
   "metadata": {},
   "outputs": [],
   "source": [
    "T = T + (5,6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "950856a1-7c31-4147-83ac-ab89d8554955",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 3, 4, 5, 6)"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "temporal-hours",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 3, 4)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "junior-appliance",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'tuple' object has no attribute 'append'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[38], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mT\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mappend\u001b[49m(\u001b[38;5;241m5\u001b[39m)\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
     ]
    }
   ],
   "source": [
    "T.append(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fundamental-factor",
   "metadata": {},
   "source": [
    "So, we define a new tuple with parentheses, but there's one catch: if your tuple has a single item, it needs a special bit of syntax."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "cosmetic-raleigh",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = (1)\n",
    "print(x)\n",
    "type(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b07de15c-3cf0-4cba-a8f7-974eea4b3994",
   "metadata": {},
   "outputs": [],
   "source": [
    "y = 3 * (5+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "recorded-canadian",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1,)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = (1,)\n",
    "print(x)\n",
    "type(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "military-browser",
   "metadata": {},
   "source": [
    "So, lists are **mutable**, tuples are **immutable**. Why do we need two different versions?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "cooperative-muscle",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L = [1,2,3,4,5,6]\n",
    "5 in L"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "perceived-morrison",
   "metadata": {},
   "source": [
    "Under-the-hood, when you store things in a set, Python is being super smart about how it stores it. When you add an element to a set you really do not want python to have to scan one-by-one through all the things in the set to make sure it's not already there. So, it uses a clever technique called *hashing*."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "imported-upper",
   "metadata": {},
   "source": [
    "You don't need to know the details right now, but the broad idea is that Python takes each thing in the set and assigns a number to it called its *hash*, and then uses the hashes to make sure there are no duplicates."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "insured-nepal",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hash(17)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "guilty-granny",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6579000509280954569"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hash(\"banana\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "vocational-accreditation",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "590899387183067792"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hash((1,2,3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "recorded-intellectual",
   "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[52], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mhash\u001b[39;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[38;5;241;43m3\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m4\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
     ]
    }
   ],
   "source": [
    "hash([1,2,3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "brutal-popularity",
   "metadata": {},
   "outputs": [],
   "source": [
    "L = [1,2,3,4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "developing-strike",
   "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[53], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m {[\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m4\u001b[39m], [\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m], [\u001b[38;5;241m7\u001b[39m,\u001b[38;5;241m8\u001b[39m]}\n",
      "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
     ]
    }
   ],
   "source": [
    "{[1, 2, 3, 4], [1, 2], [7,8]}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "enclosed-mixer",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(1, 2), (1, 2, 3, 4), (7, 8)}"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{(1, 2, 3, 4), (1, 2), (7, 8)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "fb075a10-1ed5-41c9-9dfa-d386ffbfce85",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, [4, 5, 6])\n"
     ]
    }
   ],
   "source": [
    "example = (1, 2, [4, 5, 6])\n",
    "print(example)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "fd838644-6226-452f-b26e-fe5952f0b394",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 5, 6]"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "example[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "e415b110-2f0b-49ae-9eaa-8e7e4fbc0b61",
   "metadata": {},
   "outputs": [],
   "source": [
    "example[2].append(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "bb16448b-f956-44e2-988e-9ae2733e7674",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, [4, 5, 6, 10])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "27f6c3af-4dc5-4acb-9002-25899d97e0d4",
   "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[64], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mhash\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mexample\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
     ]
    }
   ],
   "source": [
    "hash(example)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "9a45aad7-3ee5-475a-916a-c07b2bcd7942",
   "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[60], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m example_set \u001b[38;5;241m=\u001b[39m {\u001b[38;5;241m5\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhello\u001b[39m\u001b[38;5;124m\"\u001b[39m, example}\n",
      "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
     ]
    }
   ],
   "source": [
    "example_set = {5, \"hello\", example}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "55c1356a-eb0e-4887-968c-ef5787692980",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, (1, 2, 3, 4, 5, 6))\n"
     ]
    }
   ],
   "source": [
    "T = (1, 2, T)\n",
    "print(T)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "9556e4f1-0937-4920-a62f-8a5292563a17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-6902896392512693720"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hash(T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "8a4703fe-d52a-4743-a7dc-f769473e6e54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5881802312257552497"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hash(T[2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "mysterious-orchestra",
   "metadata": {},
   "source": [
    "The problem is that you **can't hash mutable things**. Once you get an object's hash, that needs to stay its hash forever. You could hash a list, then appending an element to the list would mean a new hash would have to be generated, and this would mess everything up."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "liquid-bundle",
   "metadata": {},
   "source": [
    "Bottom line: Sometimes you need an immutable version of something, like to put it in a set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "several-joshua",
   "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[66], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m {\u001b[38;5;241m5\u001b[39m, \u001b[38;5;241m17\u001b[39m, [\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3\u001b[39m]}\n",
      "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
     ]
    }
   ],
   "source": [
    "{5, 17, [1,2,3]}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "mexican-blackberry",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(1, 2, 3), 17, 5}"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{5, 17, (1,2,3)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "divine-taiwan",
   "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[68], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m {\u001b[38;5;241m5\u001b[39m, \u001b[38;5;241m17\u001b[39m, {\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3\u001b[39m}}\n",
      "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'set'"
     ]
    }
   ],
   "source": [
    "{5, 17, {1,2,3}}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "wound-bulgaria",
   "metadata": {},
   "source": [
    "Sets are mutable too! Sets must contain immutable things, but they themselves are mutable.\n",
    "\n",
    "Of course we knew this, because we can do `S.add()`. So what if you want sets in your sets? There is an immutable version of a set called a `frozenset`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "thorough-microphone",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{17, 5, frozenset({1, 2, 3})}"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{ 5, 17, frozenset({1, 2, 3}) }"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "athletic-mixer",
   "metadata": {},
   "source": [
    "When should you use a tuple versus a list?\n",
    "- If it's going to go in a set (or, as we'll see in a second, in a dictionary), it has to be immutable. Thus, use a tuple.\n",
    "- If you need to be able to add and remove things, use a list.\n",
    "- If the size will always stay the same, you probably want a tuple. For example, if you're representing xy-coordiates, use tuples."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "brave-employer",
   "metadata": {},
   "source": [
    "## Dictionaries"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "distributed-trace",
   "metadata": {},
   "source": [
    "You can think of a list as kind of like a mathematical function whose inputs are the the indices 0, 1, ... and whose outputs are the elements of the list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "varied-contest",
   "metadata": {},
   "outputs": [],
   "source": [
    "L = [\"apple\", \"banana\", \"pear\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "legitimate-prison",
   "metadata": {},
   "outputs": [],
   "source": [
    "#  0 -> apple,  1 -> banana,  2 -> pear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "unavailable-manchester",
   "metadata": {},
   "source": [
    "In a dictionary, the inputs don't have to be integers, they can be any (immutable) object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "generic-power",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{17: 'apple', 'banana': 'pear', (1, 2, 3): True}\n"
     ]
    }
   ],
   "source": [
    "# To define  17 -> apple,  banana -> pear,  (1, 2, 3) -> True\n",
    "d = {17:\"apple\", \"banana\":\"pear\", (1,2,3):True}\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "micro-revelation",
   "metadata": {},
   "source": [
    "The inputs are called **keys** and the outputs are called **values**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "official-saturday",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'apple'"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d[17]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "broad-cookbook",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'pear'"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d[\"banana\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "temporal-exclusion",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d[(1,2,3)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "available-contrary",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello'"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d[\"pear\"] = \"hello\"\n",
    "d[\"pear\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "54146136-06bd-4ff0-b29a-3d8813e877a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{17: 'apple', 'banana': 'pear', (1, 2, 3): True, 'pear': 'hello'}"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "large-prerequisite",
   "metadata": {},
   "source": [
    "You can assign new values too"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "retained-fortune",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{17: 'apple', 'banana': 'pear', (1, 2, 3): True, 'pear': 'hello', 1: 'one'}\n"
     ]
    }
   ],
   "source": [
    "d[1] = \"one\"\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "responsible-knife",
   "metadata": {},
   "outputs": [],
   "source": [
    "d[ (2, 3, 5, 7) ] = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "f6ded60c-2928-4075-b0d8-489db9ef84bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{17: 'apple',\n",
       " 'banana': 'pear',\n",
       " (1, 2, 3): True,\n",
       " 'pear': 'hello',\n",
       " 1: 'one',\n",
       " (2, 3, 5, 7): False}"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "0a927e48-5d39-47f4-88e7-ce44cc6d1b7a",
   "metadata": {},
   "outputs": [],
   "source": [
    "d[ (1, 2, 3) ] = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "2557d1a6-7163-42b8-9857-8beaa452c712",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{17: 'apple',\n",
       " 'banana': 'pear',\n",
       " (1, 2, 3): False,\n",
       " 'pear': 'hello',\n",
       " 1: 'one',\n",
       " (2, 3, 5, 7): False}"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "vulnerable-black",
   "metadata": {},
   "source": [
    "Dictionaries are *super* useful, but take some getting used to. The keys are hashed in the background, which makes looking up the value for a given key very fast."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "bba254ab-934a-4fe3-a525-acf7c22b0c3e",
   "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[82], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43md\u001b[49m\u001b[43m[\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[38;5;241;43m3\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m \u001b[49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n",
      "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
     ]
    }
   ],
   "source": [
    "d[ [1,2,3] ] = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "2f6cafb8-e5b1-4d17-9081-04cf9b9c5996",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{17: 'apple',\n",
       " 'banana': 'pear',\n",
       " (1, 2, 3): False,\n",
       " 'pear': 'hello',\n",
       " 1: 'one',\n",
       " (2, 3, 5, 7): False}"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "available-element",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17\n",
      "banana\n",
      "(1, 2, 3)\n",
      "pear\n",
      "1\n",
      "(2, 3, 5, 7)\n"
     ]
    }
   ],
   "source": [
    "for k in d.keys():\n",
    "    print(k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "increased-functionality",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "pear\n",
      "False\n",
      "hello\n",
      "one\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "for v in d.values():\n",
    "    print(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "preceding-sight",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(17, 'apple')\n",
      "('banana', 'pear')\n",
      "((1, 2, 3), False)\n",
      "('pear', 'hello')\n",
      "(1, 'one')\n",
      "((2, 3, 5, 7), False)\n"
     ]
    }
   ],
   "source": [
    "for pair in d.items():\n",
    "    print(pair)\n",
    "# (key, value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "aaa1af1f-9b42-4d78-a9df-da384e87346d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{17: 'apple',\n",
       " 'banana': 'pear',\n",
       " (1, 2, 3): False,\n",
       " 'pear': 'hello',\n",
       " 1: 'one',\n",
       " (2, 3, 5, 7): False}"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "ea568278-5ff1-48c2-890c-05c04d90a16a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.pop((2,3,5,7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "2d4d9c44-7600-4a6c-b1a8-41c7666b4d65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{17: 'apple', 'banana': 'pear', (1, 2, 3): False, 'pear': 'hello', 1: 'one'}"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "754ffa40-54fa-48b4-b463-cbece27d6a11",
   "metadata": {},
   "outputs": [],
   "source": [
    "d[\"whatever\"] = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "20e20a34-bdf1-4554-864c-a309966a3d9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{17: 'apple',\n",
       " 'banana': 'pear',\n",
       " (1, 2, 3): False,\n",
       " 'pear': 'hello',\n",
       " 1: 'one',\n",
       " 'whatever': False}"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "9eaf81e0-6e46-4c01-94bf-a0bdbf70b6de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type({})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "abc246bf-0be0-4a19-bb2e-927b1b017355",
   "metadata": {},
   "outputs": [],
   "source": [
    "empty_set = set()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "19745116-cfd0-47b8-acfc-96b4dcdfffd6",
   "metadata": {},
   "outputs": [],
   "source": [
    "empty_dict = dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d244e67-7b3d-42bc-9db2-1118e5de03c5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "06da7f58-5654-4ab9-9321-614c68fef85f",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "7304dfc8-53c1-4020-a390-bd2bdc8787c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = [1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "329373be-1b37-41e3-a4af-63a6e870e5a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "s.append(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "78cdb819-a685-4d8f-a0ba-72fdc132a780",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ready-story",
   "metadata": {},
   "source": [
    "## Casting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a420e3cd-e42f-4e12-b123-029a156c8fe4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85df4574-0215-42cd-8c81-6367c19115ad",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "895509e1-84ee-436b-bf6c-96e010bac34e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3f13fed-f855-4f0f-9fda-c13c66b5457f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "frozen-sauce",
   "metadata": {},
   "source": [
    "You can tell Python to turn an object of one type into an object of another type. This is called **casting**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "lovely-terrain",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 7, 7, 12]\n"
     ]
    }
   ],
   "source": [
    "L = [3, 7, 7, 12]\n",
    "print(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "exposed-development",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 7, 7, 12)\n",
      "[3, 7, 7, 12]\n"
     ]
    }
   ],
   "source": [
    "T = tuple(L)\n",
    "print(T)\n",
    "print(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "flush-cause",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{3, 12, 7}\n"
     ]
    }
   ],
   "source": [
    "S = set(L)\n",
    "print(S)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3a9a2bff-00ba-4ae8-9f1c-dab078cdb42b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 12, 7]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# remove duplicates from a list, but leave it a list\n",
    "list(set(L))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7f77eac7-c906-4ad1-9d05-849910dc4722",
   "metadata": {},
   "outputs": [],
   "source": [
    "M = [\"hello\", 5, [1,2,3]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d6a2a492-3c77-4e5f-b762-f7dd6089e182",
   "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[7], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mset\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mM\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
     ]
    }
   ],
   "source": [
    "set(M)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "de9f7cca-5d84-4e07-b5c9-e3f38753894e",
   "metadata": {},
   "outputs": [],
   "source": [
    "M = [\"hello\", 5, (1,2,3)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "519eff07-5c81-4ebc-954e-8d3a2531764e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(1, 2, 3), 5, 'hello'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set(M)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "precise-chile",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(L)\n",
    "list(set(L))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "81dafa29-6942-44c7-99ed-c0a5199ef27a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 7, 7, 12]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "instrumental-tracy",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "cannot convert dictionary update sequence element #0 to a sequence",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[11], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mdict\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mL\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: cannot convert dictionary update sequence element #0 to a sequence"
     ]
    }
   ],
   "source": [
    "dict(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "reserved-hunter",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[3, 7, 7, 12]'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "experimental-defeat",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "int() argument must be a string, a bytes-like object or a real number, not 'list'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[13], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mL\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: int() argument must be a string, a bytes-like object or a real number, not 'list'"
     ]
    }
   ],
   "source": [
    "int(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "69917144-f1d1-46f8-aafe-f1a057541b6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13287843"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int(\"13287843\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "617c41af-5755-4e4f-b4e4-dc1e261eb19b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12871.123897"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "float(\"12871.123897\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d143504e-3df9-4872-a143-3b734c40e2cf",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: 'hello'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[17], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mint\u001b[39;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;31mValueError\u001b[0m: invalid literal for int() with base 10: 'hello'"
     ]
    }
   ],
   "source": [
    "int(\"hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "70485ff5-5983-445c-9adc-abe512f9a045",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['h', 'e', 'l', 'l', 'o']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(\"hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f9ff99b-f0bd-45b9-9f66-76c73cea0c3a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "dangerous-password",
   "metadata": {},
   "outputs": [],
   "source": [
    "d = {1:\"one\", 2:\"two\", 3:\"three\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ef78ff59-14ca-4e55-9bc4-cea0746e1caf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'two'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "inside-collective",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "de990a22-6a19-4f6a-b52b-e032531cf823",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['one', 'two', 'three']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(d.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "457aa5d3-42a1-427b-ba85-5e89c0b6d824",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 'one'), (2, 'two'), (3, 'three')]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(d.items())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "95238139-b10c-4109-b229-7c1bc0659294",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = [[1,2,3],[4,5,6],[7,8,9]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "9e25bedc-f546-4d83-a062-0f1b1f47675a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(A[1])[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "5e6ca092-b458-4bb5-b9ea-15743e6e29ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[0;31mSignature:\u001b[0m \u001b[0mchr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m/\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
       "\u001b[0;31mDocstring:\u001b[0m Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.\n",
       "\u001b[0;31mType:\u001b[0m      builtin_function_or_method"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "chr?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "ef2f26db-a25e-4a38-939c-a8971ec53a24",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "104"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ord(\"h\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "537124c7-9caa-4631-b679-42f27496ff5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'h'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chr(104)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d72395bb-70ae-4b53-8029-ea4e5eb6e4fa",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "054a4947-e8cb-4c8e-84e0-b9b56c526e13",
   "metadata": {},
   "source": [
    "## Practice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3e8aad9-11aa-41f2-97fd-d1c27d13553c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64cbf258-170c-4488-b208-02f23d2ac50f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c185b0d2-78be-4eb1-8cc2-60b400b2bc22",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "vertical-pulse",
   "metadata": {},
   "source": [
    "<hr style=\"border:1px solid black\"> </hr>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "duplicate-homework",
   "metadata": {},
   "source": [
    "Time for some practice!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "metric-notebook",
   "metadata": {},
   "source": [
    "https://projecteuler.net/"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "refined-missouri",
   "metadata": {},
   "source": [
    "Problem 1: mod, looping, and comprehensions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "global-commercial",
   "metadata": {},
   "source": [
    "Problem 2: negative indexing"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "allied-entertainment",
   "metadata": {},
   "source": [
    "Problem 5: all / any, and thinking mathematically"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "tracked-married",
   "metadata": {},
   "source": [
    "<hr style=\"border:1px solid black\"> </hr>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "iraqi-delta",
   "metadata": {},
   "source": [
    "If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.\n",
    "\n",
    "Find the sum of all the multiples of 3 or 5 below 1000."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "imperial-medline",
   "metadata": {},
   "outputs": [],
   "source": [
    "# mod - modulus\n",
    "#  a % b -- the remainder you get when you divide a by b\n",
    "\n",
    "# list comprehensions\n",
    "# += "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "eligible-salmon",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10 % 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c040dc05-1f4d-408f-a3f3-2f90ef3b933a",
   "metadata": {},
   "outputs": [],
   "source": [
    "10 = 4*2 + 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "f2302669-48bf-42bd-b03f-45a2e0feb9fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "13 % 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a2e33d50-c40a-420a-a24f-e7a7d7ce1d50",
   "metadata": {},
   "outputs": [],
   "source": [
    "# a % b is a number between 0 and a-1 (inclusive)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "7e2ed679-cb1a-497e-b455-bad550cbc53b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "14 % 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "6a0a1393-cfcf-4e20-8d86-1fd8e7e208a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "233168\n"
     ]
    }
   ],
   "source": [
    "answer = 0\n",
    "for number in range(1, 1000):\n",
    "    if number % 3 == 0:\n",
    "        answer = answer + number\n",
    "    elif number % 5 == 0:\n",
    "        answer = answer + number\n",
    "print(answer)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0aa3815e-fafc-4e74-b33d-7c36e1e1d612",
   "metadata": {},
   "source": [
    "if (number is div by 3):\n",
    "   something\n",
    "else:\n",
    "   if (number is div by 5):\n",
    "       something"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "653a83b0-ebf4-4cb1-adbc-8f6e587a07b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "233168\n"
     ]
    }
   ],
   "source": [
    "answer = 0\n",
    "for number in range(1, 1000):\n",
    "    if number % 3 == 0 or number % 5 == 0:\n",
    "        answer = answer + number\n",
    "print(answer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "5115d97b-0bea-40de-8211-43dd2c9bea0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#   \"A += B\"  is shorthand for \"A = A + B\"\n",
    "#    A -= B     A = A - B\n",
    "#    A *= B     A = A * B\n",
    "#    A /= B     A = A/B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "c24f23ca-9181-4115-b3a9-960c4b82b7f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.3333333333333335\n"
     ]
    }
   ],
   "source": [
    "x = 10\n",
    "x /= 3\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "7f7b1eb8-c572-48d8-b596-b3d9dd746192",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "233168\n"
     ]
    }
   ],
   "source": [
    "answer = 0\n",
    "for number in range(1, 1000):\n",
    "    if number % 3 == 0 or number % 5 == 0:\n",
    "        answer += number\n",
    "print(answer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c3bafe9-30f7-4bcf-bd88-d4044ab5c31f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# list comprehension"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "e498c46a-5903-45a7-9cc6-08ba9247992f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x for x in range(10)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "959de493-7553-4ef7-9f3c-df8e6c682684",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 9, 25, 49, 81]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x**2 for x in range(10) if x % 2 == 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "2a0f48f2-f3d6-4566-928c-9420e8614fb0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 9, 25, 49, 81}"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{x**2 for x in range(10) if x % 2 == 1}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "8221e811-983c-49a8-a9d3-bffa806b7ce5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 9, 25, 49, 81)"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuple(x**2 for x in range(10) if x % 2 == 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "09cdf878-48a9-4c4a-872c-e8889ba4f9a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'!': 1, '!!!': 9, '!!!!!': 25, '!!!!!!!': 49, '!!!!!!!!!': 81}"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{(\"!\"*x):x**2 for x in range(10) if x % 2 == 1}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "23f1d77a-5868-489a-ae79-ec8d8e31b9ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "233168"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# solve the problem with a list comprehension\n",
    "sum([x for x in range(1000) if x % 3 == 0 or x % 5 == 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f83f6f6-e761-40d8-9a69-b7aaf5886d27",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "734a81d3-0f4c-413a-9b4e-e2cf7e30bb67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apple': 5, 'banana': 6, 'grape': 5, 'pear': 4, 'dragonfruit': 11}"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L = [\"apple\", \"banana\", \"grape\", \"pear\", \"dragonfruit\"]\n",
    "{fruit : len(fruit) for fruit in L}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "468d6302-0d59-4e92-b4aa-bb1d5bec1796",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4bc6089b-2e63-4471-8c5d-99a83fbf0eb8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "english-means",
   "metadata": {},
   "source": [
    "Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:\n",
    "\n",
    "1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...\n",
    "\n",
    "By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "according-equipment",
   "metadata": {},
   "outputs": [],
   "source": [
    "# generate a list of all fibonacci numbers up to 4m\n",
    "# use a list comprehension to filter out the even ones\n",
    "# sum them"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "bc4858c9-7f81-4518-9940-9322ad589dd4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4613732\n"
     ]
    }
   ],
   "source": [
    "fib = [1, 2]\n",
    "# add together the last two elements of fib\n",
    "# if that's less than 4 million, append it to fib and keep going\n",
    "# otherwise stop\n",
    "while True:\n",
    "    # next_number = fib[len(fib)-1] + fib[len(fib)-2]\n",
    "    # negative indexing\n",
    "    next_number = fib[-1] + fib[-2]\n",
    "    # fib[-1] is the last thing in the list\n",
    "    # fib[-2] is the second to last\n",
    "    if next_number < 4_000_000:\n",
    "        fib.append(next_number)\n",
    "        # if even, add it to a running total\n",
    "    else:\n",
    "        break\n",
    "print(sum([number for number in fib if number % 2 == 0]))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "21dbbc53-c44c-4545-95a7-8eb83813d86c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4200000.0"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "4 * 10**6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c4f6f15-c57d-42ef-a27b-a855081ec67f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bafa3f1c-9797-422d-bbff-734f4e17111a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26b3ecde-7257-47ae-a195-e86d5840f276",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "538742e0-cb1f-402e-b601-cf9f41377944",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "comic-vietnam",
   "metadata": {},
   "source": [
    "2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.\n",
    "\n",
    "What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "civil-sterling",
   "metadata": {},
   "outputs": [],
   "source": [
    "# all / any"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "optional-detail",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
