{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "id": "wRInyZwMAFjD"
   },
   "outputs": [],
   "source": [
    "a = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "WCVXpW1AAFfy",
    "outputId": "4edad6e1-c215-475a-a1f3-643ac76cc93e"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "wJbEdg_6AFci",
    "outputId": "114b6e55-cb82-4ea3-a9b4-d6c3317f839a"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Y8dLucjCAFZb",
    "outputId": "12501770-93ea-44d1-caf1-1b04d89f28a0"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello world\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "id": "9WOspGbKAFWJ"
   },
   "outputs": [],
   "source": [
    "# Factorial function: iterative\n",
    "def fact(n):\n",
    "  result = 1\n",
    "  while n > 1:\n",
    "    result = result * n\n",
    "    n = n - 1\n",
    "\n",
    "  return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "WybNYaddAFTC",
    "outputId": "72599744-0d2d-4343-a072-7a35e239eed7"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fact(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "id": "yYqUeru8AFP4"
   },
   "outputs": [],
   "source": [
    "# Factorial function: iterative\n",
    "def fact(n):\n",
    "  if n == 0:\n",
    "    return 1\n",
    "  return n * fact(n - 1)\n",
    "\n",
    "  return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "2NS4JDpRAFMx",
    "outputId": "3187c6c4-600f-4bac-cb7b-0f955152ac40"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fact(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "H52VNQkIAFJj",
    "outputId": "dd7302e5-dc9c-4d93-9915-12fbd1cdbbe6"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "function"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(fact)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "qDINA2hPAFGi",
    "outputId": "c57fc31a-e49d-4172-f5b6-54f5eaabc03a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 0.1\n",
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "MODsy-t4AFDZ",
    "outputId": "606ba19a-bb58-48ae-f6f8-39462798e7b5"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9999999999999999\n"
     ]
    }
   ],
   "source": [
    "a = 0.1\n",
    "sum = 0\n",
    "for _ in range(10):\n",
    "  sum = sum + a\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "zvjMRu6FAFAJ",
    "outputId": "f3d6b533-ac04-4e98-d820-0236c64b8705"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "gXmh7fPIAE8x",
    "outputId": "5dba391e-0144-4254-a2e1-c3b0d467e9ac"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.1\n"
     ]
    }
   ],
   "source": [
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "id": "HLB96cBWAE5S",
    "outputId": "5501571b-2631-430a-b508-5287c25236f3"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a is really  0.10000000000000000555'"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Formatted string\n",
    "f\"a is really {a : .20f}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "EXoGht-pAEHy",
    "outputId": "e161e83e-e842-4293-881f-968db00dd701"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "vkQCR5U8GFBG",
    "outputId": "e1277ac4-e572-4787-806c-ea58166d9a30"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(1, 10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "JdgjBl-gGFKG",
    "outputId": "b41e1e48-9f09-49a4-ff1d-77673d987ccc"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 5, 7, 9]"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(1, 10, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "hDFeRMsBGFNf",
    "outputId": "d7d5b51d-9f2c-40db-bf04-aa0f4e79c797"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = [1, 2, 3, 4, 5]\n",
    "type(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "id": "mZakWtFfGFQe"
   },
   "outputs": [],
   "source": [
    "l = list(range(20))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "3Cm2xoVBGFTl",
    "outputId": "d6c121ac-4ef4-4260-9aab-b161afdad07a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "x8zqawGPGFWo",
    "outputId": "cd40729c-4f9c-448d-b815-94006c2f4747"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 4, 5, 6, 7, 8, 9, 10]\n"
     ]
    }
   ],
   "source": [
    "l = l[2:11]\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "lUhrswm-GFZ1",
    "outputId": "52d34420-7169-433f-a5a2-1dd7450a4119"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "l = l[2:11:2]\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "VnTf_Sb5GFc-",
    "outputId": "c681a69e-2dd9-432d-bce7-ba9cc2f1a40a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[6, 8, 10]"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l[1:len(l)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "XRqpmku7GFfv",
    "outputId": "78937495-33e0-4f03-d2a5-995d15526088"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[6, 8, 10]"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Ql3ej5yGGFim",
    "outputId": "21399e5c-bc59-46ae-8880-274e7830407c"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 6, 8, 10]"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "NMZRBSPSGFlu",
    "outputId": "791d4476-c75b-4585-93ef-ae4786f1a06c"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 6, 8]"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "iprzaTAoGFpX",
    "outputId": "ec6228c0-1e97-43be-8aba-fddf3b63a169"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 6, 8, 10]"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "DKRE_HduH88i",
    "outputId": "9b678532-4247-4681-ec6b-1c01e63707c8"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 8, 6, 4]\n"
     ]
    }
   ],
   "source": [
    "lnew = l[::-1]\n",
    "print(lnew)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "bkp65kesH9FR",
    "outputId": "853ccdad-90eb-4c54-cb15-3b48f9bbb1e9"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 10, 6, 4]\n"
     ]
    }
   ],
   "source": [
    "lnew[1] = 10\n",
    "print(lnew)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "pYBurfdDH9Ip",
    "outputId": "30972f03-208c-45c2-f60d-c75cd30b1c1d"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "print(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "qXP5fQoNH9Lq",
    "outputId": "21b8846b-7333-4df8-e3df-d95aa246686b"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 6, 8, 8.5]\n"
     ]
    }
   ],
   "source": [
    "l[3] = 8.5\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "oVoaGjmOH9Or",
    "outputId": "dd141fd2-117a-4678-af46-8721839b2d72"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['charlotte', 6, 8, 8.5]\n"
     ]
    }
   ],
   "source": [
    "l[0] = 'charlotte'\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "upA84LfQH9R6",
    "outputId": "d14e3ebc-fdf2-4cc1-9754-f7c666369486"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = (1, 3, 5, 5)\n",
    "type(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 141
    },
    "id": "-D_s3nBuH9U7",
    "outputId": "cdb71f34-768d-4b5b-f6dd-ae816a76d91f"
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mTypeError\u001b[39m                                 Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[102]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m      1\u001b[39m \u001b[38;5;66;03m# Python tuples are immutable\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m \u001b[43mt\u001b[49m\u001b[43m[\u001b[49m\u001b[32;43m0\u001b[39;49m\u001b[43m]\u001b[49m = \u001b[32m0\u001b[39m\n",
      "\u001b[31mTypeError\u001b[39m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "# Python tuples are immutable\n",
    "t[0] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "SkQAvyfaH9YD",
    "outputId": "5a55c77b-4d68-4886-ef61-ca18d8f4e05d"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3)\n"
     ]
    }
   ],
   "source": [
    "a = 1, 2, 3\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "id": "l-DijU97H9bC"
   },
   "outputs": [],
   "source": [
    "# This does tuple assignment (a, b) = (1, 2)\n",
    "a, b = 1, 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "cDO-j3vcH9eT",
    "outputId": "e875f298-da4c-4af4-f5e9-f2f98b2d5938"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2\n"
     ]
    }
   ],
   "source": [
    "print (a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "jlJIlNR3H9hS",
    "outputId": "cad50486-5178-4c2c-8c62-8a4afab7020e"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 1\n"
     ]
    }
   ],
   "source": [
    "temp = a\n",
    "a = b\n",
    "b = temp\n",
    "print(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "uIYiFZOuLedh",
    "outputId": "922dbde2-fdaa-48d2-c4bd-581d0098aa02"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2\n"
     ]
    }
   ],
   "source": [
    "a, b = b, a\n",
    "print(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "g_tXomT2LeiC",
    "outputId": "f3f797a9-7779-4184-c92b-a306d2f1fc26"
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'math' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mNameError\u001b[39m                                 Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[108]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mmath\u001b[49m.pi\n",
      "\u001b[31mNameError\u001b[39m: name 'math' is not defined"
     ]
    }
   ],
   "source": [
    "math.pi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "id": "BdkuLzYgLelB",
    "outputId": "cb9546c5-40aa-4c91-eef0-50cbb223a249"
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'math' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mNameError\u001b[39m                                 Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[109]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m spi = \u001b[38;5;28mstr\u001b[39m(\u001b[43mmath\u001b[49m.pi)\n\u001b[32m      2\u001b[39m spi\n",
      "\u001b[31mNameError\u001b[39m: name 'math' is not defined"
     ]
    }
   ],
   "source": [
    "spi = str(math.pi)\n",
    "spi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "edOrBJL7LeoC",
    "outputId": "ef59125e-79f1-4bed-ad23-6a1558f5a0c4"
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'spi' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mNameError\u001b[39m                                 Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[110]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28mfloat\u001b[39m(\u001b[43mspi\u001b[49m)\n",
      "\u001b[31mNameError\u001b[39m: name 'spi' is not defined"
     ]
    }
   ],
   "source": [
    "float(spi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "i8rcSR-CLerB",
    "outputId": "3cadc689-601f-41a8-ccd0-c6d5e659006d"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2351"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int(\"2351\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "NhvC6civsomA",
    "outputId": "857f66d4-a171-4376-a189-a69708b9a41a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = 'UNC Charlotte'\n",
    "s.find('har')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "w4OGzNzMuMYf",
    "outputId": "e5b28d54-a3a9-4c6d-f4b9-9be2b51a4ea6"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.find('hare')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "id": "ezzWDYOTuP_u"
   },
   "outputs": [],
   "source": [
    "None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "B3oKCPmkuRzF",
    "outputId": "d84e61fc-8ae9-4dbe-f0e2-e7202e45dab7"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "34"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = \"UNC Charlotte's campus is full of hares.\"\n",
    "s.rfind('har')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "q90UnvPpuegv",
    "outputId": "4a82dce5-a257-4fda-c0da-8740aa1af9b4"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['UNC', \"Charlotte's\", 'campus', 'is', 'full', 'of', 'hares.']"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 141
    },
    "id": "fO4OdktEumnw",
    "outputId": "52401b63-5a4d-48b0-84a2-69fc68aeaa4c"
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "unterminated string literal (detected at line 1) (1253534556.py, line 1)",
     "output_type": "error",
     "traceback": [
      "  \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[117]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[31m    \u001b[39m\u001b[31ms = 'UNC Charlotte's campus is full of hares.'\u001b[39m\n                                                 ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m unterminated string literal (detected at line 1)\n"
     ]
    }
   ],
   "source": [
    "s = 'UNC Charlotte's campus is full of hares.'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "id": "Tn-uh61Au5Ea",
    "outputId": "a356559e-0d1e-4c25-bb36-50264e1d536f"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"UNC Charlotte's campus is full of hares.\""
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "QJIiX7cyu-Lz",
    "outputId": "5d49fed5-0067-422e-d35a-2ef8eb720d8a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('charl', 'charl')"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = 'charlotte'\n",
    "s[0:5], s[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "id": "gKvt2zYYvFjx",
    "outputId": "82a79c5b-09a1-4a92-d7a1-a933952b399d"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'cal'"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[0:5:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "PLVWR2n9vJRo",
    "outputId": "31f63a0e-dcd5-4ce6-d31c-f10eacfeb391"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('e', 't', 'te')"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[-1], s[-2], s[-2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "id": "jYwR8Y_DvRG8",
    "outputId": "bd144b7a-2292-4c88-aa24-1d34758a9341"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ettolrahc'"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "id": "Iq2gdMaPvV8D",
    "outputId": "a6f90467-5b11-475e-bfb2-941d6597cf2a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'charlotte'"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 176
    },
    "id": "Y4f9lcOwvdzs",
    "outputId": "f48357ef-293b-40c8-b747-e57c60269aa2"
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'str' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mTypeError\u001b[39m                                 Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[124]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m      1\u001b[39m \u001b[38;5;66;03m# Python strings are immutable\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m \u001b[43ms\u001b[49m\u001b[43m[\u001b[49m\u001b[32;43m0\u001b[39;49m\u001b[43m]\u001b[49m = \u001b[33m'\u001b[39m\u001b[33ms\u001b[39m\u001b[33m'\u001b[39m\n",
      "\u001b[31mTypeError\u001b[39m: 'str' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "# Python strings are immutable\n",
    "s[0] = 's'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "NtkYQDz4viX9",
    "outputId": "00590ae2-4ee7-485e-dde7-22f97edac930"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('charlotte', 'charlottecharlotte')"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s, s * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "id": "MUWr3tgDv8wv",
    "outputId": "61268d26-7ae8-4f4b-83b3-b6ecc0a47ed2"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'charlottecharlotte'"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s + s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "id": "H3665QcUv-3P"
   },
   "outputs": [],
   "source": [
    "x = list(range(1, 11))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "P5tMfr0VwC7W",
    "outputId": "833fa1e7-2942-4b23-a694-2fb840206511"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "ZUVa5yb1wDt_",
    "outputId": "aa1c48ac-a2a9-4cbd-f37a-25b6b1477663"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([1, 3, 5, 7, 9], [2, 4, 6, 8, 10])"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[::2], x[1::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "5uiQlDQQwKLP",
    "outputId": "072114b5-5c0f-46fd-ed60-86dbc11b371f"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 5, 4, 5, 9, 7, 8, 9, 10]"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[2] += 2\n",
    "x[5] += 3\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Ty_WviDiwYQ4",
    "outputId": "77123e49-ab78-4809-a298-60b8b2fac9e1"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20, 40, 80, 100]"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[j * 10 for j in x if j % 2 == 0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "5GnipSWcwsoq",
    "outputId": "aafddc3a-7522-4628-efdb-990cbedaff1f"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 4, 8, 10]"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[j for j in x if j % 2 == 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "SWTMmrm5w5TD",
    "outputId": "c3d92880-16f6-47c1-f1b0-7586320fdbf0"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 5, 4, 5, 9, 7, 8, 9, 10]"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "JrvnBrwSxne1",
    "outputId": "70ab964d-9ea3-4464-900a-3f40470aeb14"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 5, 4, 5, 9, 7, 8, 9, 10)"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = tuple(x)\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 176
    },
    "id": "IW7YfWvTxq1t",
    "outputId": "43d1a0a4-46ec-4c64-9c7f-fd1f5221678e"
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mTypeError\u001b[39m                                 Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[135]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43my\u001b[49m\u001b[43m[\u001b[49m\u001b[32;43m0\u001b[39;49m\u001b[43m]\u001b[49m = \u001b[32m0\u001b[39m\n",
      "\u001b[31mTypeError\u001b[39m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "y[0] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Mlv46mNNxs1V",
    "outputId": "781eb97a-e6e4-478b-ad30-e75f7d4ddf4e"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def prod_sum(a, b):\n",
    "  return a + b, a * b\n",
    "\n",
    "y = prod_sum(2, 3)\n",
    "type(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "EC5-hyeCx89H",
    "outputId": "a0cac2da-084a-46c3-f550-8cc56bc26375"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 6)"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "9GgzPwmVyDXP",
    "outputId": "871fd34e-3783-4bc3-fe6a-095cabdfd237"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5,)"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(5,)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "CfslZoRmyFww",
    "outputId": "9ba5885d-3e85-4141-a7b9-50bb7885d4f5"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "gqvr-figr92q",
    "outputId": "324660f3-682f-486d-8002-4cf85716181b"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "{'john': 23, 'alex': 25, 'bill': 99}\n"
     ]
    }
   ],
   "source": [
    "# Dictionaries\n",
    "d = {'john': 23, 'alex': 25, 'bill': 99}\n",
    "print(type(d))\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "sWEcesL3r96v",
    "outputId": "e4c4fa1b-067d-4e71-c075-5f8e786a4f47"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'alex' in d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "2jY2ygw4L3q7",
    "outputId": "74171846-a88b-4354-a36e-63dd7c9adb2d"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'mary' in d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "JhWnvhmZL3wU",
    "outputId": "262fe289-b268-4ad7-99b9-10c2bf812b0e"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'john': 23, 'alex': 25, 'bill': 99, 'mary': 30}"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d['mary'] = 30\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "dVXSYU96L-4G",
    "outputId": "2c767f1a-07aa-4fb7-f8ea-9c7a1e5eda07"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'john': 35, 'alex': 25, 'bill': 99, 'mary': 30}"
      ]
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d['john'] = 35\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 144
    },
    "id": "RHFw8DCYL-7O",
    "outputId": "e394de71-3768-44aa-b714-17f473f94635"
   },
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'harry'",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mKeyError\u001b[39m                                  Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[145]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43md\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mharry\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[31mKeyError\u001b[39m: 'harry'"
     ]
    }
   ],
   "source": [
    "d['harry']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "qCQs8VGcL--V",
    "outputId": "cec8eb9e-5469-48ba-bdfe-cc94920a3f9c"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'john': 35, 'alex': 25, 'bill': 99, 'mary': 30, 'harry': 10, 'barry': 10}\n"
     ]
    }
   ],
   "source": [
    "# Two solutions for initializing value (if key non-existent) or updating value (if key exists in dictionary).\n",
    "\n",
    "key = 'harry'\n",
    "# Solution 1\n",
    "if key in d:\n",
    "  d[key] += 10\n",
    "else:\n",
    "  d[key] = 10\n",
    "\n",
    "key = 'barry'\n",
    "#Solution 2\n",
    "d[key] = d.get(key, 0) + 10\n",
    "\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {
    "id": "kuJwZ3tJL_Bs"
   },
   "outputs": [],
   "source": [
    "del d['harry']\n",
    "del d['barry']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "glGtggioL_FV"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "KCeQYGlmLetx",
    "outputId": "e18eb2f3-008d-4c02-d5d8-238201e44169"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm tired\n"
     ]
    }
   ],
   "source": [
    "# Note right-branching effect of if-else, and one way of including quotes in a Python string.\n",
    "a = 5\n",
    "if a == 0:\n",
    "  print('nada')\n",
    "else:\n",
    "  if a == 1:\n",
    "    print('uno')\n",
    "  else:\n",
    "    if a == 2:\n",
    "      print('dos')\n",
    "    else:\n",
    "      if a == 3:\n",
    "        print('tres')\n",
    "      else:\n",
    "        print(\"I'm tired\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "-s5gi8CUMQJD",
    "outputId": "677c16d2-e417-4fd8-93a8-9b1a7153fb5d"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm tired\n"
     ]
    }
   ],
   "source": [
    "# elif statements eliminate right-branching.\n",
    "a = 5\n",
    "if a == 0:\n",
    "  print('nada')\n",
    "elif a == 1:\n",
    "  print('uno')\n",
    "elif a == 2:\n",
    "  print('dos')\n",
    "elif a == 3:\n",
    "  print('tres')\n",
    "else:\n",
    "  print(\"I'm tired\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {
    "id": "N3f1jLHfMQMI"
   },
   "outputs": [],
   "source": [
    "# If x belongs to a, return True (else with for).\n",
    "def search(a, x):\n",
    "  for e in a:\n",
    "    if e == x:\n",
    "      return True\n",
    "  else:\n",
    "    return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "KZrZeYPsMQPS",
    "outputId": "dfa578d2-2b21-47dd-9483-dc6bfca7d808"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search([1, 2, 3, 5], 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "otKvxAeIP5lr",
    "outputId": "72e4d7ba-999f-4b30-b98b-523d8610c563"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "l1 = [1, 2, 3]\n",
    "l1.append(4)\n",
    "print(l1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 0
    },
    "id": "0miFsyAxOeHi",
    "outputId": "e6abca38-74d3-408f-e052-0063beaa2906"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040]\n"
     ]
    }
   ],
   "source": [
    "# Fibonnacci numbers 1, 1, 2, 3, 5, 8, 13, 21, ...\n",
    "# Function that generates a list with the first n Fibonnacci numbers.\n",
    "def fibo1(n):\n",
    "  if n == 1:\n",
    "    return [1]\n",
    "\n",
    "  if n == 2:\n",
    "    return [1, 1]\n",
    "\n",
    "  a, b = 1, 1\n",
    "  result = [a, b]\n",
    "  for _ in range(n-2):\n",
    "    a, b = b, a + b\n",
    "    result.append(b)\n",
    "\n",
    "  return result\n",
    "\n",
    "print(fibo1(30))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "JqJ-U-vSRNWk",
    "outputId": "88f7afb7-2739-4601-d4b1-78c19829e141"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "354224848179261915075"
      ]
     },
     "execution_count": 154,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Fibonnacci numbers 1, 1, 2, 3, 5, 8, 13, 21, ...\n",
    "# Function that generates the nth Fibonnacci numbers.\n",
    "def fibo1(n):\n",
    "  a, b = 0, 1\n",
    "  for _ in range(n):\n",
    "    a, b = b, a + b\n",
    "\n",
    "  return a\n",
    "\n",
    "fibo1(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 321
    },
    "id": "OrQHZ203PQ0R",
    "outputId": "50e17c3c-b9d6-4f60-a745-a9564d450cad"
   },
   "outputs": [],
   "source": [
    "# Fibonnacci numbers 1, 1, 2, 3, 5, 8, 13, 21, ...\n",
    "# Function that generates the nth Fibonnaci number\n",
    "def fibo2(n):\n",
    "  if n == 1:\n",
    "    return 1\n",
    "\n",
    "  if n == 2:\n",
    "    return 1\n",
    "\n",
    "  return fibo2(n-1) + fibo2(n-2)\n",
    "\n",
    "fibo2(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "metadata": {
    "id": "AzNNNnx8OeNZ"
   },
   "outputs": [],
   "source": [
    "def fibo():\n",
    "  a, b, = 1, 1\n",
    "  while True:\n",
    "    yield a\n",
    "    a, b = b, a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {
    "id": "7sZP80ZrOeWH"
   },
   "outputs": [],
   "source": [
    "gen = fibo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 0
    },
    "id": "o67fzqKLOef-",
    "outputId": "588b6749-870b-4a20-8a12-b0f7f2afa974"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(gen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 0
    },
    "id": "btYorKcEMQSb",
    "outputId": "e77bc600-3210-42a0-db39-bb38bb4da1fe"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 159,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(gen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 0
    },
    "id": "EnXXXl2OMQV0",
    "outputId": "2ca682f9-386c-49b3-c117-395a752c2e1b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 160,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(gen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 0
    },
    "id": "HOt_9LArLexB",
    "outputId": "4bb69b1a-16f9-4244-8eff-d7e58521815a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 161,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(gen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 0
    },
    "id": "tId5tC85Le0A",
    "outputId": "0413ed2b-96c8-4b77-fa6a-2628fe1d4186"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 "
     ]
    }
   ],
   "source": [
    "for _ in range(20):\n",
    "  print(next(gen), end = ' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "metadata": {
    "id": "C0btsslbvPfo"
   },
   "outputs": [],
   "source": [
    "# Use `cmath` if you need to generate complex numbers.\n",
    "import math\n",
    "\n",
    "def quad_sol(a, b, c):\n",
    "  \"\"\"\n",
    "    This function calculates the solutions of a quadratic equation ...\n",
    "  \"\"\"\n",
    "  term = math.sqrt(b * b - 4 * a * c)\n",
    "  sol1 = (-b + term) / (2 * a)\n",
    "  sol2 = (-b - term) / (2 * a)\n",
    "\n",
    "  return sol1, sol2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "PwzQad_WwIcr",
    "outputId": "8b1796a5-10eb-4d5e-f644-6b37370045c1"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3.0, 1.0)"
      ]
     },
     "execution_count": 165,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "quad_sol(1, -4, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "l5pteSSpKPgI",
    "outputId": "72c4a0f3-b652-4325-e965-f0f982e29df7"
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "math domain error",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mValueError\u001b[39m                                Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[166]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mquad_sol\u001b[49m\u001b[43m(\u001b[49m\u001b[32;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[32;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[32;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[164]\u001b[39m\u001b[32m, line 8\u001b[39m, in \u001b[36mquad_sol\u001b[39m\u001b[34m(a, b, c)\u001b[39m\n\u001b[32m      4\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mquad_sol\u001b[39m(a, b, c):\n\u001b[32m      5\u001b[39m \u001b[38;5;250m  \u001b[39m\u001b[33;03m\"\"\"\u001b[39;00m\n\u001b[32m      6\u001b[39m \u001b[33;03m    This function calculates the solutions of a quadratic equation ...\u001b[39;00m\n\u001b[32m      7\u001b[39m \u001b[33;03m  \"\"\"\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m8\u001b[39m   term = \u001b[43mmath\u001b[49m\u001b[43m.\u001b[49m\u001b[43msqrt\u001b[49m\u001b[43m(\u001b[49m\u001b[43mb\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\u001b[43m \u001b[49m\u001b[43m-\u001b[49m\u001b[43m \u001b[49m\u001b[32;43m4\u001b[39;49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m \u001b[49m\u001b[43mc\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m      9\u001b[39m   sol1 = (-b + term) / (\u001b[32m2\u001b[39m * a)\n\u001b[32m     10\u001b[39m   sol2 = (-b - term) / (\u001b[32m2\u001b[39m * a)\n",
      "\u001b[31mValueError\u001b[39m: math domain error"
     ]
    }
   ],
   "source": [
    "quad_sol(1, 1, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "metadata": {
    "id": "VrW0O6pYwP6g"
   },
   "outputs": [],
   "source": [
    "def seq1():\n",
    "  a = [1, 2, 4, 7, 11, 16, 22, 29, 37, 46, 56, 67, 79, 94]\n",
    "  while True:\n",
    "    for e in a:\n",
    "      yield e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "uMUWtpww4v2z",
    "outputId": "ed1f05fa-7f1f-49c2-c506-c14ac7986ff4"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 4 7 11 16 22 29 37 46 56 67 79 94 1 2 4 7 11 16 22 29 37 46 56 67 79 94 1 2 "
     ]
    }
   ],
   "source": [
    "gen1 = seq1()\n",
    "for i in range(30):\n",
    "  print(next(gen1), end = ' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "metadata": {
    "id": "FQ095kra47ux"
   },
   "outputs": [],
   "source": [
    "def seq2():\n",
    "  a, d = 1, 1\n",
    "  while True:\n",
    "    yield a\n",
    "    a, d = a + d, d + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "tbiXkH9Q5Ufc",
    "outputId": "ad8e771d-2299-4579-bdb9-6c4507774bac"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 4 7 11 16 22 29 37 46 56 67 79 92 106 121 137 154 172 191 211 232 254 277 301 326 352 379 407 436 "
     ]
    }
   ],
   "source": [
    "gen2 = seq2()\n",
    "for i in range(30):\n",
    "  print(next(gen2), end = ' ')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "jte1vvgIKcCE"
   },
   "source": [
    "Write a function `find_sublist(a, b)` that returns `True` if and only if the list `b` appears somewhere in `a`. For example:\n",
    "* `find_sublist([-10, 2, 5, -2, 3], [2, 5])` should return `True`.\n",
    "* `find_sublist([-10, 2, 5, -2, 3], [2, 7])` should return `False`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {
    "id": "5i0D3i_O1Xpl"
   },
   "outputs": [],
   "source": [
    "def find_sublist(a, b):\n",
    "  for i in range(len(a)):\n",
    "    # Try to see if b appears starting at position i in a.\n",
    "    found = True\n",
    "    for j in range(len(b)):\n",
    "      if b[j] != a[i + j]:\n",
    "        found = False\n",
    "        break\n",
    "    if found:\n",
    "      return True\n",
    "  return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "iKxvI1coExps",
    "outputId": "0f46be8b-5873-4505-ee4e-0eb482833d8f"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 172,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [-10, 2, 5, -2, 3]\n",
    "b1 = [2, 5]\n",
    "b2 = [2, 5, -2]\n",
    "b3 = [2, 7]\n",
    "\n",
    "find_sublist(a, b1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "_uw5_fPME1IB",
    "outputId": "683db210-0538-42e6-d14f-d27bacaba39e"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 173,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "find_sublist(a, b2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "w_I0kk9fE29H",
    "outputId": "9b2b19f2-e463-437d-fa5d-6e1ab982e660"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 174,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "find_sublist(a, b3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "MhHQz2liLJ7C"
   },
   "source": [
    "The `for` loop in Python can have an `else` clause which gets executed if the loop ends normally."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "metadata": {
    "id": "ypXk7yW9E4rH"
   },
   "outputs": [],
   "source": [
    "def find_sublist(a, b):\n",
    "  for i in range(len(a)):\n",
    "    for j in range(len(b)):\n",
    "      if b[j] != a[i + j]:\n",
    "        break\n",
    "    else:\n",
    "      return True\n",
    "  return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "t1UROGmMGBR3",
    "outputId": "c6a9b233-1513-4877-d9d3-da8464f8f6fe"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, True, False)"
      ]
     },
     "execution_count": 176,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "find_sublist(a, b1), find_sublist(a, b2), find_sublist(a, b3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "zpaLk5wzLeVy"
   },
   "source": [
    "# Practice problems\n",
    "\n",
    "* Write a function `remove_duplicates(a)` that takes as input a sorted list and return a list where all duplicates are removed. For example, `remove_duplicates([1, 2, 2, 4, 6, 6, 6, 9, 10, 11, 11, 11, 11, 13, 14, 14])` should return `[1, 2, 4, 6, 9, 10, 11, 13, 14]`.\n",
    "\n",
    "* Write a function `remove_duplicates(a)` that removes duplicates from a list that is not necessarily sorted. For example, `remove_duplicates([-3, 4, 2, 4, -3, 2])` should return `[-3, 4, 2]`.\n",
    "\n",
    "* Write a function to find the longest common prefix string amongst an array of strings. For example, `longest-prefix([\"flower\",\"flow\",\"flight\"])` should return `'fl'`.\n",
    "\n",
    "* Generate a list of all permutations of the elements in a list. For example, `perm([1, 2, 3])` should output `[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 1, 3], [3, 1, 2], [3, 2, 1]]`.\n",
    "\n",
    "* Consider a pre-order tree representation using lists, where a tree T with the value `R` in the root node and subtrees T1, T2, ..., Tn is represented as a list `[R, T1 T2, ..., Tn]`. For example, [this tree](https://webpages.charlotte.edu/rbunescu/courses/itcs5356/tree.svg) would be represented as `[2, [7, [2], [10], [6, [5], [11]]], [5, [9, [4]]]]`. Write the functions:\n",
    "  - `count_nodes(t)` that count the nodes of a tree. For the tree above it should return `10`.\n",
    "  - `count_leaves(t)` that counts the leaves of a tree. For the tree above it should return `5`.\n",
    "  - `height(t)` that calculates the height of a tree. The the tree above it should return `3`.\n",
    "  - `find(t, x)` that returns `Treu` if and only if t contains the number `x`."
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "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.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
