{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a5372204",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Ranges\n",
    "#### CS 65/STEM 280: Introduction to Computer Science I"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d88bde03",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## The `range()` function\n",
    "\n",
    "Let's take a look at the `range()` function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fe602d95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(1, 10)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "range(1,10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "766db901",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "A `range` is a new type - it's a sequence like a list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3ec5baf1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(range(1,10))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69a05bcb",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "`range(start,end)` creates a sequence of numbers that begin with `start` and ending _just before_ `end`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b8898784",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(1,10))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09cde3d2",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## For loops with `range()`\n",
    "\n",
    "The `range()` function is often used with `for` loops to create counter-controlled loops where you don't have to explicitly manage the counter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f2d33fe4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is iteration 1\n",
      "This is iteration 2\n",
      "This is iteration 3\n",
      "This is iteration 4\n",
      "This is iteration 5\n",
      "This is iteration 6\n",
      "This is iteration 7\n",
      "This is iteration 8\n",
      "This is iteration 9\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    print(\"This is iteration\",i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40b0d73f",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Arguments to `range()`\n",
    "\n",
    "The `range()` function can also take 1 or 3 arguments instead of just 2.\n",
    "\n",
    "When used with 1 argument, the start is implied to be 0.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "92c33030",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff196975",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "When used with 3 arguments, the third argument tells it what to increment the counter by."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7530e8a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10,2):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e6af558",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "You can even use this to count backwards."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "0a5f022d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "9\n",
      "8\n",
      "7\n",
      "6\n",
      "5\n",
      "4\n",
      "3\n",
      "2\n",
      "1\n",
      "Blast off!\n"
     ]
    }
   ],
   "source": [
    "for i in range(10,0,-1):\n",
    "    print(i)\n",
    "print(\"Blast off!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cda06341",
   "metadata": {},
   "source": [
    "## Group Discussion\n",
    "\n",
    "For each of the following loops, predict what will be displayed, then run it to see if you were right."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da4062dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "for num in range(1,5):\n",
    "    print(num)\n",
    "    \n",
    "for num in range(5,10):\n",
    "    print(num)\n",
    "    \n",
    "for num in range(0,5,2):\n",
    "    print(num)\n",
    "\n",
    "for num in range(5,2,-1):\n",
    "    print(num)\n",
    "    \n",
    "for num in range(5):\n",
    "    print(num)\n",
    "    \n",
    "for num in range(9,10):\n",
    "    print(num)\n",
    "    \n",
    "for num in range(10,10):\n",
    "    print(num)\n",
    "    \n",
    "for num in range(1,10,100):\n",
    "    print(num)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c6549ef",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Using for loops with `range()` and lists\n",
    "\n",
    "`range()` is often used with a `for` loop when you prefer to access the items using an index\n",
    "\n",
    "For example, consider this while loop we wrote in a previous lab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f30e21fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Day 0 had 0.0 inches of rainfall.\n",
      "Day 1 had 0.3 inches of rainfall.\n",
      "Day 2 had 0.71 inches of rainfall.\n",
      "Day 3 had 0.0 inches of rainfall.\n",
      "Day 4 had 0.32 inches of rainfall.\n",
      "Day 5 had 1.1 inches of rainfall.\n",
      "Day 6 had 0.4 inches of rainfall.\n"
     ]
    }
   ],
   "source": [
    "rainfall = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]\n",
    "\n",
    "counter = 0\n",
    "while counter < len(rainfall):\n",
    "    print(\"Day\",counter,\"had\",rainfall[counter],\"inches of rainfall.\")\n",
    "    counter += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d13c17e4",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "If we want to do this with a `for` loop, we lose the ability to print the day number. We would have to explicity keep track of a counter again - so then we might as well just use a `while` loop."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "45d695b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Day ? had 0.0 inches of rainfall.\n",
      "Day ? had 0.3 inches of rainfall.\n",
      "Day ? had 0.71 inches of rainfall.\n",
      "Day ? had 0.0 inches of rainfall.\n",
      "Day ? had 0.32 inches of rainfall.\n",
      "Day ? had 1.1 inches of rainfall.\n",
      "Day ? had 0.4 inches of rainfall.\n"
     ]
    }
   ],
   "source": [
    "rainfall = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]\n",
    "\n",
    "for amount in rainfall:\n",
    "    print(\"Day ? had\",amount,\"inches of rainfall.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f8a1bca",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Instead, we could make a `range()` with all the indices of our list by using the `len()` of the list:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b0668045",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list( range(len(rainfall)) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "bf326d1c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Day 0 had 0.0 inches of rainfall.\n",
      "Day 1 had 0.3 inches of rainfall.\n",
      "Day 2 had 0.71 inches of rainfall.\n",
      "Day 3 had 0.0 inches of rainfall.\n",
      "Day 4 had 0.32 inches of rainfall.\n",
      "Day 5 had 1.1 inches of rainfall.\n",
      "Day 6 had 0.4 inches of rainfall.\n"
     ]
    }
   ],
   "source": [
    "for counter in range(len(rainfall)):\n",
    "    print(\"Day\",counter,\"had\",rainfall[counter],\"inches of rainfall.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "346cab5b",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## In summary\n",
    "\n",
    "When using a `for` loop to iterate through a list, always determine which you are doing:\n",
    "* do I want the loop variable to be the items in the list \n",
    "    - then don't use `range()`\n",
    "    - don't use `[]` notation for your list, just use the loop variable\n",
    "* do I want the loop variable to be the indices of the list \n",
    "    - then use `range(len(name_of_list))`\n",
    "    - use `[loop_counter]` to access items in the list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9dc9a18d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Day ? had 0.0 inches of rainfall.\n",
      "Day ? had 0.3 inches of rainfall.\n",
      "Day ? had 0.71 inches of rainfall.\n",
      "Day ? had 0.0 inches of rainfall.\n",
      "Day ? had 0.32 inches of rainfall.\n",
      "Day ? had 1.1 inches of rainfall.\n",
      "Day ? had 0.4 inches of rainfall.\n",
      "Day 0 had 0.0 inches of rainfall.\n",
      "Day 1 had 0.3 inches of rainfall.\n",
      "Day 2 had 0.71 inches of rainfall.\n",
      "Day 3 had 0.0 inches of rainfall.\n",
      "Day 4 had 0.32 inches of rainfall.\n",
      "Day 5 had 1.1 inches of rainfall.\n",
      "Day 6 had 0.4 inches of rainfall.\n"
     ]
    }
   ],
   "source": [
    "rainfall = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]\n",
    "\n",
    "for amount in rainfall:\n",
    "    print(\"Day ? had\",amount,\"inches of rainfall.\")\n",
    "    \n",
    "for counter in range(len(rainfall)):\n",
    "    print(\"Day\",counter,\"had\",rainfall[counter],\"inches of rainfall.\")   "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38298654",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "It's really easy to mix these up - be ready to look for this if your loops are working."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5e63f9ae",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "list indices must be integers or slices, not float",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-22-2abd1659805b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mx\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrainfall\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[0mrainfall\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"inches of rainfall.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: list indices must be integers or slices, not float"
     ]
    }
   ],
   "source": [
    "for x in rainfall:\n",
    "    print(rainfall[x],\"inches of rainfall.\")"
   ]
  }
 ],
 "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
}
