{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "beginning-bacon",
   "metadata": {},
   "source": [
    "<!--\n",
    "---\n",
    "title: Control Flow\n",
    "type: lesson\n",
    "duration: \"1:10\"\n",
    "creator: Sonyl Nagale adapted from Susi Remondi\n",
    "Private gist location: xxxxxx\n",
    "Presentation URL: xxxxx\n",
    "---\n",
    "-->\n",
    "\n",
    "<h1>Python Programming: Conditionals</h1>\n",
    "\n",
    "\n",
    "<!--\n",
    "\n",
    "## Overview\n",
    "This lesson introduces students to the concept of control flow — Booleans (including \"truthy\" and \"falsey\"), comparison operators, and `if`/`elif`/`else`.\n",
    "\n",
    "## Learning Objectives\n",
    "In this lesson, students will:\n",
    "- Use comparison and equality operators to evaluate and compare statements.\n",
    "- Use `if`/`elif`/`else` conditionals to control program flow.\n",
    "\n",
    "## Duration\n",
    "60 minutes\n",
    "\n",
    "## Suggested Agenda\n",
    "\n",
    "| Time | Activity |\n",
    "| --- | --- |\n",
    "| 0:00 - 0:05 | Welcome |\n",
    "| 0:07 - 0:25 | Booleans and Logical Operators |\n",
    "| 0:25 - 0:47 | `if`, `else`, and `elif` |\n",
    "| 0:47 - 0:57 | Exercises |\n",
    "| 0:57 - 0:60 | Summary |\n",
    "\n",
    "## Before Class: Preparation\n",
    "- Modify the slides and exercises in this file as you see fit.\n",
    "\n",
    "## In Class: Materials\n",
    "- Projector\n",
    "- Internet connection\n",
    "- Python 3\n",
    "\n",
    "## Differentiation and Extensions\n",
    "- If students are excelling, give them more complex exercises and examples — chain comparisons inside `if` statements and make use of `Truthy`/`Falsey`.\n",
    "- If students are struggling, continuously recap the real-world comparison (e.g., a temperature range or comparing numbers), and once they grasp that, more slowly recap each individual concept. Start with the variables and an easier comparison, then build up. You can always add more simple demos, code-alongs, and exercises.\n",
    "- Feel free to come up with more exercises at the end (and throughout)! If there is extra time, write more exercises on the board (or bring up sample code) for students to complete.\n",
    "\n",
    "-->\n",
    "\n",
    "---\n",
    "\n",
    "## Learning Objectives\n",
    "*After this lesson, you will be able to:*\n",
    "\n",
    "- Use comparison and equality operators to evaluate and compare statements.\n",
    "- Use `if`/`elif`/`else` conditionals to control program flow.\n",
    "\n",
    "---\n",
    "\n",
    "## Discussion: What Do You Notice?\n",
    "\n",
    "Consider the following pseudocode for \"French toast à la GA.\"\n",
    "\n",
    "1. Dip the bread in eggs.\n",
    "\n",
    "2. Cook the bread for 3 minutes on each side.\n",
    "\n",
    "Now, consider this:\n",
    "\n",
    "1. Dip the bread in eggs.\n",
    "\n",
    "2. If the bread is thicker, dip the bread again until it's soaked through.\n",
    "\n",
    "3. Cook the bread for 3 minutes.\n",
    "\n",
    "4. Check if the bread is brown on the bottom. If not, keep cooking the bread.\n",
    "\n",
    "5. Flip the bread, and repeat steps 3 and 4.\n",
    "\n",
    "What do you notice?\n",
    "\n",
    "---\n",
    "\n",
    "## Saying \"Yes\" or \"No\"\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "completed-brush",
   "metadata": {},
   "source": [
    "- **If** the bread is thicker…\n",
    "- **If** the bread is brown…"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "valued-settle",
   "metadata": {},
   "source": [
    "Goal: Programs need to make choices.\n",
    "\n",
    "To do that, programs need to be able to say, \"Is this bread thick? Yes or no?\"\n",
    "\n",
    "Question: How does a computer say \"yes\" or \"no\"?\n",
    "\n",
    "---\n",
    "\n",
    "## Boolean Values: The Foundation of Programming\n",
    "\n",
    "\"Yes\" in computer is `True`.\n",
    "\"No\" in computer is `False`.\n",
    "\n",
    "This is the case in every programming language — it's specific to computers themselves.\n",
    "\n",
    "These are called **Boolean values**.\n",
    "\n",
    "- Is the bread sliced?\n",
    "    - `True`.\n",
    "- Is the bread brown?\n",
    "    - `False`.\n",
    "- Is 2 larger than 6?\n",
    "    - `False`.\n",
    "- Is 6 larger than 2?\n",
    "    - `True`.\n",
    "\n",
    "---\n",
    "\n",
    "## Comparison and Logic in Programming\n",
    "\n",
    "Now we can say \"yes\" or \"no,\" but how do we ask the question?\n",
    "\n",
    "The first way is with comparison operators.\n",
    "\n",
    "How does a computer decide `True` or `False`?\n",
    "\n",
    "![](https://s3.amazonaws.com/ga-instruction/assets/python-fundamentals/comparison_operators.png)\n",
    "\n",
    "\n",
    "---\n",
    "\n",
    "## Comparison Types Practice\n",
    "\n",
    "\n",
    "Check out these comparison operators. Why do you think the last one is `False`?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "marked-logic",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"3 < 5 is...\", (3 < 5))\n",
    "\n",
    "print(\"13 >= 13 is....\", (13 >= 13))\n",
    "\n",
    "print(\"50 > 100 is...\", (50 > 100))\n",
    "\n",
    "print(\"'d' < 'a' is...\", (\"d\" < \"a\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "automotive-threat",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## Equality Operators: Equality (`==`)\n",
    "\n",
    "- Accept any two types of data as inputs.\n",
    "- Will only evaluate to `True` if both sides are completely identical in *data type and value*."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "arctic-transport",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"5 == 5 is..\", 5 == 5)\n",
    "\n",
    "print(\"6 == 3 is...\", 6 == 3)\n",
    "\n",
    "print(\"'5' == 5 is..\", \"5\" == 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acceptable-match",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## Equality Operators: Inequality (`!=`):\n",
    "\n",
    "\n",
    "- Will accept any two types of data as inputs.\n",
    "- The reverse of the equality operator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "contained-modification",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"5 != 5 is..\", (5 != 5))\n",
    "\n",
    "print(\"6 != 5 is..\", (6 != 5))\n",
    "\n",
    "print(\"'5' != 5 is..\", (\"5\" != 5))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abstract-hindu",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## Comparison Operators: Knowledge Check\n",
    "\n",
    "\n",
    "What do you think the following will equal?\n",
    "\n",
    "- `8 > 8`\n",
    "\n",
    "- `8 >= 8`\n",
    "\n",
    "- `8 <= 15`\n",
    "\n",
    "- `7 != \"7\"`\n",
    "\n",
    "- `6 == 7`\n",
    "\n",
    "- `6 != 7`\n",
    "\n",
    "---\n",
    "\n",
    "## \"Truthy\" and \"Falsey\"\n",
    "\n",
    "\n",
    "Something that's `True` is always **true**… right?\n",
    "\n",
    "Yes, I totally cleaned my room. Just don't look under the bed…\n",
    "\n",
    "\n",
    "Sometimes, we need \"truthy\" and \"falsey.\" They're not explicitly `True` or `False`, but implicitly behave in the same way.\n",
    "\n",
    "Sometimes, `True` and `False` really mean, \"Is there anything there?\"\n",
    "\n",
    "- `\"Hello, World!\"`  - A non-empty string: Truthy / True.\n",
    "- `13`              - A non-zero number: Truthy / True.\n",
    "- `\"\"`               - An empty string: Falsey / False.\n",
    "- `0`                - The number 0: Falsey / False.\n",
    "\n",
    "---\n",
    "\n",
    "## The Logical Operators: `or` and `and`\n",
    "\n",
    "What if we need to check multiple things that must all be `True`?\n",
    "\n",
    "\n",
    "\n",
    "- To make a peanut butter and jelly sandwich, we need peanut butter, _and_ jelly, _and_ bread.\n",
    "\n",
    "\n",
    "\n",
    "Or check multiple things and only one needs to be `True`?\n",
    "\n",
    "\n",
    "\n",
    "- To make a fruit salad, we only need oranges, _or_ apples, _or_ strawberries.\n",
    "\n",
    "\n",
    "---\n",
    "\n",
    "## The Logical Operators: `or`\n",
    "\n",
    "`or` checks if **either** comparison is `True` and returns the first `True` value it finds. If neither side is `True`, then `or` returns `False` and the last `False` value.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "mathematical-merchant",
   "metadata": {},
   "outputs": [],
   "source": [
    "red_score = 7\n",
    "blue_score = 5\n",
    "green_score = 0\n",
    "yellow_score = 0\n",
    "\n",
    "# or prints the first truthy statement.\n",
    "print(red_score or blue_score)\n",
    "# 0 is considered False\n",
    "print(green_score or blue_score)\n",
    "# If all are false, or prints the last False statement.\n",
    "print(green_score or yellow_score)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "resident-undergraduate",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## The Logical Operators: `or` Truth Table\n",
    "\n",
    "The `or` truth table:\n",
    "\n",
    "- True or True => True\n",
    "- True or False => True\n",
    "- False or True => True\n",
    "- False or False => False\n",
    "\n",
    "---\n",
    "\n",
    "## The Logical Operators: `and`\n",
    "\n",
    "`and` checks if **both** comparisons are `True`. If both sides are `True`, then `and` will give back the last `True` value. If either side is `False`, `and` will return the first `False` value it finds."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "respected-antique",
   "metadata": {},
   "outputs": [],
   "source": [
    "red_score = 7\n",
    "blue_score = 5\n",
    "green_score = 0\n",
    "yellow_score = 0\n",
    "\n",
    "# and returns the last True statement.\n",
    "print(red_score and blue_score)\n",
    "# and returns the first False statement.\n",
    "print(green_score and blue_score)\n",
    "print(green_score and yellow_score)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "blank-dealer",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## The Logical Operators: `and` Truth Table\n",
    "\n",
    "\n",
    "The `and` truth table:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "neutral-sellers",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(True and True)\n",
    "# => True\n",
    "print(True and False)\n",
    "# => False\n",
    "print(False and True)\n",
    "# => False\n",
    "print(False and False)\n",
    "# => False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "proof-machine",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## Quick Review: Comparing Variables Using Operators\n",
    "\n",
    "- When comparing, a computer always returns a Boolean value: `True` or `False`.\n",
    "\n",
    "- We compare with operators like `<`, `<=`, `>`, `>=`, `==`, and `!=`.\n",
    "\n",
    "- We can also use the logical operators `and` and `or`.\n",
    "\n",
    "*Pro tip: Using only one equal (`=`) always assigns the variable!*\n",
    "\n",
    "Up next: Conditionals.\n",
    "\n",
    "---\n",
    "\n",
    "## Conditionals: `if`\n",
    "\n",
    "Do you remember this?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "threatened-harvey",
   "metadata": {},
   "source": [
    "- **If** the bread is thicker…\n",
    "- **If** the bread is brown…"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "tender-beach",
   "metadata": {},
   "source": [
    "\n",
    "How can we put that in a program?\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "changed-history",
   "metadata": {},
   "source": [
    "if the bread is thick\n",
    "\n",
    "    # print(\"Dunk the bread longer!\")\n",
    "\n",
    "\\# No matter what:\n",
    "print(\"Finished dunking the bread\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fantastic-clark",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## `if` Syntax\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "united-freeware",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "interested-female",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## We Do: It's Too Hot In Here\n",
    "\n",
    "Remember, in a We Do, you follow along!\n",
    "\n",
    "Our goal: A temperature program that lets us know when it is too hot. We'll be using the Kelvin scale for our temperature.\n",
    "\n",
    "- Set up a temperature variable.\n",
    "\n",
    "- Let's write a program that sets the temperature to 285 degrees Kelvin (about 50°F/10°C) and then immediately prints that it is too hot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "vanilla-singles",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "specified-thanksgiving",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## We Do: Add an `if` Statement\n",
    "\n",
    "That's not hot! Let's add an `if` statement:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "coordinated-occupation",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "brief-order",
   "metadata": {},
   "source": [
    "\n",
    "What about a higher temperature? Like `308`?\n",
    "\n",
    "---\n",
    "\n",
    "## We Do: The `else` Statement\n",
    "\n",
    "What about printing a message for when it isn't too hot?\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "parliamentary-burton",
   "metadata": {},
   "source": [
    "if condition:\n",
    "\n",
    "    # Do something\n",
    "  \n",
    "else:\n",
    "\n",
    "    # Do something else\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "liked-transcript",
   "metadata": {},
   "source": [
    "\n",
    "The `else` block is executed **only** if the `if` condition evaluates to `False`.\n",
    "\n",
    "Let's try it:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "precious-things",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "enabling-affect",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## Discussion: Other Cases\n",
    "\n",
    "\n",
    "What if it's too cold? We need more conditions.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bridal-vessel",
   "metadata": {},
   "source": [
    "if temperature > 299:\n",
    "\n",
    "    # If it is too hot, run this code block.\n",
    "    \n",
    "    print(\"It's too hot!\")\n",
    "\n",
    "\\# We want: Else if temperature < 40.\n",
    "\n",
    "\\# We want: Print that it's too cold  .\n",
    "\n",
    "else:\n",
    "\n",
    "    # Otherwise, run this code block.\n",
    "    \n",
    "    print(\"It's just right!\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "numeric-messaging",
   "metadata": {},
   "source": [
    "\n",
    "What do you think we need?\n",
    "\n",
    "---\n",
    "\n",
    "\n",
    "## We Do: The `elif` Statement\n",
    "\n",
    "That's where the `elif` (\"else if\") statement works its magic.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "stretch-objective",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "editorial-display",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## We Do: Adding More `elif`\n",
    "\n",
    "\n",
    "We can have as many `elif` as we'd like, but only one `else`.\n",
    "\n",
    "Let's change this up — remember, type this out for practice.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cultural-placement",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "corporate-conversion",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## Thought Exercise\n",
    "\n",
    "What do you think the following code will print? Why? Try to think about the answer before running the code!\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "several-workshop",
   "metadata": {},
   "outputs": [],
   "source": [
    "foo = 5\n",
    "bar = 1\n",
    "if foo > 13:\n",
    "    print(\"Flip\")\n",
    "elif bar:\n",
    "    print(\"Flop\")\n",
    "else:\n",
    "    print(\"Fly\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dying-product",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## Exercise: Even or Odd\n",
    "\n",
    "Pair with a new partner. Decide who will drive and who will navigate.\n",
    "\n",
    "Write code that prints whether a number is even or odd.\n",
    "\n",
    "Do you remember how to determine that?\n",
    "\n",
    "- We can use the modulus operator (`%`) to check the remainder.\n",
    "\n",
    "Here is some code to get you started:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "behind-occasion",
   "metadata": {},
   "outputs": [],
   "source": [
    "number = 10\n",
    "remainder = number % 2\n",
    "# For an even number, print \"It's even!\"\n",
    "\n",
    "# For an odd number, print \"It's odd!\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "lesbian-baseball",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## Exercise: `and` and `or`\n",
    "\n",
    "Write code that compares two variables and prints out statements accordingly. Start here and follow this:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "scenic-dressing",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = True\n",
    "y = False\n",
    "a = True\n",
    "b = False\n",
    "\n",
    "# Check if x and b are both True. If they are, print \"Both of these are true.\"\n",
    "\n",
    "# Check if y or a is False. If one is, print \"One of these is false.\"\n",
    "\n",
    "# Check if either x or y is False. If one is, print out \"One is false.\"\n",
    "\n",
    "# Then, only if either x or y is False, check if x and a are both True. If they are, print out \"Both are true.\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "lonely-score",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## Summary: Boolean Values and Operators\n",
    "\n",
    "\n",
    "We've started control flow — changing what our program does based on a decision. We used:\n",
    "\n",
    "**Boolean values**\n",
    "\n",
    "- `True` and `False`.\n",
    "- The corresponding \"truthy\" and \"falsey\".\n",
    "\n",
    "**Conditional operators**\n",
    "\n",
    "- Comparison: `<`, `>`, `<=`, and `>=`.\n",
    "- Equality: `==` and `!=`.\n",
    "\n",
    "**Logical operators**: `all` and `or`\n",
    "\n",
    "* `or` evaluates to `True` if **any** of the comparisons are `True`.\n",
    "* `and` evaluates to `True` only if **all** of the comparisons are `True.`\n",
    "---\n",
    "\n",
    "## Summary and Q&A\n",
    "\n",
    "\n",
    "Then, we went into `if` and `else`:\n",
    "\n",
    "\"**If** your toast is thick, dip the bread for longer, **else** do not.\"\n",
    "\n",
    "* `if`: Use only as the first conditional operator.\n",
    "* `elif`: Adds multiple comparisons to your `if` blocks.\n",
    "* `else`: Use only at the end of your code block, for if the previous conditional tests are `False`."
   ]
  }
 ],
 "metadata": {
  "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.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
