{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6b4fce18-128e-441d-9040-7d738b1ac1c8",
   "metadata": {},
   "source": [
    "## PDSP 2026, Lecture 02, 6 August 2025"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "879ec57a-2e55-4547-9e93-fbea876fdcbf",
   "metadata": {},
   "source": [
    "## Set up the table as a list\n",
    "- Each entry is a row\n",
    "- Each row is a tuple of columns \n",
    "- `(City,HTeam,ATeam,HGoals,AGoals,HPens,APens,HXG,AXG)`\n",
    "- `HGoals`, `AGoals`, `HPens`, `APens` are integers (`Int`)\n",
    "- `HXG`, `AXG` are decimal fractions (`Float`)\n",
    "- `City`, `HTeam`, `ATeam` are text (`String`)\n",
    "- Assign a value to a variable: `variable = value`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d5b3432e-69c2-4d36-9ef8-5fd5b266ed8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "matchlist = [\n",
    "  (\"Mexico City\",\"MEX\",\"RSA\",2,0,0,0,1.84,0.52),\n",
    "  (\"Zapopan\",\"KOR\",\"CZE\",2,1,0,0,1.45,1.12),\n",
    "  (\"Toronto\",\"CAN\",\"BIH\",1,1,0,0,1.35,0.98),\n",
    "  (\"Inglewood\",\"USA\",\"PAR\",4,1,0,0,2.76,0.88),\n",
    "  (\"Foxborough\",\"QAT\",\"SUI\",1,1,0,0,0.78,1.54),\n",
    "  (\"East Rutherford\",\"BRA\",\"MAR\",1,1,0,0,1.62,1.1),\n",
    "  (\"Philadelphia\",\"HAI\",\"SCO\",0,1,0,0,1.25,0.65),\n",
    "  (\"Seattle\",\"AUS\",\"TUR\",2,0,0,0,1.48,0.72),\n",
    "  (\"Atlanta\",\"GER\",\"CUW\",7,1,0,0,4.82,0.44),\n",
    "  (\"Santa Clara\",\"NED\",\"JPN\",2,2,0,0,1.95,1.82),\n",
    "  (\"Houston\",\"CIV\",\"ECU\",1,0,0,0,1.1,0.95),\n",
    "  (\"Kansas City\",\"SWE\",\"TUN\",5,1,0,0,3.12,0.78),\n",
    "  (\"Miami Gardens\",\"ESP\",\"CPV\",0,0,0,0,2.15,0.35),\n",
    "  (\"Arlington\",\"BEL\",\"EGY\",1,1,0,0,1.58,1.12),\n",
    "  (\"Guadalupe\",\"KSA\",\"URU\",1,1,0,0,0.85,1.76),\n",
    "  (\"Vancouver\",\"IRN\",\"NZL\",2,2,0,0,1.88,1.2),\n",
    "  (\"East Rutherford\",\"FRA\",\"SEN\",3,1,0,0,2.44,1.05),\n",
    "  (\"Foxborough\",\"IRQ\",\"NOR\",1,4,0,0,0.68,2.85),\n",
    "  (\"Inglewood\",\"ARG\",\"ALG\",3,0,0,0,2.65,0.42),\n",
    "  (\"Santa Clara\",\"AUT\",\"JOR\",3,1,0,0,1.98,0.88),\n",
    "  (\"Atlanta\",\"POR\",\"COD\",1,1,0,0,1.78,1.15),\n",
    "  (\"Miami Gardens\",\"ENG\",\"CRO\",4,2,0,0,2.92,1.64),\n",
    "  (\"Houston\",\"GHA\",\"PAN\",1,0,0,0,1.3,0.75),\n",
    "  (\"Seattle\",\"UZB\",\"COL\",1,3,0,0,0.95,2.1),\n",
    "  (\"Atlanta\",\"MEX\",\"KOR\",1,0,0,0,1.62,0.58),\n",
    "  (\"Inglewood\",\"CZE\",\"RSA\",1,1,0,0,1.35,1.15),\n",
    "  (\"Vancouver\",\"CAN\",\"QAT\",6,0,0,0,3.82,0.22),\n",
    "  (\"Mexico City\",\"SUI\",\"BIH\",4,1,0,0,2.45,0.88),\n",
    "  (\"East Rutherford\",\"BRA\",\"HAI\",3,0,0,0,1.16,0.9),\n",
    "  (\"Arlington\",\"SCO\",\"MAR\",0,1,0,0,0.97,0.54),\n",
    "  (\"Toronto\",\"USA\",\"AUS\",2,0,0,0,1.21,0.32),\n",
    "  (\"Guadalupe\",\"TUR\",\"PAR\",0,1,0,0,0.4,0.3),\n",
    "  (\"Foxborough\",\"GER\",\"CIV\",2,1,0,0,1.85,0.72),\n",
    "  (\"Kansas City\",\"ECU\",\"CUW\",0,0,0,0,1.92,0.08),\n",
    "  (\"Philadelphia\",\"NED\",\"SWE\",5,1,0,0,3.45,0.88),\n",
    "  (\"Seattle\",\"TUN\",\"JPN\",0,4,0,0,0.35,3.12),\n",
    "  (\"Houston\",\"BEL\",\"IRN\",0,0,0,0,1.45,0.62),\n",
    "  (\"Miami Gardens\",\"NZL\",\"EGY\",1,3,0,0,0.55,2.15),\n",
    "  (\"Santa Clara\",\"ESP\",\"KSA\",4,0,0,0,3.28,0.25),\n",
    "  (\"East Rutherford\",\"URU\",\"CPV\",2,2,0,0,1.65,1.1),\n",
    "  (\"Arlington\",\"FRA\",\"IRQ\",3,0,0,0,2.21,0.45),\n",
    "  (\"Toronto\",\"NOR\",\"SEN\",3,2,0,0,1.95,1.48),\n",
    "  (\"Guadalupe\",\"ARG\",\"AUT\",2,0,0,0,1.78,0.55),\n",
    "  (\"Foxborough\",\"JOR\",\"ALG\",1,2,0,0,0.88,1.92),\n",
    "  (\"Kansas City\",\"POR\",\"UZB\",5,0,0,0,3.54,0.42),\n",
    "  (\"Philadelphia\",\"COL\",\"COD\",1,0,0,0,1.62,0.58),\n",
    "  (\"Seattle\",\"ENG\",\"GHA\",0,0,0,0,1.12,0.89),\n",
    "  (\"Mexico City\",\"PAN\",\"CRO\",0,1,0,0,0.42,1.48),\n",
    "  (\"Mexico City\",\"CZE\",\"MEX\",0,3,0,0,1.1,1.27),\n",
    "  (\"Zapopan\",\"RSA\",\"KOR\",1,0,0,0,1.26,1.39),\n",
    "  (\"Vancouver\",\"SUI\",\"CAN\",2,1,0,0,0.71,1.35),\n",
    "  (\"Toronto\",\"BIH\",\"QAT\",3,1,0,0,1.48,0.95),\n",
    "  (\"East Rutherford\",\"SCO\",\"BRA\",0,3,0,0,0.45,2.18),\n",
    "  (\"Foxborough\",\"MAR\",\"HAI\",4,2,0,0,2.35,1.12),\n",
    "  (\"Inglewood\",\"TUR\",\"USA\",3,2,0,0,1.62,1.45),\n",
    "  (\"Santa Clara\",\"PAR\",\"AUS\",0,0,0,0,0.55,0.48),\n",
    "  (\"Atlanta\",\"ECU\",\"GER\",2,1,0,0,1.28,1.55),\n",
    "  (\"Houston\",\"CUW\",\"CIV\",0,2,0,0,0.32,1.68),\n",
    "  (\"Kansas City\",\"TUN\",\"NED\",1,3,0,0,0.62,2.15),\n",
    "  (\"Seattle\",\"JPN\",\"SWE\",1,1,0,0,1.12,0.95),\n",
    "  (\"Arlington\",\"NZL\",\"BEL\",1,5,0,0,0.48,3.25),\n",
    "  (\"Guadalupe\",\"EGY\",\"IRN\",1,1,0,0,0.85,0.92),\n",
    "  (\"Miami Gardens\",\"URU\",\"ESP\",0,1,0,0,0.78,1.35),\n",
    "  (\"Philadelphia\",\"CPV\",\"KSA\",0,0,0,0,0.42,0.38),\n",
    "  (\"Foxborough\",\"NOR\",\"FRA\",1,4,0,0,1.3,0.96),\n",
    "  (\"East Rutherford\",\"SEN\",\"IRQ\",5,0,0,0,3.01,0.14),\n",
    "  (\"Santa Clara\",\"JOR\",\"ARG\",1,3,0,0,0.76,2.13),\n",
    "  (\"Inglewood\",\"ALG\",\"AUT\",3,3,0,0,1.62,1.44),\n",
    "  (\"Miami Gardens\",\"COL\",\"POR\",0,0,0,0,1.62,0.73),\n",
    "  (\"Atlanta\",\"COD\",\"UZB\",3,1,0,0,2.35,0.2),\n",
    "  (\"East Rutherford\",\"PAN\",\"ENG\",0,2,0,0,0.69,1.39),\n",
    "  (\"Philadelphia\",\"CRO\",\"GHA\",2,1,0,0,0.42,0.74),\n",
    "  (\"Inglewood\",\"RSA\",\"CAN\",0,1,0,0,0.13,1.32),\n",
    "  (\"Houston\",\"BRA\",\"JPN\",2,1,0,0,1.69,0.23),\n",
    "  (\"Foxborough\",\"GER\",\"PAR\",1,1,3,4,0.72,0.42),\n",
    "  (\"Guadalupe\",\"NED\",\"MAR\",1,1,2,3,0.23,1.4),\n",
    "  (\"Arlington\",\"CIV\",\"NOR\",1,2,0,0,1.15,2.02),\n",
    "  (\"East Rutherford\",\"FRA\",\"SWE\",3,0,0,0,3.17,0.67),\n",
    "  (\"Mexico City\",\"MEX\",\"ECU\",2,0,0,0,1.02,0.73),\n",
    "  (\"Atlanta\",\"ENG\",\"COD\",2,1,0,0,2.04,0.76),\n",
    "  (\"Seattle\",\"BEL\",\"SEN\",3,2,0,0,1.74,3.58),\n",
    "  (\"Santa Clara\",\"USA\",\"BIH\",2,0,0,0,0.92,0.25),\n",
    "  (\"Inglewood\",\"ESP\",\"AUT\",3,0,0,0,2.84,0.32),\n",
    "  (\"Toronto\",\"POR\",\"CRO\",2,1,0,0,2.18,1.34),\n",
    "  (\"Vancouver\",\"SUI\",\"ALG\",2,0,0,0,2.52,0.73),\n",
    "  (\"Arlington\",\"AUS\",\"EGY\",1,1,2,4,0.87,1.36),\n",
    "  (\"Miami Gardens\",\"ARG\",\"CPV\",3,2,0,0,2.16,0.46),\n",
    "  (\"Kansas City\",\"COL\",\"GHA\",1,0,0,0,2.19,0.26),\n",
    "  (\"Philadelphia\",\"PAR\",\"FRA\",0,1,0,0,0.3,1.8),\n",
    "  (\"Houston\",\"CAN\",\"MAR\",0,3,0,0,0.8,1.2),\n",
    "  (\"Arlington\",\"BRA\",\"NOR\",1,2,0,0,2.61,1.05),\n",
    "  (\"Mexico City\",\"MEX\",\"ENG\",2,3,0,0,1.88,1.61),\n",
    "  (\"Miami Gardens\",\"POR\",\"ESP\",0,1,0,0,0.63,1.69),\n",
    "  (\"Santa Clara\",\"USA\",\"BEL\",1,4,0,0,0.67,2.15),\n",
    "  (\"Kansas City\",\"ARG\",\"EGY\",3,2,0,0,2.12,1.54),\n",
    "  (\"Seattle\",\"SUI\",\"COL\",0,0,4,3,1.15,1.46),\n",
    "  (\"Foxborough\",\"FRA\",\"MAR\",2,0,0,0,3.1,0.13),\n",
    "  (\"Inglewood\",\"ESP\",\"BEL\",2,1,0,0,2.08,0.37),\n",
    "  (\"Miami Gardens\",\"NOR\",\"ENG\",1,2,0,0,0.77,0.96),\n",
    "  (\"Kansas City\",\"ARG\",\"SUI\",3,1,0,0,2.0,0.53),\n",
    "  (\"Arlington\",\"FRA\",\"ESP\",0,2,0,0,0.3,1.63),\n",
    "  (\"Atlanta\",\"ENG\",\"ARG\",1,2,0,0,0.54,1.80),\n",
    "  (\"Miami Gardens\",\"FRA\",\"ENG\",4,6,0,0,2.88,2.88),\n",
    "  (\"East Rutherford\",\"ESP\",\"ARG\",1,0,0,0,0.52,0.09)\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5bd40f0-ca24-43db-9a69-2d55d22e1960",
   "metadata": {},
   "source": [
    "- Can evaluate the value of a variable directly in a Jupyter notebook cell"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4789c4ba-781f-47a9-93b8-736b974af695",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Mexico City', 'MEX', 'RSA', 2, 0, 0, 0, 1.84, 0.52),\n",
       " ('Zapopan', 'KOR', 'CZE', 2, 1, 0, 0, 1.45, 1.12),\n",
       " ('Toronto', 'CAN', 'BIH', 1, 1, 0, 0, 1.35, 0.98),\n",
       " ('Inglewood', 'USA', 'PAR', 4, 1, 0, 0, 2.76, 0.88),\n",
       " ('Foxborough', 'QAT', 'SUI', 1, 1, 0, 0, 0.78, 1.54),\n",
       " ('East Rutherford', 'BRA', 'MAR', 1, 1, 0, 0, 1.62, 1.1),\n",
       " ('Philadelphia', 'HAI', 'SCO', 0, 1, 0, 0, 1.25, 0.65),\n",
       " ('Seattle', 'AUS', 'TUR', 2, 0, 0, 0, 1.48, 0.72),\n",
       " ('Atlanta', 'GER', 'CUW', 7, 1, 0, 0, 4.82, 0.44),\n",
       " ('Santa Clara', 'NED', 'JPN', 2, 2, 0, 0, 1.95, 1.82),\n",
       " ('Houston', 'CIV', 'ECU', 1, 0, 0, 0, 1.1, 0.95),\n",
       " ('Kansas City', 'SWE', 'TUN', 5, 1, 0, 0, 3.12, 0.78),\n",
       " ('Miami Gardens', 'ESP', 'CPV', 0, 0, 0, 0, 2.15, 0.35),\n",
       " ('Arlington', 'BEL', 'EGY', 1, 1, 0, 0, 1.58, 1.12),\n",
       " ('Guadalupe', 'KSA', 'URU', 1, 1, 0, 0, 0.85, 1.76),\n",
       " ('Vancouver', 'IRN', 'NZL', 2, 2, 0, 0, 1.88, 1.2),\n",
       " ('East Rutherford', 'FRA', 'SEN', 3, 1, 0, 0, 2.44, 1.05),\n",
       " ('Foxborough', 'IRQ', 'NOR', 1, 4, 0, 0, 0.68, 2.85),\n",
       " ('Inglewood', 'ARG', 'ALG', 3, 0, 0, 0, 2.65, 0.42),\n",
       " ('Santa Clara', 'AUT', 'JOR', 3, 1, 0, 0, 1.98, 0.88),\n",
       " ('Atlanta', 'POR', 'COD', 1, 1, 0, 0, 1.78, 1.15),\n",
       " ('Miami Gardens', 'ENG', 'CRO', 4, 2, 0, 0, 2.92, 1.64),\n",
       " ('Houston', 'GHA', 'PAN', 1, 0, 0, 0, 1.3, 0.75),\n",
       " ('Seattle', 'UZB', 'COL', 1, 3, 0, 0, 0.95, 2.1),\n",
       " ('Atlanta', 'MEX', 'KOR', 1, 0, 0, 0, 1.62, 0.58),\n",
       " ('Inglewood', 'CZE', 'RSA', 1, 1, 0, 0, 1.35, 1.15),\n",
       " ('Vancouver', 'CAN', 'QAT', 6, 0, 0, 0, 3.82, 0.22),\n",
       " ('Mexico City', 'SUI', 'BIH', 4, 1, 0, 0, 2.45, 0.88),\n",
       " ('East Rutherford', 'BRA', 'HAI', 3, 0, 0, 0, 1.16, 0.9),\n",
       " ('Arlington', 'SCO', 'MAR', 0, 1, 0, 0, 0.97, 0.54),\n",
       " ('Toronto', 'USA', 'AUS', 2, 0, 0, 0, 1.21, 0.32),\n",
       " ('Guadalupe', 'TUR', 'PAR', 0, 1, 0, 0, 0.4, 0.3),\n",
       " ('Foxborough', 'GER', 'CIV', 2, 1, 0, 0, 1.85, 0.72),\n",
       " ('Kansas City', 'ECU', 'CUW', 0, 0, 0, 0, 1.92, 0.08),\n",
       " ('Philadelphia', 'NED', 'SWE', 5, 1, 0, 0, 3.45, 0.88),\n",
       " ('Seattle', 'TUN', 'JPN', 0, 4, 0, 0, 0.35, 3.12),\n",
       " ('Houston', 'BEL', 'IRN', 0, 0, 0, 0, 1.45, 0.62),\n",
       " ('Miami Gardens', 'NZL', 'EGY', 1, 3, 0, 0, 0.55, 2.15),\n",
       " ('Santa Clara', 'ESP', 'KSA', 4, 0, 0, 0, 3.28, 0.25),\n",
       " ('East Rutherford', 'URU', 'CPV', 2, 2, 0, 0, 1.65, 1.1),\n",
       " ('Arlington', 'FRA', 'IRQ', 3, 0, 0, 0, 2.21, 0.45),\n",
       " ('Toronto', 'NOR', 'SEN', 3, 2, 0, 0, 1.95, 1.48),\n",
       " ('Guadalupe', 'ARG', 'AUT', 2, 0, 0, 0, 1.78, 0.55),\n",
       " ('Foxborough', 'JOR', 'ALG', 1, 2, 0, 0, 0.88, 1.92),\n",
       " ('Kansas City', 'POR', 'UZB', 5, 0, 0, 0, 3.54, 0.42),\n",
       " ('Philadelphia', 'COL', 'COD', 1, 0, 0, 0, 1.62, 0.58),\n",
       " ('Seattle', 'ENG', 'GHA', 0, 0, 0, 0, 1.12, 0.89),\n",
       " ('Mexico City', 'PAN', 'CRO', 0, 1, 0, 0, 0.42, 1.48),\n",
       " ('Mexico City', 'CZE', 'MEX', 0, 3, 0, 0, 1.1, 1.27),\n",
       " ('Zapopan', 'RSA', 'KOR', 1, 0, 0, 0, 1.26, 1.39),\n",
       " ('Vancouver', 'SUI', 'CAN', 2, 1, 0, 0, 0.71, 1.35),\n",
       " ('Toronto', 'BIH', 'QAT', 3, 1, 0, 0, 1.48, 0.95),\n",
       " ('East Rutherford', 'SCO', 'BRA', 0, 3, 0, 0, 0.45, 2.18),\n",
       " ('Foxborough', 'MAR', 'HAI', 4, 2, 0, 0, 2.35, 1.12),\n",
       " ('Inglewood', 'TUR', 'USA', 3, 2, 0, 0, 1.62, 1.45),\n",
       " ('Santa Clara', 'PAR', 'AUS', 0, 0, 0, 0, 0.55, 0.48),\n",
       " ('Atlanta', 'ECU', 'GER', 2, 1, 0, 0, 1.28, 1.55),\n",
       " ('Houston', 'CUW', 'CIV', 0, 2, 0, 0, 0.32, 1.68),\n",
       " ('Kansas City', 'TUN', 'NED', 1, 3, 0, 0, 0.62, 2.15),\n",
       " ('Seattle', 'JPN', 'SWE', 1, 1, 0, 0, 1.12, 0.95),\n",
       " ('Arlington', 'NZL', 'BEL', 1, 5, 0, 0, 0.48, 3.25),\n",
       " ('Guadalupe', 'EGY', 'IRN', 1, 1, 0, 0, 0.85, 0.92),\n",
       " ('Miami Gardens', 'URU', 'ESP', 0, 1, 0, 0, 0.78, 1.35),\n",
       " ('Philadelphia', 'CPV', 'KSA', 0, 0, 0, 0, 0.42, 0.38),\n",
       " ('Foxborough', 'NOR', 'FRA', 1, 4, 0, 0, 1.3, 0.96),\n",
       " ('East Rutherford', 'SEN', 'IRQ', 5, 0, 0, 0, 3.01, 0.14),\n",
       " ('Santa Clara', 'JOR', 'ARG', 1, 3, 0, 0, 0.76, 2.13),\n",
       " ('Inglewood', 'ALG', 'AUT', 3, 3, 0, 0, 1.62, 1.44),\n",
       " ('Miami Gardens', 'COL', 'POR', 0, 0, 0, 0, 1.62, 0.73),\n",
       " ('Atlanta', 'COD', 'UZB', 3, 1, 0, 0, 2.35, 0.2),\n",
       " ('East Rutherford', 'PAN', 'ENG', 0, 2, 0, 0, 0.69, 1.39),\n",
       " ('Philadelphia', 'CRO', 'GHA', 2, 1, 0, 0, 0.42, 0.74),\n",
       " ('Inglewood', 'RSA', 'CAN', 0, 1, 0, 0, 0.13, 1.32),\n",
       " ('Houston', 'BRA', 'JPN', 2, 1, 0, 0, 1.69, 0.23),\n",
       " ('Foxborough', 'GER', 'PAR', 1, 1, 3, 4, 0.72, 0.42),\n",
       " ('Guadalupe', 'NED', 'MAR', 1, 1, 2, 3, 0.23, 1.4),\n",
       " ('Arlington', 'CIV', 'NOR', 1, 2, 0, 0, 1.15, 2.02),\n",
       " ('East Rutherford', 'FRA', 'SWE', 3, 0, 0, 0, 3.17, 0.67),\n",
       " ('Mexico City', 'MEX', 'ECU', 2, 0, 0, 0, 1.02, 0.73),\n",
       " ('Atlanta', 'ENG', 'COD', 2, 1, 0, 0, 2.04, 0.76),\n",
       " ('Seattle', 'BEL', 'SEN', 3, 2, 0, 0, 1.74, 3.58),\n",
       " ('Santa Clara', 'USA', 'BIH', 2, 0, 0, 0, 0.92, 0.25),\n",
       " ('Inglewood', 'ESP', 'AUT', 3, 0, 0, 0, 2.84, 0.32),\n",
       " ('Toronto', 'POR', 'CRO', 2, 1, 0, 0, 2.18, 1.34),\n",
       " ('Vancouver', 'SUI', 'ALG', 2, 0, 0, 0, 2.52, 0.73),\n",
       " ('Arlington', 'AUS', 'EGY', 1, 1, 2, 4, 0.87, 1.36),\n",
       " ('Miami Gardens', 'ARG', 'CPV', 3, 2, 0, 0, 2.16, 0.46),\n",
       " ('Kansas City', 'COL', 'GHA', 1, 0, 0, 0, 2.19, 0.26),\n",
       " ('Philadelphia', 'PAR', 'FRA', 0, 1, 0, 0, 0.3, 1.8),\n",
       " ('Houston', 'CAN', 'MAR', 0, 3, 0, 0, 0.8, 1.2),\n",
       " ('Arlington', 'BRA', 'NOR', 1, 2, 0, 0, 2.61, 1.05),\n",
       " ('Mexico City', 'MEX', 'ENG', 2, 3, 0, 0, 1.88, 1.61),\n",
       " ('Miami Gardens', 'POR', 'ESP', 0, 1, 0, 0, 0.63, 1.69),\n",
       " ('Santa Clara', 'USA', 'BEL', 1, 4, 0, 0, 0.67, 2.15),\n",
       " ('Kansas City', 'ARG', 'EGY', 3, 2, 0, 0, 2.12, 1.54),\n",
       " ('Seattle', 'SUI', 'COL', 0, 0, 4, 3, 1.15, 1.46),\n",
       " ('Foxborough', 'FRA', 'MAR', 2, 0, 0, 0, 3.1, 0.13),\n",
       " ('Inglewood', 'ESP', 'BEL', 2, 1, 0, 0, 2.08, 0.37),\n",
       " ('Miami Gardens', 'NOR', 'ENG', 1, 2, 0, 0, 0.77, 0.96),\n",
       " ('Kansas City', 'ARG', 'SUI', 3, 1, 0, 0, 2.0, 0.53),\n",
       " ('Arlington', 'FRA', 'ESP', 0, 2, 0, 0, 0.3, 1.63),\n",
       " ('Atlanta', 'ENG', 'ARG', 1, 2, 0, 0, 0.54, 1.8),\n",
       " ('Miami Gardens', 'FRA', 'ENG', 4, 6, 0, 0, 2.88, 2.88),\n",
       " ('East Rutherford', 'ESP', 'ARG', 1, 0, 0, 0, 0.52, 0.09)]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matchlist"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45dcdf50-d308-41ae-aae5-4e5539308371",
   "metadata": {},
   "source": [
    "- Can only check value of a name if it has bene assigned earlier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ac851f6b-34d3-4ca2-a3e9-dd64e4c583fd",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'n' 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[3]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m n\n",
      "\u001b[31mNameError\u001b[39m: name 'n' is not defined"
     ]
    }
   ],
   "source": [
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b57c6ac8-7256-4370-9889-b2497517c07c",
   "metadata": {},
   "source": [
    "- Any expression can be evaluated in a Jupyter notebook cell"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a1cc551f-e92e-4120-a388-c540457128ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "7+3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dd09d100-7f2a-44d7-bd88-3e8b3b46e301",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 9\n",
    "y = x + 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3ac61fef-730e-4cba-a09e-f6c891981c30",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6d6a362-f403-4f5e-b439-07b5e4423adc",
   "metadata": {},
   "source": [
    "## Questions\n",
    "\n",
    "- How many matches were played?\n",
    "\n",
    "- What was the maximum number of goals scored in a match?\n",
    "\n",
    "- What was the average number of goals scored in a match?\n",
    "\n",
    "- How many matches had above average number of goals?\n",
    "\n",
    "- How many cities were venues?\n",
    "\n",
    "- Which team played as Home Team at maximum number of venues?\n",
    "\n",
    "- Is being Home Team an advantage?\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c53a1966-e39a-4528-85a4-6787b44b3931",
   "metadata": {},
   "source": [
    "## How many matches were played?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e952ea5-1e00-473f-a3fd-b43f1d6cb2e8",
   "metadata": {},
   "source": [
    "- Python's built-in function `len()` directly tells the length of a list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b0274e7e-5b17-4c19-82c9-ec220c400cfb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "104"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(matchlist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82ccdeb1-0118-4c1b-9368-0ac3afe3a382",
   "metadata": {},
   "source": [
    "## Count the number of entries explicitly\n",
    "- Iterate through the list\n",
    "- `for` variable `in` listname `:`\n",
    "    - `:` indicates a block of statements (commands) to follow\n",
    "    - Indent the commands to be performed within each iteration\n",
    "- `count = count + 1`\n",
    "    - `=` assigns a new value to a variable, not to be confused with equality\n",
    "    - rhs is old value of `count`, used to update the value of `count`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "145e7238-c606-4cfe-a9a4-5b51618edc73",
   "metadata": {},
   "outputs": [],
   "source": [
    "count = 0\n",
    "for match in matchlist:\n",
    "    count = count + 1  # Compute current count + 1 and reassign it to count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e20b08cd-450b-49eb-b59a-9bedf2641952",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "104"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "count"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "212f94c8-d9ea-4992-954b-0ec57606c63c",
   "metadata": {},
   "source": [
    "- Python does not require you to \"declare\" variables in advance\n",
    "- Can introduce new names on the fly\n",
    "- This can be a source of errors, if you mistype a name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "04d1c02e-3eef-4e00-8b9c-8d11e37f019d",
   "metadata": {},
   "outputs": [],
   "source": [
    "count = 0\n",
    "for row in matchlist:\n",
    "    countt = count + 1  # Compute current count + 1 and reassign it to count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8f35176a-7dc5-4aa6-9bd3-db0452075171",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "count"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62fbe007-2303-4773-851e-04128d6677ca",
   "metadata": {},
   "source": [
    "- If you are lucky, the mistyped name will appear on the right and Python will flag an error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "31dbd6a5-bf92-4e54-95b1-2ada22b48fcb",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'ccount' 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[12]\u001b[39m\u001b[32m, line 3\u001b[39m\n\u001b[32m      1\u001b[39m count = \u001b[32m0\u001b[39m\n\u001b[32m      2\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m row \u001b[38;5;28;01min\u001b[39;00m matchlist:\n\u001b[32m----> \u001b[39m\u001b[32m3\u001b[39m     count = ccount + \u001b[32m1\u001b[39m  \u001b[38;5;66;03m# Compute current count + 1 and reassign it to count\u001b[39;00m\n",
      "\u001b[31mNameError\u001b[39m: name 'ccount' is not defined"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "for row in matchlist:\n",
    "    count = ccount + 1  # Compute current count + 1 and reassign it to count"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc8bfeb3-cd45-4cef-a8ec-cbac953334d5",
   "metadata": {},
   "source": [
    "## What was the maximum number of goals scored in a match?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c186fd50-b24c-4f01-abe3-8fbd783255dd",
   "metadata": {},
   "source": [
    "- Initialize `maxgoals` to `0`\n",
    "- Update `maxgoals` whenever run target in current row exceeds current maximum\n",
    "    - Extract run target from tuple of values of current row using positional index\n",
    "    - In Python, indices always start with 0, so nine entries in the tuple have indices 0,1,,..,8\n",
    "- `if`specifies conditional execution\n",
    "    - `if` conditional-expression `:`\n",
    "        - Expression evaluates to `True` or `False`\n",
    "        - As before, `:` and indentation indicate scope of conditional execution\n",
    "        - Indented commands (update of `maxgoals`) happens only when `if` condition evaluates to `True`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a6fe11c3-de3f-4c8d-a83a-51c8590e0775",
   "metadata": {},
   "outputs": [],
   "source": [
    "maxgoals = 0\n",
    "for row in matchlist:\n",
    "    # Goals are in columns 4 to 7, add up these columns to get goals for the match\n",
    "    # Positions in a list start with 0, so these are values from index 3 to index 6\n",
    "    if row[3] + row[4] + row[5] + row[6] > maxgoals:\n",
    "        maxgoals = row[3] + row[4] + row[5] + row[6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e82adc2d-c22f-482b-8524-28bb16aac506",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "maxgoals"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8e9f053-1308-4474-9aa7-825848ea96c1",
   "metadata": {},
   "source": [
    "- `row[3] + row[4] + row[5] + row[6]` is clumsy to use each time\n",
    "- Better to assign a separate variable for `row[3] + row[4] + row[5] + row[6]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0da23d78-4dcc-4f1d-b7b2-9736f457e222",
   "metadata": {},
   "outputs": [],
   "source": [
    "maxgoals = 0\n",
    "for row in matchlist:\n",
    "    goals = row[3] + row[4] + row[5] + row[6]\n",
    "    if goals > maxgoals:\n",
    "        maxgoals = goals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d98d5d27-4315-4762-8ca3-701b0fda33aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "maxgoals"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a08a668-1926-4b6a-b34e-b8a57da337f0",
   "metadata": {},
   "source": [
    "- Can also initialize `maxgoals` to first target in the table\n",
    "- After this, can use `for` to iterate through the entire table\n",
    "    - Not a problem to read the first row of the table again\n",
    "    - Avoids complication of skipping the first row and running `for` from the second row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b1082273-f302-48ba-bcf5-76162cb74d4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "row = matchlist[0]\n",
    "maxgoals = row[3] + row[4] + row[5] + row[6]\n",
    "for row in matchlist:\n",
    "    goals = row[3] + row[4] + row[5] + row[6]\n",
    "    if goals > maxgoals:\n",
    "        maxgoals = goals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c59b749b-b9a5-4fc0-b75f-fecc6a36a81c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "maxgoals"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ad9f07f-9d4c-438f-95e8-d31ab33e0312",
   "metadata": {},
   "source": [
    "## What was the average number of goals scored in a match?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0e28915-9e9c-432a-8441-0c7ce319aa06",
   "metadata": {},
   "source": [
    "- Iterate to compute sum of all run targets\n",
    "- Average is total divided by count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9fc94d02-9cf8-4b38-9110-28262301035f",
   "metadata": {},
   "outputs": [],
   "source": [
    "count = 0\n",
    "goalsum = 0\n",
    "for row in matchlist: \n",
    "    count = count+1\n",
    "    goals = row[3] + row[4] + row[5] + row[6]\n",
    "    goalsum = goalsum + goals\n",
    "goalavg = goalsum/count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a7e6a2f3-0e75-4946-b613-794ce1727ee6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.201923076923077"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "goalavg"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbc0cb72-3a73-4c5d-877c-9b138f696606",
   "metadata": {},
   "source": [
    "- Can also maintain running average instead of running total"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "78df06ef-c03a-4586-b2d1-77b29698ba6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "count = 0\n",
    "currentavg = 0\n",
    "for row in matchlist:\n",
    "    totalsofar = count*currentavg\n",
    "    count = count+1\n",
    "    goals = row[3] + row[4] + row[5] + row[6]\n",
    "    currentavg = (totalsofar + goals)/count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "68c3fa3f-6ea8-499e-ad0a-a03b6a317154",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.201923076923077"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "currentavg"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4faac400-8cb7-4b96-980c-fc7b4fc32a6c",
   "metadata": {},
   "source": [
    "- Alternatively, avoid creating running total altogether\n",
    "- Note that `count` has already been incremented, so multiply `currentavg` by `count-1`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "9764fca3-b1e6-48b5-8e46-d6405a3a3a45",
   "metadata": {},
   "outputs": [],
   "source": [
    "count = 0\n",
    "currentavg = 0\n",
    "for row in matchlist:\n",
    "    count = count+1\n",
    "    goals = row[3] + row[4] + row[5] + row[6]\n",
    "    currentavg = (currentavg*(count-1) + goals)/count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "1b6381d3-868d-4047-8078-c7e1e951d76d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.201923076923077"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "currentavg"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "616fcef6-d8be-4b43-a504-1940030856cf",
   "metadata": {},
   "source": [
    "## How many matches had above average number of goals?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "412b1eeb-b29f-4c79-b0c4-319812ae2a3c",
   "metadata": {},
   "source": [
    "- *Filtered* iteration\n",
    "- Update the count only if the current run target is $\\gt$ average"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "4e613662-7d48-4230-b1c9-ed498c4037f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "count = 0\n",
    "goalsum = 0\n",
    "for row in matchlist: \n",
    "    count = count+1\n",
    "    goals = row[3] + row[4] + row[5] + row[6]\n",
    "    goalsum = goalsum + goals\n",
    "goalavg = goalsum/count\n",
    "\n",
    "aboveavgcount = 0\n",
    "for row in matchlist:\n",
    "    goals = row[3] + row[4] + row[5] + row[6]\n",
    "    if goals > goalavg:\n",
    "        aboveavgcount = aboveavgcount + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "f7023f58-8906-4379-8353-4e0e8c4a1bbc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "40"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aboveavgcount"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ecbb55de-4e53-4f9b-a685-84ba936c26dc",
   "metadata": {},
   "source": [
    "## How many cities were venues?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69f8a0aa-ff7d-4746-8c5c-95c51dbbb2f8",
   "metadata": {},
   "source": [
    "- Maintain a list of venues\n",
    "    - Need to initialize to empty list to tell Python this value can be used as a list\n",
    "    - Built-in check `v in l` returns `True` if value `v` is present in list `l`\n",
    "    - Add new venue to the list --- `l1 + l2` concatenates two lists into a single list "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "1ce56da8-0568-44b2-aa86-c6c619e1dba2",
   "metadata": {},
   "outputs": [],
   "source": [
    "venuelist = []\n",
    "for row in matchlist:\n",
    "    venue = row[0]  # Leftmost column\n",
    "    if not(venue in venuelist):\n",
    "        venuelist = venuelist + [venue]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "8a7ee7b9-6bc5-4c0b-9d96-d4c0492aa053",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mexico City',\n",
       " 'Zapopan',\n",
       " 'Toronto',\n",
       " 'Inglewood',\n",
       " 'Foxborough',\n",
       " 'East Rutherford',\n",
       " 'Philadelphia',\n",
       " 'Seattle',\n",
       " 'Atlanta',\n",
       " 'Santa Clara',\n",
       " 'Houston',\n",
       " 'Kansas City',\n",
       " 'Miami Gardens',\n",
       " 'Arlington',\n",
       " 'Guadalupe',\n",
       " 'Vancouver']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "venuelist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "5a7f7921-3c71-46a7-ae63-5593354cf9b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(venuelist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50ac9eee-a9bf-454d-824e-ce68af11fa41",
   "metadata": {},
   "source": [
    "- Can instead use a *dictionary*\n",
    "    - A collection of key:value pairs\n",
    "    - Initialize to empty dictionary\n",
    "    - Check if venue already exists as a key\n",
    "        - Create a new key if the current venue is not a key, by assigning a value\n",
    "        - Value assigned to the key is unimportant here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "f7e62548-51dd-4c1f-9223-23c369c888a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "venuedict = {}\n",
    "for row in matchlist:\n",
    "    venue = row[0]  # Leftmost column\n",
    "    if not(venue in venuedict.keys()):\n",
    "        venuedict[venue] = \"Something\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a6a8762d-4186-403e-b4c4-f6ceffa39bb1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Mexico City': 'Something',\n",
       " 'Zapopan': 'Something',\n",
       " 'Toronto': 'Something',\n",
       " 'Inglewood': 'Something',\n",
       " 'Foxborough': 'Something',\n",
       " 'East Rutherford': 'Something',\n",
       " 'Philadelphia': 'Something',\n",
       " 'Seattle': 'Something',\n",
       " 'Atlanta': 'Something',\n",
       " 'Santa Clara': 'Something',\n",
       " 'Houston': 'Something',\n",
       " 'Kansas City': 'Something',\n",
       " 'Miami Gardens': 'Something',\n",
       " 'Arlington': 'Something',\n",
       " 'Guadalupe': 'Something',\n",
       " 'Vancouver': 'Something'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "venuedict"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a86521fe-0894-4243-8a55-5380719c982b",
   "metadata": {},
   "source": [
    "- Can extract keys of a dictionary\n",
    "    - Similar to, but not quite a list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "c348afda-a01c-4fda-8485-812dad911714",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['Mexico City', 'Zapopan', 'Toronto', 'Inglewood', 'Foxborough', 'East Rutherford', 'Philadelphia', 'Seattle', 'Atlanta', 'Santa Clara', 'Houston', 'Kansas City', 'Miami Gardens', 'Arlington', 'Guadalupe', 'Vancouver'])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "venuedict.keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc8dfa32-4f06-4d3d-b36c-623417c1616f",
   "metadata": {},
   "source": [
    "- By convention, Python assumes you mean `d.keys()` if you say `v in d` for a dictionary `d`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "17f81fe6-1457-410e-bd8a-8056c847a361",
   "metadata": {},
   "outputs": [],
   "source": [
    "venuedict2 = {}\n",
    "for row in matchlist:\n",
    "    venue = row[0]  # Leftmost column\n",
    "    if not(venue in venuedict2):  # Short form for venue in venuedict2.keys()\n",
    "        venuedict2[venue] = \"Something\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "5f4f0d1a-a547-4ebb-89f6-7656366268af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Mexico City': 'Something',\n",
       " 'Zapopan': 'Something',\n",
       " 'Toronto': 'Something',\n",
       " 'Inglewood': 'Something',\n",
       " 'Foxborough': 'Something',\n",
       " 'East Rutherford': 'Something',\n",
       " 'Philadelphia': 'Something',\n",
       " 'Seattle': 'Something',\n",
       " 'Atlanta': 'Something',\n",
       " 'Santa Clara': 'Something',\n",
       " 'Houston': 'Something',\n",
       " 'Kansas City': 'Something',\n",
       " 'Miami Gardens': 'Something',\n",
       " 'Arlington': 'Something',\n",
       " 'Guadalupe': 'Something',\n",
       " 'Vancouver': 'Something'}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "venuedict2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7283613-8cf5-40af-81ce-957abddd752e",
   "metadata": {},
   "source": [
    "- Can also count the number of matches at each venue\n",
    "- For each key, maintain a counter\n",
    "    - When the key is created, initialize the counter to 1\n",
    "    - If the key already exists, increment the counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "bb27be65-23fb-4148-8a46-c7f21be5348b",
   "metadata": {},
   "outputs": [],
   "source": [
    "venuedict3 = {}\n",
    "for row in matchlist:\n",
    "    venue = row[0]  # Leftmost column\n",
    "    if not(venue in venuedict3):\n",
    "        venuedict3[venue] = 1\n",
    "    else:\n",
    "        venuedict3[venue] = venuedict3[venue] + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "232c8279-c897-46f1-9232-26203837e1fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Mexico City': 6,\n",
       " 'Zapopan': 2,\n",
       " 'Toronto': 5,\n",
       " 'Inglewood': 8,\n",
       " 'Foxborough': 8,\n",
       " 'East Rutherford': 9,\n",
       " 'Philadelphia': 6,\n",
       " 'Seattle': 7,\n",
       " 'Atlanta': 7,\n",
       " 'Santa Clara': 7,\n",
       " 'Houston': 6,\n",
       " 'Kansas City': 7,\n",
       " 'Miami Gardens': 9,\n",
       " 'Arlington': 8,\n",
       " 'Guadalupe': 5,\n",
       " 'Vancouver': 4}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "venuedict3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa449823-fae8-471f-8f1a-d2354cdcb206",
   "metadata": {},
   "source": [
    "- Can invert the condition and exchange the two parts of the `if`\n",
    "- More readable if we avoid negated conditions!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "ff50b0c6-e0af-4ef0-a871-5f46d391f7b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "venuedict4 = {}\n",
    "for row in matchlist:\n",
    "    venue = row[0]  # Leftmost column\n",
    "    if venue in venuedict4:\n",
    "        venuedict4[venue] = venuedict4[venue] + 1\n",
    "    else:\n",
    "        venuedict4[venue] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "5cc7a37a-a48e-4c83-8042-bfb6511f4266",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Mexico City': 6,\n",
       " 'Zapopan': 2,\n",
       " 'Toronto': 5,\n",
       " 'Inglewood': 8,\n",
       " 'Foxborough': 8,\n",
       " 'East Rutherford': 9,\n",
       " 'Philadelphia': 6,\n",
       " 'Seattle': 7,\n",
       " 'Atlanta': 7,\n",
       " 'Santa Clara': 7,\n",
       " 'Houston': 6,\n",
       " 'Kansas City': 7,\n",
       " 'Miami Gardens': 9,\n",
       " 'Arlington': 8,\n",
       " 'Guadalupe': 5,\n",
       " 'Vancouver': 4}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "venuedict4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57a696da-6d6e-4b26-a3bd-b16890147559",
   "metadata": {},
   "source": [
    "## Which team played as Home Team at maximum number of venues?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc855945-c424-421c-be77-fbe9062cdc9d",
   "metadata": {},
   "source": [
    "- Maintain a dictionary where keys are teams\n",
    "- For each team, associate a list of venues where it has played as Home Team"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "76f9f99b-1d80-4179-8e29-d822bbcdfa75",
   "metadata": {},
   "outputs": [],
   "source": [
    "teamvenuedict = {}\n",
    "for row in matchlist:\n",
    "    venue = row[0]\n",
    "    hometeam = row[1]\n",
    "\n",
    "    # Update the dictionary for hometeam\n",
    "    if not (hometeam in teamvenuedict):\n",
    "        teamvenuedict[hometeam] = [venue]\n",
    "    else:\n",
    "        if not (venue in teamvenuedict[hometeam]):\n",
    "            teamvenuedict[hometeam] = teamvenuedict[hometeam] + [venue]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "f1ad3681-6607-4c59-94b7-e36a39e0d224",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'MEX': ['Mexico City', 'Atlanta'],\n",
       " 'KOR': ['Zapopan'],\n",
       " 'CAN': ['Toronto', 'Vancouver', 'Houston'],\n",
       " 'USA': ['Inglewood', 'Toronto', 'Santa Clara'],\n",
       " 'QAT': ['Foxborough'],\n",
       " 'BRA': ['East Rutherford', 'Houston', 'Arlington'],\n",
       " 'HAI': ['Philadelphia'],\n",
       " 'AUS': ['Seattle', 'Arlington'],\n",
       " 'GER': ['Atlanta', 'Foxborough'],\n",
       " 'NED': ['Santa Clara', 'Philadelphia', 'Guadalupe'],\n",
       " 'CIV': ['Houston', 'Arlington'],\n",
       " 'SWE': ['Kansas City'],\n",
       " 'ESP': ['Miami Gardens', 'Santa Clara', 'Inglewood', 'East Rutherford'],\n",
       " 'BEL': ['Arlington', 'Houston', 'Seattle'],\n",
       " 'KSA': ['Guadalupe'],\n",
       " 'IRN': ['Vancouver'],\n",
       " 'FRA': ['East Rutherford', 'Arlington', 'Foxborough', 'Miami Gardens'],\n",
       " 'IRQ': ['Foxborough'],\n",
       " 'ARG': ['Inglewood', 'Guadalupe', 'Miami Gardens', 'Kansas City'],\n",
       " 'AUT': ['Santa Clara'],\n",
       " 'POR': ['Atlanta', 'Kansas City', 'Toronto', 'Miami Gardens'],\n",
       " 'ENG': ['Miami Gardens', 'Seattle', 'Atlanta'],\n",
       " 'GHA': ['Houston'],\n",
       " 'UZB': ['Seattle'],\n",
       " 'CZE': ['Inglewood', 'Mexico City'],\n",
       " 'SUI': ['Mexico City', 'Vancouver', 'Seattle'],\n",
       " 'SCO': ['Arlington', 'East Rutherford'],\n",
       " 'TUR': ['Guadalupe', 'Inglewood'],\n",
       " 'ECU': ['Kansas City', 'Atlanta'],\n",
       " 'TUN': ['Seattle', 'Kansas City'],\n",
       " 'NZL': ['Miami Gardens', 'Arlington'],\n",
       " 'URU': ['East Rutherford', 'Miami Gardens'],\n",
       " 'NOR': ['Toronto', 'Foxborough', 'Miami Gardens'],\n",
       " 'JOR': ['Foxborough', 'Santa Clara'],\n",
       " 'COL': ['Philadelphia', 'Miami Gardens', 'Kansas City'],\n",
       " 'PAN': ['Mexico City', 'East Rutherford'],\n",
       " 'RSA': ['Zapopan', 'Inglewood'],\n",
       " 'BIH': ['Toronto'],\n",
       " 'MAR': ['Foxborough'],\n",
       " 'PAR': ['Santa Clara', 'Philadelphia'],\n",
       " 'CUW': ['Houston'],\n",
       " 'JPN': ['Seattle'],\n",
       " 'EGY': ['Guadalupe'],\n",
       " 'CPV': ['Philadelphia'],\n",
       " 'SEN': ['East Rutherford'],\n",
       " 'ALG': ['Inglewood'],\n",
       " 'COD': ['Atlanta'],\n",
       " 'CRO': ['Philadelphia']}"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "teamvenuedict"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3de1f8d-2019-4c50-aafe-e60810728ec1",
   "metadata": {},
   "source": [
    "- Again, may be more readable if we avoid (at least one) negated condition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "9b2031d8-8000-4378-965c-373fcedbbdbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "teamvenuedict = {}\n",
    "for row in matchlist:\n",
    "    venue = row[0]\n",
    "    hometeam = row[1]\n",
    "\n",
    "    # Update the dictionary for hometeam\n",
    "    if hometeam in teamvenuedict:\n",
    "        if not (venue in teamvenuedict[hometeam]):\n",
    "            teamvenuedict[hometeam] = teamvenuedict[hometeam] + [venue]     \n",
    "    else:\n",
    "        teamvenuedict[hometeam] = [venue]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb796af3-18ae-4fd2-a2a2-bf97acc1f7cc",
   "metadata": {},
   "source": [
    "- Display the dictionary as a list of key-value pairs\n",
    "- `{ k1:v1, k2:v2, ..., km:vm }`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "e7592299-5c9b-4ebc-ab98-d5e99d928e14",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'MEX': ['Mexico City', 'Atlanta'],\n",
       " 'KOR': ['Zapopan'],\n",
       " 'CAN': ['Toronto', 'Vancouver', 'Houston'],\n",
       " 'USA': ['Inglewood', 'Toronto', 'Santa Clara'],\n",
       " 'QAT': ['Foxborough'],\n",
       " 'BRA': ['East Rutherford', 'Houston', 'Arlington'],\n",
       " 'HAI': ['Philadelphia'],\n",
       " 'AUS': ['Seattle', 'Arlington'],\n",
       " 'GER': ['Atlanta', 'Foxborough'],\n",
       " 'NED': ['Santa Clara', 'Philadelphia', 'Guadalupe'],\n",
       " 'CIV': ['Houston', 'Arlington'],\n",
       " 'SWE': ['Kansas City'],\n",
       " 'ESP': ['Miami Gardens', 'Santa Clara', 'Inglewood', 'East Rutherford'],\n",
       " 'BEL': ['Arlington', 'Houston', 'Seattle'],\n",
       " 'KSA': ['Guadalupe'],\n",
       " 'IRN': ['Vancouver'],\n",
       " 'FRA': ['East Rutherford', 'Arlington', 'Foxborough', 'Miami Gardens'],\n",
       " 'IRQ': ['Foxborough'],\n",
       " 'ARG': ['Inglewood', 'Guadalupe', 'Miami Gardens', 'Kansas City'],\n",
       " 'AUT': ['Santa Clara'],\n",
       " 'POR': ['Atlanta', 'Kansas City', 'Toronto', 'Miami Gardens'],\n",
       " 'ENG': ['Miami Gardens', 'Seattle', 'Atlanta'],\n",
       " 'GHA': ['Houston'],\n",
       " 'UZB': ['Seattle'],\n",
       " 'CZE': ['Inglewood', 'Mexico City'],\n",
       " 'SUI': ['Mexico City', 'Vancouver', 'Seattle'],\n",
       " 'SCO': ['Arlington', 'East Rutherford'],\n",
       " 'TUR': ['Guadalupe', 'Inglewood'],\n",
       " 'ECU': ['Kansas City', 'Atlanta'],\n",
       " 'TUN': ['Seattle', 'Kansas City'],\n",
       " 'NZL': ['Miami Gardens', 'Arlington'],\n",
       " 'URU': ['East Rutherford', 'Miami Gardens'],\n",
       " 'NOR': ['Toronto', 'Foxborough', 'Miami Gardens'],\n",
       " 'JOR': ['Foxborough', 'Santa Clara'],\n",
       " 'COL': ['Philadelphia', 'Miami Gardens', 'Kansas City'],\n",
       " 'PAN': ['Mexico City', 'East Rutherford'],\n",
       " 'RSA': ['Zapopan', 'Inglewood'],\n",
       " 'BIH': ['Toronto'],\n",
       " 'MAR': ['Foxborough'],\n",
       " 'PAR': ['Santa Clara', 'Philadelphia'],\n",
       " 'CUW': ['Houston'],\n",
       " 'JPN': ['Seattle'],\n",
       " 'EGY': ['Guadalupe'],\n",
       " 'CPV': ['Philadelphia'],\n",
       " 'SEN': ['East Rutherford'],\n",
       " 'ALG': ['Inglewood'],\n",
       " 'COD': ['Atlanta'],\n",
       " 'CRO': ['Philadelphia']}"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "teamvenuedict"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50c9a71d-4319-422e-a193-ab719be18d70",
   "metadata": {},
   "source": [
    "- Run through the dictionary and record the team with max venues as Team 1\n",
    "- Keep track of team name and the number of venues"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "e4523efc-7f2c-40e3-b28a-81894075240e",
   "metadata": {},
   "outputs": [],
   "source": [
    "maxteam = \"\"\n",
    "maxvenues = 0\n",
    "for team in teamvenuedict:\n",
    "    if len(teamvenuedict[team]) > maxvenues:\n",
    "        maxteam = team\n",
    "        maxvenues = len(teamvenuedict[team])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3216905-e789-44cc-9673-d6b6b6ebc338",
   "metadata": {},
   "source": [
    "- Can display more than one value at a time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "e34035cb-424c-49b4-a3bb-85570d9b77eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('ESP', 4)"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "maxteam, maxvenues"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a23cb96-f779-4473-b105-377d0c008af6",
   "metadata": {},
   "source": [
    "## Is being Home Team an advantage?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33bef569-4f29-4361-8acd-fcea66bf5f16",
   "metadata": {},
   "source": [
    "- Count the rows where the home team won the match\n",
    "- Check if the fraction of such rows is above a given threshold\n",
    "- Should we count all matches or only those where there was a result (ignoring draws)?\n",
    "- The code below counts matches with draws and matches with results separately"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "8638d470-870c-46a7-8ee4-171890022ba0",
   "metadata": {},
   "outputs": [],
   "source": [
    "threshold = 0.6\n",
    "result = 0   # Number of matches with a result\n",
    "draw = 0     # Number of matches that were draws\n",
    "homewin = 0  # Number of matches won by the home team\n",
    "for row in matchlist:\n",
    "    # Was it a draw?\n",
    "    if row[3] + row[5] == row[4] + row[6]: # Note '==' for equality\n",
    "        draw = draw + 1\n",
    "    else: # There was a result\n",
    "        result = result+1\n",
    "    # Did the home team score more goals?\n",
    "    if row[3] + row[5] > row[4] + row[6]:\n",
    "        homewin = homewin + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cea0060-5f71-4f5b-8533-79f89269ac2e",
   "metadata": {},
   "source": [
    "- Is the ratio above the threshold?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "0d8bfe47-512c-44ee-b9b2-7f81183f06c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "homewin/result >= threshold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "ccdb1160-3df0-4134-a93e-9f967d0a5d27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6071428571428571"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "homewin/result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "a8f24cbd-f1ef-4213-b68a-fb3b27e4fd72",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "draw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "292c35da-f3f0-4a53-bb73-315778fc12a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "84"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "9c699005-4d8a-4713-9d6b-8e3b43e7a512",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "51"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "homewin"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f363729e-2c86-4d87-be58-8de4471c2097",
   "metadata": {},
   "source": [
    "- What if we had defined *home team advantage* in terms of all matches, including draws?\n",
    "- Outcome is negative\n",
    "- Important to check whether what you are computing matches the intent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "16771c96-2888-4f33-9a89-edb0ee536015",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.49038461538461536"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "homewin/(result + draw)"
   ]
  }
 ],
 "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
}
