{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6b4fce18-128e-441d-9040-7d738b1ac1c8",
   "metadata": {},
   "source": [
    "## PDSP 2025, Lecture 05, 21 August 2025"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79fe9ec3-2cd3-4d6b-b757-138dc4960978",
   "metadata": {},
   "source": [
    "### Conditional statement\n",
    "`if` allows conditional execution\n",
    "\n",
    "```\n",
    "if condition:\n",
    "    statement 1\n",
    "    ...\n",
    "    statement k\n",
    "else:\n",
    "    statement 1'\n",
    "    ...\n",
    "    statement k'\n",
    "```\n",
    "- If `condition` evaluates to `True`, the first block is executed, otherwise the second block.\n",
    "- The `else:` block is optional. If there is no `else:` block and the `condition` evaluates to `False`, execution skips over to the next statement after the `if`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f046bee2-92fe-4e2e-8deb-3248e86ed371",
   "metadata": {},
   "source": [
    "- Example: Compute the absolute value of a number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "24f50487-f9c8-4bf9-919b-c887a3d31462",
   "metadata": {},
   "outputs": [],
   "source": [
    "def myabs(x):  # myabs to avoid any confusion with built-in abs()\n",
    "    if x < 0:\n",
    "        return(-x)\n",
    "    else:\n",
    "        return(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e187eacf-c2bf-47c4-921b-032c2bba7533",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(9, 7)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myabs(-9), myabs(7)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "596eb09e-0a05-4581-9bc4-a231a220f17d",
   "metadata": {},
   "source": [
    "### Multiway branching --- `elif`\n",
    "\n",
    "Suppose we want to compute $sign(x) = \\begin{cases}\n",
    "                                x < 0 & = & -1,\\\\\n",
    "                                x = 0 & = & 0,\\\\\n",
    "                                x > 0 & = & 1\n",
    "\\end{cases}$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "677afd4c-c594-4d94-b3d6-a3a6747ad95a",
   "metadata": {},
   "source": [
    "In Python, we would have to nest `if` statements like this:\n",
    "```\n",
    "if x < 0:\n",
    "    return(-1)\n",
    "else:\n",
    "    if x == 0:\n",
    "        return(0):\n",
    "    else:\n",
    "        return(1)\n",
    "```\n",
    "- As we see, the indentation of the nested `if` pushes the code to the right\n",
    "- With more cases, this would become worse\n",
    "- Python provides `elif` to avoid this cascaded nesting\n",
    "\n",
    "```\n",
    "if x < 0:\n",
    "    return(-1)\n",
    "elif x == 0:\n",
    "    return(0):\n",
    "else:\n",
    "    return(1)\n",
    "```\n",
    "- Can have as many `elif` blocks as you need\n",
    "- `else` is still optional"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "253f2c14-2cfe-4feb-8cb6-2dc9ac66d2ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "def sign(x):\n",
    "    if x < 0:\n",
    "        return(-1)\n",
    "    elif x == 0:\n",
    "        return(0)\n",
    "    else:\n",
    "        return(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d84b5a77-d385-40b1-87c9-80d3efdfa5b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sign(-7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8d6fe007-1c7a-4353-8c38-2af16cad8a80",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sign(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bfc807fa-f603-48fa-9857-a09be9f52ab3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sign(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4aa3ced2-2f5e-4435-b9fb-5ecc8e80ace6",
   "metadata": {},
   "source": [
    "### Lists\n",
    "- Sequences of values, indexed by position\n",
    "- For a list with `n` values, valid positions are `0` to `n-1`\n",
    "    - `len(l)` gives the length of a list\n",
    "- Accessing a position beyond `len(l)-1` results in `IndexError`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bafaf95d-a547-4a85-9204-f5b887886d65",
   "metadata": {},
   "outputs": [],
   "source": [
    "l = list(range(20,40))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5512bba4-f07f-4e78-9162-26251cf9d284",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(20, 23, 39)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(l), l[3], l[19]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "354250d2-89e2-41d3-974e-a006b9e87367",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mIndexError\u001b[39m                                Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[9]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43ml\u001b[49m\u001b[43m[\u001b[49m\u001b[32;43m20\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[31mIndexError\u001b[39m: list index out of range"
     ]
    }
   ],
   "source": [
    "l[20]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bdb0f59-f594-43f9-af16-4cb925aef827",
   "metadata": {},
   "source": [
    "- What about indices below `0`?\n",
    "- Index `-j` is interpreted as `len(l)-j`\n",
    "    - Useful for accessing values from the end of the list\n",
    "    - Valid indices in reverse are `-1`, `-2`, ..., `-len(l)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6838faf7-f93d-4603-8330-02f6520592b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(39, 20)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l[-1], l[-20]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8dbe1201-3d56-4abf-889d-319c5ffed710",
   "metadata": {},
   "source": [
    "### Slices\n",
    "- Recall that `nprimes(n)` computed the first `n` primes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3e52482b-01df-4949-b560-8fe712ae0bf7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def isprime(n):\n",
    "    for j in range(2,n):\n",
    "        if n % j == 0:\n",
    "            return(False)\n",
    "    return(True)\n",
    "\n",
    "def nprimes(n):\n",
    "    plist = []\n",
    "    j = 2\n",
    "    while (len(plist) < n):\n",
    "        if isprime(j):\n",
    "            plist.append(j)\n",
    "        j = j+1\n",
    "    return(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "300baf8c-548a-4c5b-910d-8d5195ff2c13",
   "metadata": {},
   "outputs": [],
   "source": [
    "first20primes = nprimes(20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "09827cba-4c1f-4341-8957-6b9534ebf99e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first20primes"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0458775d-1b7e-40b5-a7b8-43d301a0a1fc",
   "metadata": {},
   "source": [
    "- What are the primes from 11 to 15?\n",
    "- Need a sublist of the original list\n",
    "- `l[i:j]` is the list `[l[i], l[[i+1], ..., l[j-1]]`\n",
    "- Similar to\n",
    "  ```\n",
    "  newl = []\n",
    "  for k in range(i,j):\n",
    "     newl.append(k)\n",
    "  ```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "cfec7a4f-12d0-4649-af9c-e1da6fd3c835",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[37, 41, 43, 47, 53]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first20primes[11:16]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae97a899-5c3f-4688-a626-3e4b03b0e69d",
   "metadata": {},
   "source": [
    "- like `range()` if the indices don't make sense, you get an empty list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "481ebf33-abd9-4553-9a6f-72c89666b16b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first20primes[11:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "717be12f-1cc5-467c-a6dd-5c1e6fcbf616",
   "metadata": {},
   "source": [
    "- Unlike accessing `l[i]`, can give upper bound beyond the list\n",
    "- `l[i:len(l)+10]` is interpreted as `l[i:len(l)]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d659a3ce-0a6c-4712-8fe2-9600ccaea5f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[37, 41, 43, 47, 53, 59, 61, 67, 71]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first20primes[11:40]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4e9080f-3e3a-49ca-87a4-8a4139cd6077",
   "metadata": {},
   "source": [
    "- Can omit the upper bound, defaults to `len(l)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3719ebac-0f22-4017-932e-9c7cbfe80b98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[53, 59, 61, 67, 71]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first20primes[15:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8785f2f-59f6-44db-a5fb-ccc257aa88b8",
   "metadata": {},
   "source": [
    "- Likewise, omit the lower bound, defaults to `0`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "045ab934-6f7b-4446-a1f2-28807c43804a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first20primes[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "744a56eb-fde4-41bb-afcf-f7dae5828ee1",
   "metadata": {},
   "source": [
    "- Omit both lower and upper bound to get a `full slice`\n",
    "    - Full slice returns a new list that is a copy of the list\n",
    "    - Significance will become clearer later"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a4faa69f-8d87-4964-8796-807150cb88fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first20primes[:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0aa73a52-4791-4782-9208-77cd277207d9",
   "metadata": {},
   "source": [
    "### More about `range()`\n",
    "- `range(i,j)` generates the sequence `i,i+1,...,j-1`\n",
    "- `range(n)` generates the sequence `0,1,...,n-1` -- implicitly starts with `0`\n",
    "- What if we want to skip over some numbers\n",
    "    - All even numbers from `4` to `40`\n",
    "- Optional third argument is the step size\n",
    "    - `range(i,j,k)` is `i,i+k,...,i+mk` for the largest `m` such that `i+mk < j` and `i+(m+1)k >= j`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f2ab16e0-7a31-438c-9f7f-b3b1c46906ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(4,41,2)) # Even numbers from 4 to 40"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ce51aa70-ee82-4392-8c3f-c1228249ec75",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(4,40,2)) # Even numbers from 4 to 38"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b65dd7d-da94-45c5-ab25-310317ad1090",
   "metadata": {},
   "source": [
    "- Can also count down -- give a negative step!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ca8d8dab-3f16-408f-ae73-484f4de52a0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(10,0,-1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccbdb6e5-3de4-411f-9645-b60aaee3bc17",
   "metadata": {},
   "source": [
    "- `range(i,j,k)` generate `i, i+k,...` so that the sequence does not *cross* `j`\n",
    "- Depending on whether it is increasing or decreasing, the last value will be less than `j` or greater than `j`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa7f15c3-a6ff-44c5-82c0-3c90aca306f9",
   "metadata": {},
   "source": [
    "### Stepped slices\n",
    "- Can similarly give a third argument in a slice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "5267700f-c7a2-445f-bfb7-53b3793e7ad5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 13, 23, 37, 47, 61]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first20primes[2:20:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "82ba70be-8956-4614-964a-c4666a6cbd85",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first20primes[19:0:-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "128a21d6-92bf-4d07-8e12-e5cd8294a20e",
   "metadata": {},
   "source": [
    "- Explain the following output. (Hint, what is `l[-1]`?)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1a1319aa-d681-4355-aa21-47bc33aa8132",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first20primes[19:-1:-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3397778-f929-4143-ad87-35a6cd65c7dd",
   "metadata": {},
   "source": [
    "- Can omit upper and lower bounds but give a step\n",
    "- `l[::-1]` is the entire list in reverse\n",
    "    - Note that the default lower and upper bound are determined by the step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b28ca6d6-e8a7-489d-8e57-b411900bdbf3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first20primes[::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8db56dc6-195c-4f71-a079-eed6c9e81036",
   "metadata": {},
   "source": [
    "### Assigning slices\n",
    "- Can assign a list to a slice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b8f1b368-6491-47db-a751-f50aa9e909dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "l = list(range(20,30))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f77059e6-6470-4a7b-8fd6-386151facfd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "l[3:6] = [53,54,55]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f836827e-5f81-4d08-a0f8-851a0623ca55",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20, 21, 22, 53, 54, 55, 26, 27, 28, 29]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5edfae4-1905-40cb-9d1d-50adfde2f8e8",
   "metadata": {},
   "source": [
    "- Can contract or expand the slice when reassigning\n",
    "- Indices of values to the right will change"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "c66e57b8-432d-4858-bed1-e9d322e30ebf",
   "metadata": {},
   "outputs": [],
   "source": [
    "l = list(range(20,30))\n",
    "l[3:5] = [53,54,55,63,64,65]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "1112570d-92f2-4491-8dac-b4ddca8f2301",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20, 21, 22, 53, 54, 55, 63, 64, 65, 25, 26, 27, 28, 29]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "e1f858b1-0cf4-431e-ac98-4ef8cb32733b",
   "metadata": {},
   "outputs": [],
   "source": [
    "l = list(range(20,30))\n",
    "l[3:5] = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "a40fbfd2-22ba-48a2-9e0b-3c9cb596e5c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20, 21, 22, 25, 26, 27, 28, 29]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1cd4ab6c-71b3-4588-8cb9-28efb57650b4",
   "metadata": {},
   "source": [
    "### Operations on lists\n",
    "- Recall that `+` concatenates two lists\n",
    "- Returns a new list\n",
    "- Original lists are unchanged"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "fc142899-ddb8-47c7-948b-5646b44accb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "l1 = [1,2,3]\n",
    "l2 = [4,5,6]\n",
    "l3 = l1 + l2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "bd7dc9a5-d0c3-4ffa-ac1f-d763a6a7f1a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([1, 2, 3, 4, 5, 6], [1, 2, 3], [4, 5, 6])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l3, l1, l2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2952a2f9-44e0-451a-a346-de815cbf699e",
   "metadata": {},
   "source": [
    "- A useful invariant about slices\n",
    "- For any list `l`, and any integer `j`, `l == l[:j] + l[j:]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "22c62563-bccb-4e72-ba93-a677e5da263f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l3[:-1]+l3[-1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "1a4bf1ec-9c05-47c7-99e8-e14cb258e08b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l3[:2]+l3[2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "71444fdf-97eb-44b4-9faa-a3fe25d4b949",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l3[:9]+l3[9:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ac1ae66-7917-451b-8f0e-562ce21adbf5",
   "metadata": {},
   "source": [
    "### Applying functions to lists\n",
    "- `l.append(v)` is the same as `l = l+[v]`\n",
    "    - Ask the list `l` to append `v` to itself\n",
    "    - `l.append(v)` updates `l` in place\n",
    "    - `l = l+[v]` creates a new list and reassigns the list pointed to by `l`\n",
    "    - Again, we will see the significance of this later"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "7139d381-9939-4e17-8d9c-923bc2710627",
   "metadata": {},
   "outputs": [],
   "source": [
    "l3.append(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "9ad795a7-ee96-4b6d-90fe-c007cfe61422",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "957db5db-312f-429a-b822-cc58eb137776",
   "metadata": {},
   "source": [
    "- It is a mistake to reassign a list after an `append()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "4ee47fb3-8725-444e-8515-fb3495cded35",
   "metadata": {},
   "outputs": [],
   "source": [
    "l3 = l3.append(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "96415768-cfac-4229-9894-7bea7d09e680",
   "metadata": {},
   "outputs": [],
   "source": [
    "l3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bc1ac54-f128-4c61-983a-77111c2cb316",
   "metadata": {},
   "source": [
    "- Assignment `v = e` stores return value of `e` in `v`\n",
    "- Return value of `l.append()` is empty"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c745bf25-1146-4e9b-9f3e-645e3d5d8290",
   "metadata": {},
   "source": [
    "### Other functions\n",
    "- `l.insert(pos,val)` inserts `val` at position `p`\n",
    "    - Similar to `l = l[:pos] + [val] + l[pos:]`\n",
    "- `l.extend(newl)` extends `l` with a list of values `newl`\n",
    "    - Similar to `l = l + newl`\n",
    "- Like `l.append(v)`, these update the list in place, do not reassign return value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "cf3246e0-0e2f-4eec-9d2e-b3b915475906",
   "metadata": {},
   "outputs": [],
   "source": [
    "l3 = [1,2,3,4,5,6,7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "8f4e636e-3fc6-4a3d-a5fd-9540527d80d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "l3.insert(0,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "96d27977-b85a-405d-b25e-4c833d96cb8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "0df6a902-94c6-40b4-8c2c-a8ef071835ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "l3.extend([8,9,10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "027dabe6-4f10-4245-b6e6-c2069d3b4df7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f23ba46d-225b-4c6b-ab6c-908a1914b752",
   "metadata": {},
   "source": [
    "### Sorting\n",
    "- `l.sort()` sorts a list in place\n",
    "    - Python allows lists of mixed types\n",
    "    - To sort a list, the values must be of a uniform comparable type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "69f18f4d-e7b7-46f8-bc35-4a43fd7449b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "l = [1,15,3,7,9,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "02150f71-b277-433b-b385-7f6f4e53c8b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "l.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "744c7983-b0ee-4fda-b706-5343eb107ca5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 7, 9, 15]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "ee24fa5f-bf49-482e-beb0-0b7f93c9a569",
   "metadata": {},
   "outputs": [],
   "source": [
    "badl = [1,'CSK',True,7.5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "1390825b-c99f-40c1-9e6f-3f3bcb943dec",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'<' not supported between instances of 'str' and 'int'",
     "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[52]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mbadl\u001b[49m\u001b[43m.\u001b[49m\u001b[43msort\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[31mTypeError\u001b[39m: '<' not supported between instances of 'str' and 'int'"
     ]
    }
   ],
   "source": [
    "badl.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "05e241ea-eef8-441e-8e15-a56c8e5c8fe8",
   "metadata": {},
   "outputs": [],
   "source": [
    "blist = [True,False]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "c293171c-dd4e-4ecd-b21a-a25e60b258db",
   "metadata": {},
   "outputs": [],
   "source": [
    "blist.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "5283b0ae-576e-442c-b806-e757f8defeee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[False, True]"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "blist"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85cb3e38-96a7-4203-976d-c3dc055e0869",
   "metadata": {},
   "source": [
    "- If you want a sorted copy of `l` without disturbing `l`, use `sorted(l)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "a74af192-668a-49f2-a426-da34a51c8961",
   "metadata": {},
   "outputs": [],
   "source": [
    "l = [15, 1000, 9, 7, 3, 2, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "b521258a-900b-4029-baf5-b4f373561856",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[15, 1000, 9, 7, 3, 2, 1]"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "d693aac6-89a3-4d7f-810f-804da852e163",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 7, 9, 15, 1000]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "222fe345-ff37-48e5-8339-099a353b1f26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[15, 1000, 9, 7, 3, 2, 1]"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f68ffdf-f831-462f-a927-5128160d0a06",
   "metadata": {},
   "source": [
    "- Can store a copy of the sorted list in another list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "950b1003-8364-435e-9891-4a23c2e5a890",
   "metadata": {},
   "outputs": [],
   "source": [
    "newl = sorted(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "0504e907-1a74-4638-8b1b-6e2927c158c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([1, 2, 3, 7, 9, 15, 1000], [15, 1000, 9, 7, 3, 2, 1])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "newl, l"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84cd61bd-7e12-4a92-887a-ff2f015030a0",
   "metadata": {},
   "source": [
    "- Can we, instead, first copy `l` and sort the copy in place using `sort()`?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "b24ae2d5-9684-4f5f-b530-f1104638a3f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "newl = l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "866039f6-dabd-42c8-b2b5-b2d9d6071692",
   "metadata": {},
   "outputs": [],
   "source": [
    "newl.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "83db5d74-5fda-4cfe-a98f-1b988fcb00d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 7, 9, 15, 1000]"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "newl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "76b35931-5aa3-4f9c-8b30-3bc5793eea1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 7, 9, 15, 1000]"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "165b960e-9290-4c0b-9c25-b912655e45ee",
   "metadata": {},
   "source": [
    "- Sorting `newl` also sorts `l`\n",
    "- This does not happen with types like `int`\n",
    "- We will investigate this later"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "6fb19b32-d956-4399-90dc-96012b1b846a",
   "metadata": {},
   "outputs": [],
   "source": [
    "y = 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "4710d04a-0aee-4d04-bcfb-2828de25d8e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "af0f438e-0902-4ce8-a1d5-f347f6d96cf7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(7, 7)"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "bac0302c-7acf-4bb1-96fd-17d1457495e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 17"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "42751cd4-5fb7-4fec-9cfe-e8a783df886e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(17, 7)"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec6b37fd-1af2-476d-804d-0353727adf24",
   "metadata": {},
   "source": [
    "- Many other built-in functions on lists\n",
    "    - `l.reverse()` reverses a list\n",
    "- Look up Python documentation"
   ]
  }
 ],
 "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.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
