{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5d3826d8",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# For Loops\n",
    "#### CS 65: Introduction to Computer Science I"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad3cf404",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Looping through lists\n",
    "\n",
    "So far, we've processed lists like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b535c3ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Monday is a good day for programming\n",
      "Tuesday is a good day for programming\n",
      "Wednesday is a good day for programming\n",
      "Thursday is a good day for programming\n",
      "Friday is a good day for programming\n"
     ]
    }
   ],
   "source": [
    "days_of_the_week = [\"Sunday\",\"Monday\",\"Tuesday\",\"Wednesday\",\"Thursday\",\"Friday\",\"Saturday\"]\n",
    "\n",
    "counter = 0\n",
    "while counter < len(days_of_the_week):\n",
    "    \n",
    "    if \"S\" not in days_of_the_week[counter]:\n",
    "        print( ,\"is a good day for programming\")\n",
    "        \n",
    "    counter += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8aae767d",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "This pattern is so common there is a special loop for doing it without having to manage the counter yourself."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "86367606",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Monday is a good day for programming\n",
      "Tuesday is a good day for programming\n",
      "Wednesday is a good day for programming\n",
      "Thursday is a good day for programming\n",
      "Friday is a good day for programming\n"
     ]
    }
   ],
   "source": [
    "days_of_the_week = [\"Sunday\",\"Monday\",\"Tuesday\",\"Wednesday\",\"Thursday\",\"Friday\",\"Saturday\"]\n",
    "\n",
    "\n",
    "for day in days_of_the_week:\n",
    "    if \"S\" not in day:\n",
    "        print(day,\"is a good day for programming\")\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cef42e6",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "_Notice:_ `day` became the value from the list for this iteration - we didn't have to do `days_of_the_week[counter]`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d1c4e29",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## For loop syntax\n",
    "\n",
    "* keyword `for`\n",
    "* a loop variable - something new that you define here for the first time\n",
    "    - it shouldn't be a variable you're already using for something else, this will change it!\n",
    "* keyword `in`\n",
    "* a sequence (like a list or a string)\n",
    "* a colon `:`\n",
    "* an indented block of code\n",
    "\n",
    "Let's rewrite something like previous loops we've seen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9d0cf302",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Average length is 5.735\n"
     ]
    }
   ],
   "source": [
    "with open(\"top_male_baby_names_2010s.txt\") as male_names_file:\n",
    "    \n",
    "    male_names = male_names_file.readlines()\n",
    "    \n",
    "    total_length = 0\n",
    "    \n",
    "    counter = 0\n",
    "    while counter < len(male_names):\n",
    "        male_names[counter] = male_names[counter].rstrip()\n",
    "        total_length += len(male_names[counter])\n",
    "        counter += 1\n",
    "        \n",
    "    if counter != 0:\n",
    "        average_length = total_length/counter\n",
    "        print(\"Average length is\",average_length)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c2997da2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Average length is 5.735\n"
     ]
    }
   ],
   "source": [
    "#let's rewrite it here as a for loop\n",
    "\n",
    "with open(\"top_male_baby_names_2010s.txt\") as male_names_file:\n",
    "    \n",
    "    male_names = male_names_file.readlines()\n",
    "    #print(male_names)\n",
    "    \n",
    "    total_length = 0\n",
    "    \n",
    "    for name in male_names:\n",
    "        name = name.rstrip()\n",
    "        total_length += len(name)\n",
    "        #print(name,len(name),total_length)\n",
    "        \n",
    "    if len(male_names) != 0:\n",
    "        average_length = total_length/len(male_names)\n",
    "        print(\"Average length is\",average_length)\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9288022a",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Average length is 5.735\n"
     ]
    }
   ],
   "source": [
    "with open(\"top_male_baby_names_2010s.txt\") as male_names_file:\n",
    "    \n",
    "    male_names = male_names_file.readlines()\n",
    "    \n",
    "    total_length = 0\n",
    "    \n",
    "    for name in male_names:\n",
    "        \n",
    "        name = name.rstrip()\n",
    "        total_length += len(name)\n",
    "        \n",
    "    if len(male_names) > 0:\n",
    "        average_length = total_length/len(male_names)\n",
    "        print(\"Average length is\",average_length)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16e854c9",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Group Exercises:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f8a23c2",
   "metadata": {},
   "source": [
    "Write a for loop that will diplay all the temperatures in the list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9a3272da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-5\n",
      "-15\n",
      "-8\n",
      "-1\n"
     ]
    }
   ],
   "source": [
    "temperatures = [30, 20, 2, -5, -15, -8, -1, 0, 5, 35]\n",
    "\n",
    "for temp in temperatures:\n",
    "    if temp < 0:\n",
    "        print(temp)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a3a41fe",
   "metadata": {},
   "source": [
    "Once you have that working, change it so that it only displays the negative temperatures."
   ]
  }
 ],
 "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
}
