{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6b4fce18-128e-441d-9040-7d738b1ac1c8",
   "metadata": {},
   "source": [
    "## PDSP 2025, Lecture 08, 2 September 2025"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "312612ad-cbb3-4871-91b2-9e9a8165831b",
   "metadata": {},
   "source": [
    "### Nested collections\n",
    "- List of lists, list of tuples, dictionary whose values are lists ...\n",
    "- `matchlist` is a list of tuples\n",
    "- Use two indices to extract a value\n",
    "    - `matchlist[3]` is `(\"Jaipur\",\"RR\",\"LSG\",\"RR\",\"RR\",194)`\n",
    "    - `matchlist[3][1]` is `\"RR\"`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "90dd963d-bf0a-4fa5-963d-1f79a418243d",
   "metadata": {},
   "outputs": [],
   "source": [
    "matchlist = [\n",
    "    (\"Chennai\",\"RCB\",\"CSK\",\"RCB\",\"CSK\",174),\n",
    "    (\"Mohali\",\"DC\",\"PK\",\"PK\",\"PK\",175),\n",
    "    (\"Kolkata\",\"KKR\",\"SRH\",\"SRH\",\"KKR\",209),\n",
    "    (\"Jaipur\",\"RR\",\"LSG\",\"RR\",\"RR\",194),\n",
    "    (\"Ahmedabad\",\"GT\",\"MI\",\"MI\",\"GT\",169),\n",
    "    (\"Bengaluru\",\"PK\",\"RCB\",\"RCB\",\"RCB\",177),\n",
    "    (\"Chennai\",\"CSK\",\"GT\",\"GT\",\"CSK\",207),\n",
    "    (\"Hyderabad\",\"SRH\",\"MI\",\"MI\",\"SRH\",278),\n",
    "    (\"Jaipur\",\"RR\",\"DC\",\"DC\",\"RR\",186),\n",
    "    (\"Bengaluru\",\"RCB\",\"KKR\",\"KKR\",\"KKR\",183),\n",
    "    (\"Lucknow\",\"LSG\",\"PK\",\"LSG\",\"LSG\",200),\n",
    "    (\"Ahmedabad\",\"SRH\",\"GT\",\"SRH\",\"GT\",163),\n",
    "    (\"Visakhapatnam\",\"DC\",\"CSK\",\"DC\",\"DC\",192),\n",
    "    (\"Mumbai\",\"MI\",\"RR\",\"RR\",\"RR\",126),\n",
    "    (\"Bengaluru\",\"LSG\",\"RCB\",\"RCB\",\"LSG\",182),\n",
    "    (\"Visakhapatnam\",\"KKR\",\"DC\",\"KKR\",\"KKR\",273),\n",
    "    (\"Ahmedabad\",\"GT\",\"PK\",\"PK\",\"PK\",200),\n",
    "    (\"Hyderabad\",\"CSK\",\"SRH\",\"SRH\",\"SRH\",166),\n",
    "    (\"Jaipur\",\"RCB\",\"RR\",\"RR\",\"RR\",184),\n",
    "    (\"Mumbai\",\"MI\",\"DC\",\"DC\",\"MI\",235),\n",
    "    (\"Lucknow\",\"LSG\",\"GT\",\"LSG\",\"LSG\",164),\n",
    "    (\"Chennai\",\"KKR\",\"CSK\",\"CSK\",\"CSK\",138),\n",
    "    (\"Mohali\",\"SRH\",\"PK\",\"PK\",\"SRH\",183),\n",
    "    (\"Jaipur\",\"RR\",\"GT\",\"GT\",\"GT\",197),\n",
    "    (\"Mumbai\",\"RCB\",\"MI\",\"MI\",\"MI\",197),\n",
    "    (\"Lucknow\",\"LSG\",\"DC\",\"LSG\",\"DC\",168),\n",
    "    (\"Mohali\",\"PK\",\"RR\",\"RR\",\"RR\",148),\n",
    "    (\"Kolkata\",\"LSG\",\"KKR\",\"KKR\",\"KKR\",162),\n",
    "    (\"Mumbai\",\"CSK\",\"MI\",\"MI\",\"CSK\",207),\n",
    "    (\"Bengaluru\",\"SRH\",\"RCB\",\"RCB\",\"SRH\",288),\n",
    "    (\"Kolkata\",\"KKR\",\"RR\",\"RR\",\"RR\",224),\n",
    "    (\"Ahmedabad\",\"GT\",\"DC\",\"DC\",\"DC\",90),\n",
    "    (\"Mohali\",\"MI\",\"PK\",\"PK\",\"MI\",193),\n",
    "    (\"Lucknow\",\"CSK\",\"LSG\",\"LSG\",\"LSG\",177),\n",
    "    (\"Delhi\",\"SRH\",\"DC\",\"DC\",\"SRH\",267),\n",
    "    (\"Kolkata\",\"KKR\",\"RCB\",\"RCB\",\"KKR\",223),\n",
    "    (\"Mohali\",\"PK\",\"GT\",\"PK\",\"GT\",143),\n",
    "    (\"Jaipur\",\"MI\",\"RR\",\"MI\",\"RR\",180),\n",
    "    (\"Chennai\",\"CSK\",\"LSG\",\"LSG\",\"LSG\",211),\n",
    "    (\"Delhi\",\"DC\",\"GT\",\"GT\",\"DC\",225),\n",
    "    (\"Hyderabad\",\"RCB\",\"SRH\",\"RCB\",\"RCB\",207),\n",
    "    (\"Kolkata\",\"KKR\",\"PK\",\"PK\",\"PK\",262),\n",
    "    (\"Delhi\",\"DC\",\"MI\",\"MI\",\"DC\",258),\n",
    "    (\"Lucknow\",\"LSG\",\"RR\",\"RR\",\"RR\",197),\n",
    "    (\"Ahmedabad\",\"GT\",\"RCB\",\"RCB\",\"RCB\",201),\n",
    "    (\"Chennai\",\"CSK\",\"SRH\",\"SRH\",\"CSK\",213),\n",
    "    (\"Kolkata\",\"DC\",\"KKR\",\"DC\",\"KKR\",154),\n",
    "    (\"Lucknow\",\"MI\",\"LSG\",\"LSG\",\"LSG\",145),\n",
    "    (\"Chennai\",\"CSK\",\"PK\",\"PK\",\"PK\",163),\n",
    "    (\"Hyderabad\",\"SRH\",\"RR\",\"SRH\",\"SRH\",202),\n",
    "    (\"Mumbai\",\"KKR\",\"MI\",\"MI\",\"KKR\",170),\n",
    "    (\"Bengaluru\",\"GT\",\"RCB\",\"RCB\",\"RCB\",148),\n",
    "    (\"Dharamsala\",\"CSK\",\"PK\",\"PK\",\"CSK\",168),\n",
    "    (\"Lucknow\",\"KKR\",\"LSG\",\"LSG\",\"KKR\",236),\n",
    "    (\"Mumbai\",\"SRH\",\"MI\",\"MI\",\"MI\",174),\n",
    "    (\"Delhi\",\"DC\",\"RR\",\"RR\",\"DC\",222),\n",
    "    (\"Hyderabad\",\"LSG\",\"SRH\",\"LSG\",\"SRH\",166),\n",
    "    (\"Dharamsala\",\"RCB\",\"PK\",\"PK\",\"RCB\",242),\n",
    "    (\"Ahmedabad\",\"GT\",\"CSK\",\"CSK\",\"GT\",232),\n",
    "    (\"Kolkata\",\"KKR\",\"MI\",\"MI\",\"KKR\",158),\n",
    "    (\"Chennai\",\"RR\",\"CSK\",\"RR\",\"CSK\",142),\n",
    "    (\"Bengaluru\",\"RCB\",\"DC\",\"DC\",\"RCB\",188),\n",
    "    (\"Delhi\",\"DC\",\"LSG\",\"LSG\",\"DC\",209),\n",
    "    (\"Guwahati\",\"RR\",\"PK\",\"RR\",\"PK\",145),\n",
    "    (\"Mumbai\",\"LSG\",\"MI\",\"MI\",\"LSG\",215),\n",
    "    (\"Bengaluru\",\"RCB\",\"CSK\",\"CSK\",\"RCB\",219),\n",
    "    (\"Hyderabad\",\"PK\",\"SRH\",\"PK\",\"SRH\",215),\n",
    "    (\"Ahmedabad\",\"SRH\",\"KKR\",\"SRH\",\"KKR\",160),\n",
    "    (\"Ahmedabad\",\"RCB\",\"RR\",\"RR\",\"RR\",173),\n",
    "    (\"Chennai\",\"SRH\",\"RR\",\"RR\",\"SRH\",176),\n",
    "    (\"Chennai\",\"SRH\",\"KKR\",\"SRH\",\"KKR\",114)\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15aec21a-795c-4e6a-8d82-bcf549fc5925",
   "metadata": {},
   "source": [
    "#### List of venues where each team played played IPL 2024\n",
    "- Initially, build a dictionary of dictionaries\n",
    "    - Outer keys are team names\n",
    "    - Inner dictionary keys are venues where team played\n",
    "- Finally, convert dictionary of dictionaries into dictionary of lists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "57606d60-c73b-471f-ae52-8c9d778b3629",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_venues(l):\n",
    "    venues = {}\n",
    "    for m in l:\n",
    "        venue,team1,team2 = m[0],m[1],m[2]\n",
    "        for t in team1,team2:\n",
    "            if t in venues:\n",
    "                venues[t][venue] = 1\n",
    "            else:\n",
    "                venues[t] = {venue:1}  # Create a dictionary for venues[t]\n",
    "                                       # Equivalent to \n",
    "                                       #   venues[t] = {}\n",
    "                                       #   venues[t[[venue] = 1\n",
    "    venuelists = {}\n",
    "    for t in sorted(venues):           # Scan teams in alphabetical order\n",
    "        venuelists[t] = sorted(list(venues[t]))  # Sort venues in alphabetical order\n",
    "    return(venuelists)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8bcaab1b-060b-4e00-8913-bd60b75824bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'CSK': ['Ahmedabad',\n",
       "  'Bengaluru',\n",
       "  'Chennai',\n",
       "  'Dharamsala',\n",
       "  'Hyderabad',\n",
       "  'Lucknow',\n",
       "  'Mumbai',\n",
       "  'Visakhapatnam'],\n",
       " 'DC': ['Ahmedabad',\n",
       "  'Bengaluru',\n",
       "  'Delhi',\n",
       "  'Jaipur',\n",
       "  'Kolkata',\n",
       "  'Lucknow',\n",
       "  'Mohali',\n",
       "  'Mumbai',\n",
       "  'Visakhapatnam'],\n",
       " 'GT': ['Ahmedabad',\n",
       "  'Bengaluru',\n",
       "  'Chennai',\n",
       "  'Delhi',\n",
       "  'Jaipur',\n",
       "  'Lucknow',\n",
       "  'Mohali'],\n",
       " 'KKR': ['Ahmedabad',\n",
       "  'Bengaluru',\n",
       "  'Chennai',\n",
       "  'Kolkata',\n",
       "  'Lucknow',\n",
       "  'Mumbai',\n",
       "  'Visakhapatnam'],\n",
       " 'LSG': ['Bengaluru',\n",
       "  'Chennai',\n",
       "  'Delhi',\n",
       "  'Hyderabad',\n",
       "  'Jaipur',\n",
       "  'Kolkata',\n",
       "  'Lucknow',\n",
       "  'Mumbai'],\n",
       " 'MI': ['Ahmedabad',\n",
       "  'Delhi',\n",
       "  'Hyderabad',\n",
       "  'Jaipur',\n",
       "  'Kolkata',\n",
       "  'Lucknow',\n",
       "  'Mohali',\n",
       "  'Mumbai'],\n",
       " 'PK': ['Ahmedabad',\n",
       "  'Bengaluru',\n",
       "  'Chennai',\n",
       "  'Dharamsala',\n",
       "  'Guwahati',\n",
       "  'Hyderabad',\n",
       "  'Kolkata',\n",
       "  'Lucknow',\n",
       "  'Mohali'],\n",
       " 'RCB': ['Ahmedabad',\n",
       "  'Bengaluru',\n",
       "  'Chennai',\n",
       "  'Dharamsala',\n",
       "  'Hyderabad',\n",
       "  'Jaipur',\n",
       "  'Kolkata',\n",
       "  'Mumbai'],\n",
       " 'RR': ['Ahmedabad',\n",
       "  'Chennai',\n",
       "  'Delhi',\n",
       "  'Guwahati',\n",
       "  'Hyderabad',\n",
       "  'Jaipur',\n",
       "  'Kolkata',\n",
       "  'Lucknow',\n",
       "  'Mohali',\n",
       "  'Mumbai'],\n",
       " 'SRH': ['Ahmedabad',\n",
       "  'Bengaluru',\n",
       "  'Chennai',\n",
       "  'Delhi',\n",
       "  'Hyderabad',\n",
       "  'Kolkata',\n",
       "  'Mohali',\n",
       "  'Mumbai']}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_venues(matchlist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6388018f-f399-4829-8110-410f5944711a",
   "metadata": {},
   "source": [
    "## Matrices\n",
    "- Matrix is a list of lists\n",
    "- Each row is a list of values `[v1,v2,...vm]`\n",
    "- Matrix is a list of rows `[ row1, row2, ..., rown ]`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58f33181-0d6f-444a-8b8b-0ef6a4b20882",
   "metadata": {},
   "source": [
    "### Transpose a matrix\n",
    "- Columns of original matrix are rows of transpose\n",
    "- If input is square, can swap `M[i][j]` and `M[j][i]`\n",
    "- If not square, need to construct transpose of correct dimension"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ad6fba01-5e02-429a-a9e1-1cb5e01e3871",
   "metadata": {},
   "outputs": [],
   "source": [
    "def transpose(mat):\n",
    "    # Extract dimensions of input -- assume well-formed, non-empty\n",
    "    nrows = len(mat)\n",
    "    ncols = len(mat[0])\n",
    "    # Create an empty matrix for the transpose\n",
    "    trmat = []\n",
    "    for i in range(ncols):\n",
    "        trmat.append([])\n",
    "    # Populate the transpose -- append each M[i][j] to row j of transpose\n",
    "    for i in range(nrows):\n",
    "        for j in range(ncols):\n",
    "            trmat[j].append(mat[i][j])\n",
    "    return(trmat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "314e1ff4-8480-4592-9a45-0f80ee299b4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = [[0,3],[1,4],[2,5]]\n",
    "mt = transpose(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "44d29c19-82e0-4114-92cb-dfcdfb4bdfc9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([[0, 3], [1, 4], [2, 5]], [[0, 1, 2], [3, 4, 5]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m, mt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98f9ed19-7917-4a05-9735-936b1e5383c5",
   "metadata": {},
   "source": [
    "### Strings\n",
    "- Also sequences, of characters\n",
    "- String values can be enclosed in single or double quotes\n",
    "    - `'Chennai'` or `\"Chennai\"`\n",
    "- Allows a value that has a quote to be easily embedded\n",
    "    - `\"Fermat's Last Theorem\"`\n",
    "    - `'He said, \"Thank you!\"`\n",
    "- If you need to use both single and double quotes inside the string, use a triple quote!\n",
    "    - `'''He said, \"That's great!\"'''`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "411eca0c-7532-4077-92e1-f3253f7f3299",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = '''He said, \"That's great!\"'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2fec1aac-a6ef-4eb1-acd7-559c2498e281",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'He said, \"That\\'s great!\"'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54db6aa5-ced8-4d70-a93a-01bc2097bc05",
   "metadata": {},
   "source": [
    "- Python prefers to render strings using single quote\n",
    "- Embedded quotes are \"escaped\" using `\\` to remove their special meaning"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fdd6aca3-76f2-4d81-b26b-bcb822fccd67",
   "metadata": {},
   "source": [
    "- Like lists and tuples, can access elements of a string by position, or by slices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fdc7dd9d-704d-45d0-af41-ffd917499dad",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = \"hello\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "65b2cb63-3ef3-46f8-a69f-964028cd43dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'e'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "53d57f02-fb50-4608-a767-e2a3edf3496a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ll'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[2:4]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3486b0c1-5cda-49e4-ae40-5132f6162d0f",
   "metadata": {},
   "source": [
    "- Some languages have a separate type `char` for a single character\n",
    "    - A string is then a sequence of `char`\n",
    "- In Python, there is only the string type `str`\n",
    "    - A single character is the same as a string of length 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6da97fea-5521-478a-9777-8c7e660befbd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[1] == \"e\"  # Logically speaking, s[1] is a single character"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "946b98fb-708a-4c5f-9afd-f60bad35be93",
   "metadata": {},
   "source": [
    "- Concatenate strings using `+`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9257524a-a5fb-4bf1-9665-7e506764f596",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = \"hello\"\n",
    "t = \"there\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "84a43675-65a9-45ec-9af2-f4eabb6604be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('hellothere', 'hello', 'there')"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s+t, s, t"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69192fd4-e7b7-483f-827c-d3e7fd276e7e",
   "metadata": {},
   "source": [
    "- Like tuples, cannot update parts of a string directly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7c952f0c-f419-4890-8fb5-00fe5c6d0378",
   "metadata": {},
   "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[15]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43ms\u001b[49m\u001b[43m[\u001b[49m\u001b[32;43m3\u001b[39;49m\u001b[43m]\u001b[49m = \u001b[33m\"\u001b[39m\u001b[33mp\u001b[39m\u001b[33m\"\u001b[39m\n",
      "\u001b[31mTypeError\u001b[39m: 'str' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "s[3] = \"p\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "338ec8d1-d566-4077-ae16-36fdea673be2",
   "metadata": {},
   "source": [
    "- Instead, assemble a new string from the old one"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "6abff667-6eb3-4bdf-a337-26c91345036f",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = s[0:3] + \"p\" + s[4:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9b1ef9ef-8b3e-40b7-9320-2efda1beca50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'helpo'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82cf3e41-5d0c-45dc-8939-33a76d726216",
   "metadata": {},
   "source": [
    "- Can iterate over strings and check membership, like lists\n",
    "- For iteration, no distinction between a string `\"xyz\"` and the list `[\"x\",\"y\",\"z\"]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "4f350fb1-467e-4858-a919-fb9bb8c427e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def vowel(c):\n",
    "    return(c in \"aeiou\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "26f92d73-cb76-43bd-9340-9a780a10a5f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, False)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowel(\"a\"),vowel(\"b\"),"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8829f2aa-09ee-411a-8d6d-1ab0db095a74",
   "metadata": {},
   "source": [
    "- For stringx `x in s` seems to be interpreted as substring membership"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "47441292-189e-4670-a9e1-43cfd8db0a21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, False, True)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowel(\"ae\"),vowel(\"ai\"),vowel(\"io\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0237ac2e-1bcc-484b-bfe1-3eb92f1eb537",
   "metadata": {},
   "source": [
    "- Standard example of filtered iteration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ee69d477-ef7f-40c2-8e7d-2bc6f3c29e74",
   "metadata": {},
   "outputs": [],
   "source": [
    "def countvowels(s):\n",
    "    count = 0\n",
    "    for c in s:\n",
    "        if vowel(c):\n",
    "            count = count+1\n",
    "    return(count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d12fd3db-af41-49fb-9720-834e23c287c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "countvowels(\"hello\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fbe4dee8-f07a-418b-8546-347258b0f8eb",
   "metadata": {},
   "source": [
    "- `x in s` expects `x` to be a string if `s` is a string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "83f5a864-6b3f-45dc-8968-3f1127171dd1",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'in <string>' requires string as left operand, not 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[23]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mvowel\u001b[49m\u001b[43m(\u001b[49m\u001b[32;43m7\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[18]\u001b[39m\u001b[32m, line 2\u001b[39m, in \u001b[36mvowel\u001b[39m\u001b[34m(c)\u001b[39m\n\u001b[32m      1\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mvowel\u001b[39m(c):\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m     \u001b[38;5;28;01mreturn\u001b[39;00m(\u001b[43mc\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43maeiou\u001b[39;49m\u001b[33;43m\"\u001b[39;49m)\n",
      "\u001b[31mTypeError\u001b[39m: 'in <string>' requires string as left operand, not int"
     ]
    }
   ],
   "source": [
    "vowel(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "3dacb31c-f8e5-4302-8fe7-c0c1c5685484",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowel(\"7\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f3d24b16-7aa6-4ece-9e96-fa3d4a53d50a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowel(\"there\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f22d8e37-9215-4038-8b30-7d5439a19841",
   "metadata": {},
   "source": [
    "- We have seen that `list()` and `int()` can be use to convert values from one type to another\n",
    "- Likewise `str()` converts its argument to a string\n",
    "    - Almost any value converts sensibly into a \"readable\" representation\n",
    "    - `print(v)` implicitly converts `v` to `str(v)` to display on screen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "67d55c3c-bad4-404c-b650-8616ab24c5e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[1, 2, 3]'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str([1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "1ac30287-6d74-4e30-a7cb-e062f270bb22",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'77'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(77)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6775a477-b1a7-44d0-b8d9-50bb20328568",
   "metadata": {},
   "source": [
    "- Can convert a string to a number if the contents can be intepreted sensibly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "2decd87e-dde9-421a-a7a7-1250a52c2597",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "77"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int('77')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "9da617d1-e00d-4a19-9529-1b266ac03eb5",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: 'hello'",
     "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[29]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mhello\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[31mValueError\u001b[39m: invalid literal for int() with base 10: 'hello'"
     ]
    }
   ],
   "source": [
    "int('hello')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "11dea25b-8aa3-41b6-b54e-ca6c1cf9f0f0",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: '77.5'",
     "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[30]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43m77.5\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m)\u001b[49m  \u001b[38;5;66;03m# 77.5 is a number, but not an int\u001b[39;00m\n",
      "\u001b[31mValueError\u001b[39m: invalid literal for int() with base 10: '77.5'"
     ]
    }
   ],
   "source": [
    "int('77.5')  # 77.5 is a number, but not an int"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a3636dde-78eb-45e7-86e0-b9baca41bb28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "77.5"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "float('77.5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "8d6c6688-6ece-4c45-bf0c-77db42dab676",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "77.0"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "float('77')"
   ]
  }
 ],
 "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
}
