{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "CPSC217_5Decisions.ipynb", "provenance": [], "collapsed_sections": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "Dap8bd9lwxVG" }, "source": [ "# Topic 5: Decisions: Booleans" ] }, { "cell_type": "markdown", "metadata": { "id": "FM8AXGw3x2v9" }, "source": [ "## Booleans" ] }, { "cell_type": "markdown", "metadata": { "id": "ewBQdM0-xYJ8" }, "source": [ "We can store the idea of true of false" ] }, { "cell_type": "code", "metadata": { "id": "ZL9Ifd0dxasd" }, "source": [ "x = True\n", "y = False\n", "print(type(x))\n", "print(type(y))" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "32dZxi-mx4zV" }, "source": [ "## Relational operators" ] }, { "cell_type": "markdown", "metadata": { "id": "y4GIPz-mxdEd" }, "source": [ "We can get these also from relating things (Relational operators)" ] }, { "cell_type": "code", "metadata": { "id": "0wS-WvLwxfyt" }, "source": [ "3 < 5" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "X678DlCCxhPM" }, "source": [ "5 > 3" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "sbEM0MEVxiLN" }, "source": [ "3 == 3" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "bQy9F376xjC9" }, "source": [ "5 <= 5" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "1de75eRqxkC1" }, "source": [ "3 >= 4" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "tH3-S0DZxoL1" }, "source": [ "#Not the same =/=\n", "#More shared with other languages\n", "5 != 3" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "71G4Zv1jxyPc" }, "source": [ "x = 1.0\n", "y = 2.0\n", "c = (x <= y)\n", "print (type(c))\n", "print(c)" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "t3jiYsIw3iN0" }, "source": [ "print(int(True))\n", "print(int(False))" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "CEhsLmEOx-pt" }, "source": [ "## Boolean operators" ] }, { "cell_type": "markdown", "metadata": { "id": "dJo_FBKIx_wF" }, "source": [ "We can combine things that are already boolean" ] }, { "cell_type": "code", "metadata": { "id": "6p1cRtrmyDuW" }, "source": [ "a = True\n", "b = True\n", "c = False" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "2CMChlyfyJSN" }, "source": [ "a and b" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "zUAGbXlQyONV" }, "source": [ "a and c" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "IgR-PbEcyPeW" }, "source": [ "a or c" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "Hrk6ie91ySZV" }, "source": [ "not a" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "tPJpixkYyU4d" }, "source": [ "## Precedence" ] }, { "cell_type": "markdown", "metadata": { "id": "PZQfWcgcyrI2" }, "source": [ "Precedence is for math operators first, then relational, then booleans, and finally assignment to a variable" ] }, { "cell_type": "code", "metadata": { "id": "rUYb74y6yWz9" }, "source": [ "not True and False or True" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "srHL19hoyfOG" }, "source": [ "((not True) and False) or True" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "u3jw46SryfbF" }, "source": [ "(not True) and (False or True)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "wwK5CwZMzAVv" }, "source": [ "Things are always better with brackets" ] }, { "cell_type": "code", "metadata": { "id": "wMMtzA7Eyzou" }, "source": [ "not 6 == 2 * 3 and False or True" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "vZCs-AVGzC1a" }, "source": [ "((not (6 == (2 * 3))) and False) or True" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "IQCTV4yqzICv" }, "source": [ "##Truth tables " ] }, { "cell_type": "code", "metadata": { "id": "UaJaRQRm7X9l" }, "source": [ "a = 1\n", "b = 0\n", "print(a and b)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "8KRxokSTzRJm" }, "source": [ "How we write down how a boolean opreator, or expression is evaluated for different inputs" ] }, { "cell_type": "code", "metadata": { "id": "eL4JO9N6zQh_" }, "source": [ "print(\"A\",\"B\",\"A or B\", sep=\"\\t\")\n", "A = True\n", "B = True\n", "print(A,B,A or B, sep=\"\\t\")\n", "A = True\n", "B = False\n", "print(A,B,A or B, sep=\"\\t\")\n", "A = False\n", "B = True\n", "print(A,B,A or B, sep=\"\\t\")\n", "A = False\n", "B = False\n", "print(A,B,A or B, sep=\"\\t\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "9-Dm4J07z1Ho" }, "source": [ "print(\"A\",\"B\",\"A and B\", sep=\"\\t\")\n", "A = True\n", "B = True\n", "print(A,B,A and B, sep=\"\\t\")\n", "A = True\n", "B = False\n", "print(A,B,A and B, sep=\"\\t\")\n", "A = False\n", "B = True\n", "print(A,B,A and B, sep=\"\\t\")\n", "A = False\n", "B = False\n", "print(A,B,A and B, sep=\"\\t\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "_hkBN2Ifz4_W" }, "source": [ "print(\"A\",\"not A\", sep=\"\\t\")\n", "A = True\n", "print(A, not A, sep=\"\\t\")\n", "A = False\n", "print(A, not A, sep=\"\\t\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "IDPTItLvw2H1" }, "source": [ "# Topic 5: Decisions: IfElse" ] }, { "cell_type": "markdown", "metadata": { "id": "yNe4BHCj0TXV" }, "source": [ "So we know have the idea of yes or no \n", "\n", "That let's us make decisions" ] }, { "cell_type": "markdown", "metadata": { "id": "W5tcSvvw0afu" }, "source": [ "##If" ] }, { "cell_type": "markdown", "metadata": { "id": "A_Ut3xB-1Ak7" }, "source": [ "Do something if condition, or a boolean expression, or a boolean variable is True" ] }, { "cell_type": "code", "metadata": { "id": "5xGmuNkO0xG-" }, "source": [ "print(\"this happens before\")\n", "if True:\n", " print(\"this is printed always as condition always True\")\n", "print(\"this happens after\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "R2lZcmba04CX" }, "source": [ "print(\"this happens before\")\n", "if False:\n", " print(\"this is never printed\")\n", "print(\"this happens after\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "C2RKQYT50dQN" }, "source": [ "age = int(input(\"Enter age:\"))\n", "if age < 18:\n", " print(\"You are underage!\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "nLXbwKwm09Mq" }, "source": [ "##IfElse" ] }, { "cell_type": "code", "metadata": { "id": "fBtifUeD1TIO" }, "source": [ "age = int(input(\"Enter age:\"))\n", "cond = age < 18\n", "if cond:\n", " print(\"You are underage!\")\n", "if not cond:\n", " print(\"You are overage.\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "PJnSHJod1O3f" }, "source": [ "Natural to want to do something if the boolean was False without reinventing the condition" ] }, { "cell_type": "code", "metadata": { "id": "L_Txbdq90_Bu" }, "source": [ "age = int(input(\"Enter age:\"))\n", "cond = age < 18\n", "if cond:\n", " print(\"You are underage!\")\n", "else:\n", " print(\"You are overage.\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "MetC5eaS1m5p" }, "source": [ "print(\"this happens before\")\n", "if True:\n", " print(\"this is printed always as condition always True\")\n", "else:\n", " print(\"this is never printed\")\n", "print(\"this happens after\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "y08mRQ4D1rhG" }, "source": [ "print(\"this happens before\")\n", "if False:\n", " print(\"this is never printed\")\n", "else:\n", " print(\"this is printed if condition is false which is always true\")\n", "print(\"this happens after\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "-5pcFqhR1zUR" }, "source": [ "##Elif" ] }, { "cell_type": "markdown", "metadata": { "id": "Vr8V0IBu100o" }, "source": [ "What if we have a bunch of related conditions" ] }, { "cell_type": "code", "metadata": { "id": "rS8zoa1015KQ" }, "source": [ "age = int(input(\"Enter age:\"))\n", "cond1 = age < 17\n", "cond2 = age < 18\n", "if cond1:\n", " print(\"You are underage!\")\n", "if not cond1 and cond2:\n", " print(\"You are underage but almost not!\")\n", "if not cond2 and not cond1:\n", " print(\"You aren't underage.\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "2g0W-AwD2fc7" }, "source": [ "age = int(input(\"Enter age:\"))\n", "cond1 = age < 17\n", "cond2 = age < 18\n", "if cond1:\n", " print(\"You are underage!\")\n", "elif cond2:\n", " print(\"You are underage!\")\n", " print(\"You are underage but almost not!\")\n", "else:\n", " print(\"You aren't underage.\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "pB_Rq2ra3BaH" }, "source": [ "##Nesting" ] }, { "cell_type": "markdown", "metadata": { "id": "QUx1we6w3CR_" }, "source": [ "Sometimes is clearer to manage your condition logic by nesting it" ] }, { "cell_type": "code", "metadata": { "id": "JAUH9LLn3HX_" }, "source": [ "age = int(input(\"Enter age:\"))\n", "cond1 = age < 17\n", "cond2 = age < 18\n", "if cond2:\n", " print(\"You are underage!\")\n", " if not cond1:\n", " print(\"But you are almost not!\")\n", "else:\n", " print(\"You aren't underage.\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "xWE-vTBgHFSp" }, "source": [ "## Some simple mistakes" ] }, { "cell_type": "code", "metadata": { "id": "uYHCxUDKHG8v" }, "source": [ "#missing :\n", "if True\n", " print(\"x\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "HRvPsIUKHLtm" }, "source": [ "#indentation\n", "if False:\n", "\tprint(\"x\")\n", " print(\"z\")\n", " " ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "QYJcdwzEHuv6" }, "source": [ "#Greater than 0 is true\n", "if 1:\n", " print(\"hi\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "Tse-HW1UHxJs" }, "source": [ "#0 is False\n", "if 0:\n", " print(\"hi\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "OLw1lYAaHzs9" }, "source": [ "#Greater than 0 is true\n", "if 7:\n", " print(\"hi\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "sLneF0vBH7Nc" }, "source": [ "#Greater than 0 is true\n", "if 0.1:\n", " print(\"hi\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "Lw34u2ZiNLQ9" }, "source": [ "if -1:\n", " print(\"hi\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "MZpFzQBzNkVq" }, "source": [ "x = 10 + 1\n", "if x > 10:\n", " a = 100\n", "print(a)" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "fh2Nu6CpNrQj" }, "source": [ "anotherx = 9\n", "if anotherx > 10:\n", " anothera = 100\n", "print(anothera)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "Jwevfn2UIM5t" }, "source": [ "## Scope" ] }, { "cell_type": "code", "metadata": { "id": "2Himat0MIN0u" }, "source": [ "A = 1\n", "print(dir())\n", "del A\n", "print(dir())" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "95x3-laLIqnW" }, "source": [ "\n", "A = 1\n", "print(dir())\n", "del A\n", "print(dir())\n", "print(A)" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "gKmHKa0iI1r3" }, "source": [ "A = 1\n", "del A\n", "age = input(\"Enter an age:\")\n", "age = int(age)\n", "\n", "print(dir())\n", "\n", "if age > 0:\n", " print(dir())\n", " A = 2\n", " print(dir())\n", "\n", "print(dir())\n", "print(A)\n" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "ewpXp83EJVBp" }, "source": [ "##Testing" ] }, { "cell_type": "code", "metadata": { "id": "ptbiS4mbJW8e" }, "source": [ "x = int(input(\"Enter an integer:\"))\n", "y = int(input(\"Enter an integer:\"))\n", "\n", "if x > 10:\n", " print(\"x > 10\")\n", "else:\n", " print(\"x <= 10\")\n", "if y > 10:\n", " print(\"y > 10\")\n", "else:\n", " print(\"y <= 10\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "R18whKqtJlLp" }, "source": [ "print(\"test 1\")\n", "#Condition test (conditions are executed)\n", "x = 11\n", "y = 11\n", "\n", "if x > 10:\n", " print(\"x > 10\")\n", "else:\n", " print(\"x <= 10\")\n", "if y > 10:\n", " print(\"y > 10\")\n", "else:\n", " print(\"y <= 10\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "Nja6WKS0JqlH" }, "source": [ "print(\"test 1\")\n", "#Statement test (all lines execute)\n", "x = 11\n", "y = 11\n", "\n", "if x > 10:\n", " print(\"x > 10\")\n", "else:\n", " print(\"x <= 10\")\n", "if y > 10:\n", " print(\"y > 10\")\n", "else:\n", " print(\"y <= 10\")\n", "\n", "print(\"test 2\")\n", "#Statement test (all lines execute)\n", "x = 1\n", "y = 1\n", "\n", "if x > 10:\n", " print(\"x > 10\")\n", "else:\n", " print(\"x <= 10\")\n", "if y > 10:\n", " print(\"y > 10\")\n", "else:\n", " print(\"y <= 10\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "6kuqIY3WLKVT" }, "source": [ "print(\"test 1\")\n", "#Path test (all orders of lines execute)\n", "x = 11\n", "y = 11\n", "\n", "if x > 10:\n", " print(\"x > 10\")\n", "else:\n", " print(\"x <= 10\")\n", "if y > 10:\n", " print(\"y > 10\")\n", "else:\n", " print(\"y <= 10\")\n", "\n", "print(\"test 2\")\n", "#Path test (all orders of lines execute)\n", "x = 1\n", "y = 1\n", "\n", "if x > 10:\n", " print(\"x > 10\")\n", "else:\n", " print(\"x <= 10\")\n", "if y > 10:\n", " print(\"y > 10\")\n", "else:\n", " print(\"y <= 10\")\n", "\n", "print(\"test 3\")\n", "#Path test (all orders of lines execute)\n", "x = 1\n", "y = 11\n", "\n", "if x > 10:\n", " print(\"x > 10\")\n", "else:\n", " print(\"x <= 10\")\n", "if y > 10:\n", " print(\"y > 10\")\n", "else:\n", " print(\"y <= 10\")\n", "\n", "print(\"test 4\")\n", "#Path test (all orders of lines execute)\n", "x = 11\n", "y = 1\n", "\n", "if x > 10:\n", " print(\"x > 10\")\n", "else:\n", " print(\"x <= 10\")\n", "if y > 10:\n", " print(\"y > 10\")\n", "else:\n", " print(\"y <= 10\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "XliHoOzWm024" }, "source": [ "x = int(input())\n", "if x < 10:\n", " print(x)\n" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "fyuBEUyiLizh" }, "source": [ "#Boundary testing\n", "x = 9\n", "\n", "if x > 10:\n", " print(\"x > 10\")\n", "else:\n", " print(\"x <= 10\")\n", " \n", "#Boundary testing\n", "x = 10\n", "\n", "if x > 10:\n", " print(\"x > 10\")\n", "else:\n", " print(\"x <= 10\")\n", "\n", "#Boundary testing\n", "x = 11\n", "\n", "if x > 10:\n", " print(\"x > 10\")\n", "else:\n", " print(\"x <= 10\")" ], "execution_count": null, "outputs": [] } ] }