{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "28d5597a",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Lists\n",
    "#### CS 65: Introduction to Computer Science I"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43102c14",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Containers\n",
    "\n",
    "Some types in Python are used for storing a collection of values. These types are called __containers__. \n",
    "\n",
    "__List:__ a container type that stores values in a ordered _sequence_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "730f71ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "doctor_who_actors = [\"McGann\",\"Eccleston\",\"Tennant\",\"Smith\",\"Capaldi\",\"Whittaker\"]\n",
    "rainfall_amounts = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]\n",
    "dates = [\"March 14, 2020\",2021,\"4/1\"]\n",
    "empty_list = []"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb6fc7f2",
   "metadata": {},
   "source": [
    "values in a list are called __items__ or __elements__, e.g., `\"Capaldi\"`, `0.3`\n",
    "\n",
    "Syntax: start with `[`, end with `]`, separate with `,`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "140d668c",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Accessing items in a list\n",
    "\n",
    "You can access individual items in a list using their __index__, which is their position in the list\n",
    "\n",
    "indices _start at 0_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "50c7eb6e",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Capaldi\n"
     ]
    }
   ],
   "source": [
    "doctor_who_actors = [\"McGann\",\"Eccleston\",\"Tennant\",\"Smith\",\"Capaldi\",\"Whittaker\"]\n",
    "print(doctor_who_actors[4]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7a78a9b2",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0\n"
     ]
    }
   ],
   "source": [
    "rainfall_amounts = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]\n",
    "print(rainfall_amounts[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac591fca",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Using index notation, you can treat an item in a list like any other variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "76ed8e5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.5"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rainfall_amounts = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]\n",
    "total_weekend_rainfall = rainfall_amounts[5] + rainfall_amounts[6] \n",
    "total_weekend_rainfall "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b888bfa",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "You will get an error if you try to access an item using an index that is too big for the list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "790ac4f1",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-6-91949d9bfcd0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mdoctor_who_actors\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m\"McGann\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"Eccleston\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"Tennant\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"Smith\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"Capaldi\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"Whittaker\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdoctor_who_actors\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "doctor_who_actors = [\"McGann\",\"Eccleston\",\"Tennant\",\"Smith\",\"Capaldi\",\"Whittaker\"]\n",
    "print(doctor_who_actors[7])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80a4fcd2",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "But, if you use a negative index, it will count from the back of the list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bbc92afe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Whittaker\n"
     ]
    }
   ],
   "source": [
    "doctor_who_actors = [\"McGann\",\"Eccleston\",\"Tennant\",\"Smith\",\"Capaldi\",\"Whittaker\"]\n",
    "print(doctor_who_actors[-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97268660",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Updating items in a list\n",
    "\n",
    "You can also use list notation to change values in a list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "523761d5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.0, 0.3, 0.65, 0.0, 0.32, 1.1, 0.4]\n"
     ]
    }
   ],
   "source": [
    "rainfall_amounts = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]\n",
    "rainfall_amounts[2] = 0.65\n",
    "print(rainfall_amounts)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3792e060",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Group Exercises:\n",
    "\n",
    "Fill in the blank:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "667b207e",
   "metadata": {},
   "outputs": [],
   "source": [
    "rainfall_amounts = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]\n",
    "day_num = int(input(\"Enter the number of a day 0-6: \"))\n",
    "print(\"Day\",day_num,\"had\",         ,\"inches of rainfall\") #Fill in the blank"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57df1cbc",
   "metadata": {},
   "source": [
    "Add the line of code that will update the list with the new value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd2842d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "rainfall_amounts = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]\n",
    "day_num = int(input(\"Enter the number of a day 0-6: \"))\n",
    "new_amt = float(input(\"Enter an updated rainfall amount for that day: \"))\n",
    "#new line of code here"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09db1820",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Functions\n",
    "\n",
    "There are several useful functions that you can use on lists.\n",
    "\n",
    "Some of them require you to pass the list as an argument"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5fa1e76e",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rainfall_amounts = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]\n",
    "len(rainfall_amounts) #length of the list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2d5a9198",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.1"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max(rainfall_amounts) #largest value in the list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95a5a63b",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Some of them require to use the dot notation. Whenever you use a function with dot notation, it is technically called a __method__. The distinction will become more important later, but for now, don't be confused if you see the word _method_ in documentation like here: https://docs.python.org/3/tutorial/datastructures.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4203ff5d",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rainfall_amounts.index(0.3) #which index can I find 0.3 at?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b258f4de",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rainfall_amounts.count(0.0) #how many times does 0.0 appear in the list?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c408a7f",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Group Exercises:\n",
    "\n",
    "Execute this code and explain what is happening."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89832b6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "doctor_who_actors = [\"McGann\",\"Eccleston\",\"Tennant\",\"Smith\",\"Capaldi\",\"Whittaker\"]\n",
    "print( len(doctor_who_actors) )\n",
    "print( max(doctor_who_actors) )\n",
    "\n",
    "rainfall_amounts = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]\n",
    "print( min(rainfall_amounts) )\n",
    "print( sum(rainfall_amounts) )\n",
    "\n",
    "rainfall_amounts.reverse()\n",
    "print(rainfall_amounts)\n",
    "\n",
    "rainfall_amounts.sort()\n",
    "print(rainfall_amounts)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eac28e4e",
   "metadata": {},
   "source": [
    "What do the `min()` and `sum()` functions do? \n",
    "\n",
    "What do the `reverse()` and `sort()` function do?\n",
    "\n",
    "Can you apply `sum()` too the `doctor_who_actors` list?\n",
    "\n",
    "Write the line of code that will tell you which index `\"Tennant\"` appears at in the `doctor_who_actors` list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "930adea7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "342a2e1f",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Testing membership in a list\n",
    "\n",
    "You can test if a value is in a list using the `in` operator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f8a8dfa0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "doctor_who_actors = [\"McGann\",\"Eccleston\",\"Tennant\",\"Smith\",\"Capaldi\",\"Whittaker\"]\n",
    "print(\"Cushing\" in doctor_who_actors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9564d8fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a rainfall amount: 0.32\n",
      "that amount appears in the list\n"
     ]
    }
   ],
   "source": [
    "rainfall_amounts = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]\n",
    "amt = float(input(\"Enter a rainfall amount: \"))\n",
    "if amt in rainfall_amounts:\n",
    "    print(\"that amount appears in the list\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "373a827f",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Group Exercise\n",
    "\n",
    "Fill in the blank"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d017d210",
   "metadata": {},
   "outputs": [],
   "source": [
    "doctor_who_actors = [\"McGann\",\"Eccleston\",\"Tennant\",\"Smith\",\"Capaldi\",\"Whittaker\"]\n",
    "actor = input(\"Enter the name of an actor: \")\n",
    "if             :   #fill in the blank\n",
    "    print(actor,\"has played Doctor Who.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a08c59d",
   "metadata": {
    "hideCode": false,
    "hidePrompt": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Appending items to the end of a list\n",
    "\n",
    "Add new items to the end of a list using the `append()` list method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3de18d49",
   "metadata": {
    "hideCode": false,
    "hidePrompt": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4, 0.1]\n"
     ]
    }
   ],
   "source": [
    "rainfall_amounts = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]\n",
    "rainfall_amounts.append(0.1)\n",
    "print(rainfall_amounts)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87c044a5",
   "metadata": {
    "hideCode": false,
    "hidePrompt": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Inserting new items into the beginning/middle of a list\n",
    "\n",
    "Add new items to the beginning or middlle of a list using the `insert()` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "30ec3cbb",
   "metadata": {
    "hideCode": false,
    "hidePrompt": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['McGann', 'Hurt', 'Eccleston', 'Tennant', 'Smith', 'Capaldi', 'Whittaker']\n"
     ]
    }
   ],
   "source": [
    "doctor_who_actors = [\"McGann\",\"Eccleston\",\"Tennant\",\"Smith\",\"Capaldi\",\"Whittaker\"]\n",
    "doctor_who_actors.insert(1,\"Hurt\")\n",
    "print(doctor_who_actors)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "427e1235",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Group Exercise:\n",
    "\n",
    "A programmer wants to write a program that allows new rainfall amounts to appear at the beginning of the list. Here's the code they wrote for it, but they have an error in their code. Explain the problem and fix the code."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "611e59e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "rainfall_amounts = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]\n",
    "new_val = float(input(\"Enter a new amount: \"))\n",
    "rainfall_amounts.append(new_val)\n",
    "print(rainfall_amounts)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "521f3ca7",
   "metadata": {
    "hideCode": false,
    "hidePrompt": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Removing items by value\n",
    "\n",
    "When you know the value of the item you want to delete from the list, use the `remove()` method.\n",
    "\n",
    "This will only delete the first instance of the value.\n",
    "\n",
    "If the item isn't there, it will give an error."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "01e053d1",
   "metadata": {
    "hideCode": false,
    "hidePrompt": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.0, 0.3, 0.71, 0.0, 1.1, 0.4]\n"
     ]
    }
   ],
   "source": [
    "rainfall_amounts = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]\n",
    "rainfall_amounts.remove(0.32)\n",
    "print(rainfall_amounts)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9f11b90",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Removing items by index\n",
    "\n",
    "When you know the position of the item you want to delete, use the `pop()` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "20ce9e85",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.0, 0.3, 0.71, 0.0, 0.32, 0.4]\n"
     ]
    }
   ],
   "source": [
    "rainfall_amounts = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]\n",
    "rainfall_amounts.pop(5)\n",
    "print(rainfall_amounts)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f25d9aaf",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Group Exercise:\n",
    "\n",
    "A programmer wants to write a program that allows the user to enter a rain amount and then remove that if it is in the list. However, when they test this code, some of the tests cause the program to crash. Explain the problem and write the code that will fix it (_Hint: you might need to add an `if` statement_)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b60232da",
   "metadata": {},
   "outputs": [],
   "source": [
    "rainfall_amounts = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]\n",
    "val_to_remove = float(input(\"Enter a value to remove: \"))\n",
    "rainfall_amounts.remove(val_to_remove)\n",
    "print(rainfall_amounts)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
