{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6b4fce18-128e-441d-9040-7d738b1ac1c8",
   "metadata": {},
   "source": [
    "## PDSP 2025, Lecture 06, 26 August 2025"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2d1ff58-8e57-4c72-a188-e008e7c2620d",
   "metadata": {},
   "source": [
    "### Tuples\n",
    "- Sequence of values in round brackets - `(v1,v2,...,vk)`\n",
    "- Typically values are not of a uniform type\n",
    "    - Collect together different attributes of an item\n",
    "    - Row in a table; each column is an attribute"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d969e37e-b4e4-4239-9313-4ef4149b3c16",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = (\"Visakhapatnam\",\"DC\",\"CSK\",\"DC\",\"DC\",192)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "959d64f6-cd8f-4418-a6de-704b459de16e",
   "metadata": {},
   "source": [
    "- Positional indexing, slicing like other sequences"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f2832007-d080-4992-af70-5e1e6e075aff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('DC', 192)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[4],t[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "041c3bda-a992-4f2b-9aee-e2dc7de62521",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('DC', 'CSK', 'DC')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[1:4]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3667d30-26a3-4d4a-8dc1-87564af8737b",
   "metadata": {},
   "source": [
    "- Iterate over a tuple\n",
    "    - `print()` prints its arguments --- either a message (string) or value of a variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "44f8baa0-6627-4a00-b49c-4db3a126f359",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x is Visakhapatnam , to repeat Visakhapatnam\n",
      "x is DC , to repeat DC\n",
      "x is CSK , to repeat CSK\n",
      "x is DC , to repeat DC\n",
      "x is DC , to repeat DC\n",
      "x is 192 , to repeat 192\n"
     ]
    }
   ],
   "source": [
    "for x in t:\n",
    "    print(\"x is\", x, \", to repeat\", x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "44d20a92-d9d1-4ebf-8888-97ff99bbe4f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(False, True)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'RR' in t, 'DC' in t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "259d6eb8-2d40-47a3-b666-1a6059fc6992",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Visakhapatnam', 'DC', 'CSK', 'DC', 'DC', 192)\n"
     ]
    }
   ],
   "source": [
    "print(t)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67388361-4a88-419c-9f40-c044bcbfc5cc",
   "metadata": {},
   "source": [
    "- Unlike lists, cannot update a component of a tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "645d0ae7-870c-4493-ab9f-0e13520be38a",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = (\"Visakhapatnam\",\"DC\",\"CSK\",\"DC\",\"DC\",192) # Change Team 2 to 'RR' from 'CSK'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ceb057a1-23b0-4f72-87b7-242b17f70485",
   "metadata": {},
   "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[8]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mt\u001b[49m\u001b[43m[\u001b[49m\u001b[32;43m2\u001b[39;49m\u001b[43m]\u001b[49m = \u001b[33m'\u001b[39m\u001b[33mRR\u001b[39m\u001b[33m'\u001b[39m\n",
      "\u001b[31mTypeError\u001b[39m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "t[2] = 'RR'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "632b6a51-fc87-4acd-881b-fca230c38ac2",
   "metadata": {},
   "source": [
    "- Can concatenate tuples using `+`\n",
    "    - Update a tuple by assembling a new tuple\n",
    "- Be careful, insert a comma to indicate a singleton tuple: `(v,)` vs `(v)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "566d18ff-cffc-489a-b067-8f05db5ed89b",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can only concatenate tuple (not \"str\") to tuple",
     "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[9]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m u = \u001b[43mt\u001b[49m\u001b[43m[\u001b[49m\u001b[32;43m0\u001b[39;49m\u001b[43m:\u001b[49m\u001b[32;43m2\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m+\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mRR\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m)\u001b[49m+t[\u001b[32m3\u001b[39m:]  \u001b[38;5;66;03m# No difference between 'RR' and ('RR')\u001b[39;00m\n",
      "\u001b[31mTypeError\u001b[39m: can only concatenate tuple (not \"str\") to tuple"
     ]
    }
   ],
   "source": [
    "u = t[0:2]+('RR')+t[3:]  # No difference between 'RR' and ('RR')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ce6d4189",
   "metadata": {},
   "outputs": [],
   "source": [
    "u = t[0:2]+('RR',)+t[3:]  # ('RR',) is recognized a singleton tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9d758bde-80e6-4065-8976-d58b858c5b43",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Visakhapatnam', 'DC', 'RR', 'DC', 'DC', 192)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b555caf-5720-41d6-be4f-1d0e5e7ce30e",
   "metadata": {},
   "source": [
    "- Can use `list()` to convert a tuple to a list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5580f876-a0bd-47d4-b8dd-4b38aecfec6a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Visakhapatnam', 'DC', 'CSK', 'DC', 'DC', 192]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(t)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f819a994-8131-45d2-b50f-490659860ab5",
   "metadata": {},
   "source": [
    "- In general `list()` works provided its argument is a sequence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a9173066-b456-4623-a783-3a400922e0d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "375ff5a6-cd06-49ef-9aa1-e324ee77daa3",
   "metadata": {},
   "source": [
    "- If the argument is not a sequence, `list()` generates an error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4489aa3f-5a8d-4513-af34-80703135aa2e",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'int' object is not iterable",
     "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[14]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;43mlist\u001b[39;49m\u001b[43m(\u001b[49m\u001b[32;43m7\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[31mTypeError\u001b[39m: 'int' object is not iterable"
     ]
    }
   ],
   "source": [
    "list(7)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc5e737c-5cb2-4e89-a2b2-bdcaca8041a3",
   "metadata": {},
   "source": [
    "- Can assign a tuple of variables in one shot\n",
    "    - Useful for initialising multiple quantities\n",
    "    - In `nprimes()` we started with `primelist = []` and `p = 2`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2d004d9e-36db-404a-9ac2-718f563aed23",
   "metadata": {},
   "outputs": [],
   "source": [
    "(primelist,p) = ([],2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "cd70598e-83f7-476a-a236-1c76502388bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Equivalent to\n",
    "primelist = []\n",
    "p = 2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e723758d-3712-4b81-a5e3-5f56ebd1624b",
   "metadata": {},
   "source": [
    "- `(x,y) = (y,x)` swaps the values of `x` and `y`\n",
    "    - All values on rhs are old values\n",
    "    - All values on lhs are new assignments\n",
    "    - Cannot be done sequentially\n",
    "        - Not equivalent to `x = y` followed by `y = x` or vice versa\n",
    "- Normally, swap requires a temporary variable\n",
    "```\n",
    "t = y\n",
    "y = x\n",
    "x = t\n",
    "```\n",
    "- Imagine exchanging the contents of a glass of juice and a glass of milk\n",
    "    - Need a third empty glass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "74bab3af-dd9d-4e07-b110-f3b470fadfee",
   "metadata": {},
   "outputs": [],
   "source": [
    "(x,y) = (5,[])\n",
    "(x,y) = (y,x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "6832c415-13a2-41f8-a17b-24e1666b7c36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([], 5)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x,y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eac658e2-7973-4c4f-bcf8-95105e41f2a9",
   "metadata": {},
   "source": [
    "- When we say `x,y` we mean `(x,y)` --- brackets may be omitted\n",
    "- Python inserts them to display the value to us"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "dd861b45-d9f6-4d23-aa5b-9224f153dacd",
   "metadata": {},
   "outputs": [],
   "source": [
    "x,y = 5,[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fd074383-6256-45ce-86cc-8158ec311379",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, [])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x,y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b097078-2da4-4f18-9f0e-ff16d591e9bb",
   "metadata": {},
   "source": [
    "### Dictionaries\n",
    "- A list is a collection indexed by position \n",
    "- A list can be thought of as a function $f: \\{0,1,\\ldots,n-1\\} \\to \\{v_0,v_1,\\ldots,v_{n-1}\\}$\n",
    "  - A list maps positions to values\n",
    "- Generalize this to a function $f: \\{k_0,k_1,\\ldots,k_{n-1}\\} \\to \\{v_0,v_1,\\ldots,v_{n-1}\\}$\n",
    "  - Instead of positions, index by an abstract *key*\n",
    "- **dictionary**: maps keys, rather than positions, to values\n",
    "- Notation:\n",
    "  - `d = {k1:v1, k2:v2}`, enumerate a dictionary explicitly\n",
    "  - `d[k1]`, value in dictionary `d1` corresponding to key `k1`\n",
    "  - `{}`, empty dictionary (`[]` for lists, `()` for tuples)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d8fc9e79-badf-4882-ac76-f1f3f8f377fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "d = {'a':1,'b':17,'c':0}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "bef0ef37-d2ea-4b0d-9a69-b466d9191575",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d['b']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "7fa68be6-5640-4943-b725-04d403fae808",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'d'",
     "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[23]\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;43md\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m]\u001b[49m  \u001b[38;5;66;03m# Invalid key\u001b[39;00m\n",
      "\u001b[31mKeyError\u001b[39m: 'd'"
     ]
    }
   ],
   "source": [
    "d['d']  # Invalid key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "bf5a300b-7f80-4942-a030-b856cee1b545",
   "metadata": {},
   "outputs": [],
   "source": [
    "d['d'] = 17"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "ed814d6f-c80f-48b1-9840-b1137544108f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d['d']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d45aada7-b69f-45a8-9476-71f22e9e5039",
   "metadata": {},
   "source": [
    "- An assignment `d[k] = v` serves two purposes\n",
    "    - If there is no key `k`, create the key and assign it the value `v`\n",
    "    - If there is already a key `k`, replace its current value by `v`\n",
    "- In a list, we cannot create a value at a new position through an assignment\n",
    "    - If `l` is `[0,1,2,3]`, `l[4] = 4` generate `IndexError`\n",
    "    - If `d = {'a':1,'b':17,'c':0}`, `d['d'] = 19` extends `d` with a new key-value pair"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e34224d1-dc91-4e8c-bcb3-a3dfa8d89d8f",
   "metadata": {},
   "source": [
    "**Iteration**\n",
    "- `d.keys()` generates a sequence of all keys in `d`\n",
    "    - Iterate over keys using `for k in d.keys():`\n",
    "    - `for k in d:` also works --- `d` is implicitly intepreted as `d.keys()`\n",
    "    - Though the keys do not form a sequence, Python will generate them in the order in which they were created\n",
    "- Similarly, `d.values()` is the sequence of values present\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "a1aa6272-8938-41c8-ac6b-f2f58825f7a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "d = {'a':1,'b':17,'c':0}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "0383ea4d-5073-45a8-b645-955b9544207b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['a', 'b', 'c'], [1, 17, 0])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(d.keys()), list(d.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "38ea0aed-88ca-41a3-bfc0-3b64e54c1677",
   "metadata": {},
   "outputs": [],
   "source": [
    "d = {'b':17,'c':0,'a':1}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ef844f01-bbfb-43ba-8f72-97a3b85c2e79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['b', 'c', 'a'], [17, 0, 1])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(d.keys()), list(d.values())"
   ]
  }
 ],
 "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
}
