{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "CPSC231-5Decisions.ipynb", "provenance": [], "collapsed_sections": [], "toc_visible": true }, "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", "colab": { "base_uri": "https://localhost:8080/", "height": 68 }, "outputId": "3297579c-9443-45a5-94f6-b201b9343200" }, "source": [ "x = True\n", "y = False\n", "print(type(x))\n", "print(type(y))" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "\n", "\n", "\n" ], "name": "stdout" } ] }, { "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", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "40a09d26-e0c6-4c10-c79f-f76a8f07a8f9" }, "source": [ "3 < 5" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 6 } ] }, { "cell_type": "code", "metadata": { "id": "X678DlCCxhPM", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "c10c2f89-b886-4bc6-9437-8c451809a83c" }, "source": [ "5 > 3" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 7 } ] }, { "cell_type": "code", "metadata": { "id": "sbEM0MEVxiLN", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "7aef0aba-5f18-4d58-e715-b64eead3170e" }, "source": [ "3 == 3" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 8 } ] }, { "cell_type": "code", "metadata": { "id": "bQy9F376xjC9", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "a88a6f5c-d202-45cc-8d05-6ddd4ce0095f" }, "source": [ "5 <= 5" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 9 } ] }, { "cell_type": "code", "metadata": { "id": "1de75eRqxkC1", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "d4730804-5362-4688-f517-79057f0f65ae" }, "source": [ "3 >= 4" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 10 } ] }, { "cell_type": "code", "metadata": { "id": "tH3-S0DZxoL1", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "51891469-19a4-4bf7-9b9d-c51bb571bbce" }, "source": [ "#Not the same =/=\n", "#More shared with other languages\n", "5 != 3" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 11 } ] }, { "cell_type": "code", "metadata": { "id": "71G4Zv1jxyPc", "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, "outputId": "5b7e7fff-d82e-46cd-f12e-15e40bbc03fb" }, "source": [ "x = 1.0\n", "y = 2.0\n", "c = (x <= y)\n", "print (type(c))\n", "print(c)" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "\n", "True\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "t3jiYsIw3iN0", "colab": { "base_uri": "https://localhost:8080/", "height": 103 }, "outputId": "e7761519-6cb2-4cb7-f1ea-90438bb56c52" }, "source": [ "print(int(True))\n", "print(int(False))" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "1\n", "0\n", "True\n", "True\n", "False\n" ], "name": "stdout" } ] }, { "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", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "f850395d-8c65-4015-da6e-6f8c3c9b4c41" }, "source": [ "a and b" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 22 } ] }, { "cell_type": "code", "metadata": { "id": "zUAGbXlQyONV", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "d9a0111f-7fa8-4069-b447-16517744c757" }, "source": [ "a and c" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 21 } ] }, { "cell_type": "code", "metadata": { "id": "IgR-PbEcyPeW", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "48844a3c-e8b1-4d98-827f-113d00efa9d0" }, "source": [ "a or c" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 22 } ] }, { "cell_type": "code", "metadata": { "id": "Hrk6ie91ySZV", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "29903cd5-0bb9-45b6-ede8-82f7ce1ef7ab" }, "source": [ "not a" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 23 } ] }, { "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", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "14c34eff-8867-4cb3-b7d1-80cd1a2c02fd" }, "source": [ "not True and False or True" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 24 } ] }, { "cell_type": "code", "metadata": { "id": "srHL19hoyfOG", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "f2a3b169-ee45-4d40-e462-b0067b84495c" }, "source": [ "((not True) and False) or True" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 27 } ] }, { "cell_type": "code", "metadata": { "id": "u3jw46SryfbF", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "54bc2101-a380-481b-e439-cf3ed81fdbd5" }, "source": [ "(not True) and (False or True)" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 28 } ] }, { "cell_type": "markdown", "metadata": { "id": "wwK5CwZMzAVv" }, "source": [ "Things are always better with brackets" ] }, { "cell_type": "code", "metadata": { "id": "wMMtzA7Eyzou", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "574a0448-d0fd-4642-8628-0064ea2d850d" }, "source": [ "not 6 == 2 * 3 and False or True" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 27 } ] }, { "cell_type": "code", "metadata": { "id": "vZCs-AVGzC1a", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "e40a8956-193f-4d94-caba-f6b236a3b80a" }, "source": [ "((not (6 == (2 * 3))) and False) or True" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 28 } ] }, { "cell_type": "markdown", "metadata": { "id": "IQCTV4yqzICv" }, "source": [ "##Truth tables " ] }, { "cell_type": "code", "metadata": { "id": "UaJaRQRm7X9l", "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, "outputId": "36caede9-4159-44bb-cbca-0198b6f88faa" }, "source": [ "a = 1\n", "b = 0\n", "print(a and b)" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "0\n", "1\n" ], "name": "stdout" } ] }, { "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_", "colab": { "base_uri": "https://localhost:8080/", "height": 102 }, "outputId": "ffafd205-6b31-4d58-8d5c-3540b8e1f637" }, "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": [ { "output_type": "stream", "text": [ "A\tB\tA or B\n", "True\tTrue\tTrue\n", "True\tFalse\tTrue\n", "False\tTrue\tTrue\n", "False\tFalse\tFalse\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "9-Dm4J07z1Ho", "colab": { "base_uri": "https://localhost:8080/", "height": 102 }, "outputId": "e08e9428-cb79-46f7-9169-5e72aea67951" }, "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": [ { "output_type": "stream", "text": [ "A\tB\tA and B\n", "True\tTrue\tTrue\n", "True\tFalse\tFalse\n", "False\tTrue\tFalse\n", "False\tFalse\tFalse\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "_hkBN2Ifz4_W", "colab": { "base_uri": "https://localhost:8080/", "height": 68 }, "outputId": "0c36516c-4d90-40a7-89ac-960ccfd04982" }, "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": [ { "output_type": "stream", "text": [ "A\tnot A\n", "True\tFalse\n", "False\tTrue\n" ], "name": "stdout" } ] }, { "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": "PXuGvfA6GhDs" }, "source": [ "" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "5xGmuNkO0xG-", "colab": { "base_uri": "https://localhost:8080/", "height": 72 }, "outputId": "6ae1a60e-d12d-4de2-a982-58a5f5b57dea" }, "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": [ { "output_type": "stream", "text": [ "this happens before\n", "this is printed always as condition always True\n", "this happens after\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "R2lZcmba04CX", "colab": { "base_uri": "https://localhost:8080/", "height": 54 }, "outputId": "0e4854f5-be3f-466b-b94e-7a18647db7f7" }, "source": [ "print(\"this happens before\")\n", "if False:\n", " print(\"this is never printed\")\n", "print(\"this happens after\")" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "this happens before\n", "this happens after\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "C2RKQYT50dQN", "colab": { "base_uri": "https://localhost:8080/", "height": 72 }, "outputId": "eb19bb2d-e40a-4d62-f775-e887059a06ee" }, "source": [ "age = int(input(\"Enter age:\"))\n", "if age < 18:\n", " print(\"You are underage!\")" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "Enter age:17\n", "You are underage!\n", "asdfds\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "nLXbwKwm09Mq" }, "source": [ "##IfElse" ] }, { "cell_type": "code", "metadata": { "id": "fBtifUeD1TIO", "colab": { "base_uri": "https://localhost:8080/", "height": 54 }, "outputId": "53ff73d1-ff82-48e2-f988-d6c3ffc5ba08" }, "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": [ { "output_type": "stream", "text": [ "Enter age:19\n", "You are overage.\n" ], "name": "stdout" } ] }, { "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", "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, "outputId": "f3db69f7-8807-453e-d560-4cded82c77b2" }, "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": [ { "output_type": "stream", "text": [ "Enter age:17\n", "You are underage!\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "MetC5eaS1m5p", "colab": { "base_uri": "https://localhost:8080/", "height": 72 }, "outputId": "78bf6688-57c9-4eb4-c1d9-a3b8d74287e4" }, "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": [ { "output_type": "stream", "text": [ "this happens before\n", "this is printed always as condition always True\n", "this happens after\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "y08mRQ4D1rhG", "colab": { "base_uri": "https://localhost:8080/", "height": 72 }, "outputId": "9eb697c8-a20f-4826-84e2-30bfb64888ff" }, "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": [ { "output_type": "stream", "text": [ "this happens before\n", "this is printed if condition is false which is always true\n", "this happens after\n" ], "name": "stdout" } ] }, { "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", "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, "outputId": "410d5340-9c56-4838-8761-bb25407ce7a3" }, "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": [ { "output_type": "stream", "text": [ "Enter age:18\n", "You aren't underage.\n" ], "name": "stdout" } ] }, { "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": "code", "metadata": { "id": "HWbbFYuNKitc" }, "source": [ "" ], "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_", "colab": { "base_uri": "https://localhost:8080/", "height": 54 }, "outputId": "5dffbaad-2fb3-41fb-d25d-528fd96c2590" }, "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": [ { "output_type": "stream", "text": [ "Enter age:18\n", "You aren't underage.\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "xWE-vTBgHFSp" }, "source": [ "## Some simple mistakes" ] }, { "cell_type": "code", "metadata": { "id": "uYHCxUDKHG8v", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "b1510181-2761-4304-c1d0-175e64e6c1eb" }, "source": [ "#missing :\n", "if True\n", " print(\"x\")" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "x\n" ], "name": "stdout" } ] }, { "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", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "36a18505-7439-4d3b-f5ad-16506f3bb66e" }, "source": [ "#Greater than 0 is true\n", "if 1:\n", " print(\"hi\")" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "hi\n" ], "name": "stdout" } ] }, { "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", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "c4f27e69-65b7-495b-c03e-7f1625696215" }, "source": [ "#Greater than 0 is true\n", "if 7:\n", " print(\"hi\")" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "hi\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "sLneF0vBH7Nc", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "b6da46d9-d180-4659-c39d-bce0e78afb84" }, "source": [ "#Greater than 0 is true\n", "if 0.1:\n", " print(\"hi\")" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "hi\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "Lw34u2ZiNLQ9", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "575adf2d-5f2d-429c-d914-d7fa7ecab582" }, "source": [ "if -1:\n", " print(\"hi\")" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "hi\n" ], "name": "stdout" } ] }, { "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", "colab": { "base_uri": "https://localhost:8080/", "height": 71 }, "outputId": "4fd885bc-e903-40b5-e469-91f245f4dfa7" }, "source": [ "A = 1\n", "print(dir())\n", "del A\n", "print(dir())" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "['A', 'B', 'In', 'Out', '_', '_1', '_11', '_12', '_13', '_14', '_15', '_16', '_17', '_18', '_19', '_2', '_20', '_21', '_22', '_23', '_24', '_25', '_27', '_28', '_3', '_4', '_5', '_6', '_80', '_81', '_82', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i10', '_i11', '_i12', '_i13', '_i14', '_i15', '_i16', '_i17', '_i18', '_i19', '_i2', '_i20', '_i21', '_i22', '_i23', '_i24', '_i25', '_i26', '_i27', '_i28', '_i29', '_i3', '_i30', '_i31', '_i32', '_i33', '_i34', '_i35', '_i36', '_i37', '_i38', '_i39', '_i4', '_i40', '_i41', '_i42', '_i43', '_i44', '_i45', '_i46', '_i47', '_i48', '_i49', '_i5', '_i50', '_i51', '_i52', '_i53', '_i54', '_i55', '_i56', '_i57', '_i58', '_i59', '_i6', '_i60', '_i61', '_i62', '_i63', '_i64', '_i65', '_i66', '_i67', '_i68', '_i69', '_i7', '_i70', '_i71', '_i72', '_i73', '_i74', '_i75', '_i76', '_i77', '_i78', '_i79', '_i8', '_i80', '_i81', '_i82', '_i83', '_i9', '_ih', '_ii', '_iii', '_oh', '_sh', 'a', 'age', 'b', 'c', 'cond', 'cond1', 'cond2', 'exit', 'get_ipython', 'quit', 'x', 'y']\n", "['B', 'In', 'Out', '_', '_1', '_11', '_12', '_13', '_14', '_15', '_16', '_17', '_18', '_19', '_2', '_20', '_21', '_22', '_23', '_24', '_25', '_27', '_28', '_3', '_4', '_5', '_6', '_80', '_81', '_82', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i10', '_i11', '_i12', '_i13', '_i14', '_i15', '_i16', '_i17', '_i18', '_i19', '_i2', '_i20', '_i21', '_i22', '_i23', '_i24', '_i25', '_i26', '_i27', '_i28', '_i29', '_i3', '_i30', '_i31', '_i32', '_i33', '_i34', '_i35', '_i36', '_i37', '_i38', '_i39', '_i4', '_i40', '_i41', '_i42', '_i43', '_i44', '_i45', '_i46', '_i47', '_i48', '_i49', '_i5', '_i50', '_i51', '_i52', '_i53', '_i54', '_i55', '_i56', '_i57', '_i58', '_i59', '_i6', '_i60', '_i61', '_i62', '_i63', '_i64', '_i65', '_i66', '_i67', '_i68', '_i69', '_i7', '_i70', '_i71', '_i72', '_i73', '_i74', '_i75', '_i76', '_i77', '_i78', '_i79', '_i8', '_i80', '_i81', '_i82', '_i83', '_i9', '_ih', '_ii', '_iii', '_oh', '_sh', 'a', 'age', 'b', 'c', 'cond', 'cond1', 'cond2', 'exit', 'get_ipython', 'quit', 'x', 'y']\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "95x3-laLIqnW", "colab": { "base_uri": "https://localhost:8080/", "height": 257 }, "outputId": "2249e300-e490-40b7-9fbf-190ee0b6abf9" }, "source": [ "\n", "A = 1\n", "print(dir())\n", "del A\n", "print(dir())\n", "print(A)" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "['A', 'In', 'Out', '_', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_ih', '_ii', '_iii', '_oh', '_sh', 'exit', 'get_ipython', 'quit']\n", "['In', 'Out', '_', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_ih', '_ii', '_iii', '_oh', '_sh', 'exit', 'get_ipython', 'quit']\n" ], "name": "stdout" }, { "output_type": "error", "ename": "NameError", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0mA\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdir\u001b[0m\u001b[0;34m(\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[0;32m----> 6\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mA\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'A' is not defined" ] } ] }, { "cell_type": "code", "metadata": { "id": "gKmHKa0iI1r3", "colab": { "base_uri": "https://localhost:8080/", "height": 142 }, "outputId": "11e194df-5e45-415d-f3d4-10d68dd7075d" }, "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": [ { "output_type": "stream", "text": [ "Enter an age:10\n", "['In', 'Out', '_', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i2', '_i3', '_ih', '_ii', '_iii', '_oh', '_sh', 'age', 'exit', 'get_ipython', 'quit']\n", "['In', 'Out', '_', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i2', '_i3', '_ih', '_ii', '_iii', '_oh', '_sh', 'age', 'exit', 'get_ipython', 'quit']\n", "['A', 'In', 'Out', '_', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i2', '_i3', '_ih', '_ii', '_iii', '_oh', '_sh', 'age', 'exit', 'get_ipython', 'quit']\n", "['A', 'In', 'Out', '_', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i2', '_i3', '_ih', '_ii', '_iii', '_oh', '_sh', 'age', 'exit', 'get_ipython', 'quit']\n", "2\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "ewpXp83EJVBp" }, "source": [ "##Testing" ] }, { "cell_type": "code", "metadata": { "id": "ptbiS4mbJW8e", "colab": { "base_uri": "https://localhost:8080/", "height": 87 }, "outputId": "67cb222b-4b0d-4de0-ea59-a4a2ad3e3bd6" }, "source": [ "x = int(input(\"Enter an integer:\"))\n", "y = int(input(\"Enter an integer:\"))\n", "\n", "if x >= 0:\n", " print(x)\n", "else:\n", " print(\"x is neg\")\n", "if y >= 0:\n", " print(y)\n", "else:\n", " print(\"y is neg\")" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "Enter an integer:10\n", "Enter an integer:10000\n", "x > 10\n", "y > 10\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "fyuBEUyiLizh", "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "outputId": "9a978617-d232-418d-9bc4-615b29a69011" }, "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": [ { "output_type": "stream", "text": [ "x <= 10\n", "x > 10\n", "x > 10\n" ], "name": "stdout" } ] } ] }