{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [ "remove_input" ] }, "outputs": [], "source": [ "path_data = '../../data/'\n", "\n", "import numpy as np\n", "import pandas as pd\n", "\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "plt.style.use('fivethirtyeight')\n", "\n", "import warnings\n", "warnings.filterwarnings('ignore')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Conditional Statements\n", "In many situations, actions and results depends on a specific set of conditions being satisfied. For example, individuals in randomized controlled trials receive the treatment if they have been assigned to the treatment group. A gambler makes money if she wins her bet. \n", "\n", "In this section we will learn how to describe such situations using code. A *conditional statement* is a multi-line statement that allows Python to choose among different alternatives based on the truth value of an expression. While conditional statements can appear anywhere, they appear most often within the body of a function in order to express alternative behavior depending on argument values.\n", "\n", "A conditional statement always begins with an `if` header, which is a single line followed by an indented body. The body is only executed if the expression directly following `if` (called the *if expression*) evaluates to a true value. If the *if expression* evaluates to a false value, then the body of the `if` is skipped.\n", "\n", "Let us start defining a function that returns the sign of a number." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def sign(x):\n", " \n", " if x > 0:\n", " return 'Positive'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Positive'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sign(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This function returns the correct sign if the input is a positive number. But if the input is not a positive number, then the *if expression* evaluates to a false value, and so the `return` statement is skipped and the function call has no value." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "sign(-3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So let us refine our function to return `Negative` if the input is a negative number. We can do this by adding an `elif` clause, where `elif` if Python's shorthand for the phrase \"else, if\"." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def sign(x):\n", " \n", " if x > 0:\n", " return 'Positive'\n", " \n", " elif x < 0:\n", " return 'Negative'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now `sign` returns the correct answer when the input is -3:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Negative'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sign(-3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What if the input is 0? To deal with this case, we can add another `elif` clause:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def sign(x):\n", " \n", " if x > 0:\n", " return 'Positive'\n", " \n", " elif x < 0:\n", " return 'Negative'\n", " \n", " elif x == 0:\n", " return 'Neither positive nor negative'" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Neither positive nor negative'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sign(0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Equivalently, we can replaced the final `elif` clause by an `else` clause, whose body will be executed only if all the previous comparisons are false; that is, if the input value is equal to 0." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def sign(x):\n", " \n", " if x > 0:\n", " return 'Positive'\n", " \n", " elif x < 0:\n", " return 'Negative'\n", " \n", " else:\n", " return 'Neither positive nor negative'" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Neither positive nor negative'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sign(0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The General Form ###\n", "A conditional statement can also have multiple clauses with multiple bodies, and only one of those bodies can ever be executed. The general format of a multi-clause conditional statement appears below.\n", "\n", " if :\n", " \n", " elif :\n", " \n", " elif :\n", " \n", " ...\n", " else:\n", " \n", " \n", "There is always exactly one `if` clause, but there can be any number of `elif` clauses. Python will evaluate the `if` and `elif` expressions in the headers in order until one is found that is a true value, then execute the corresponding body. The `else` clause is optional. When an `else` header is provided, its *else body* is executed only if none of the header expressions of the previous clauses are true. The `else` clause must always come at the end (or not at all)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example: Betting on a Die\n", "Suppose I bet on a roll of a fair die. The rules of the game:\n", "\n", "- If the die shows 1 spot or 2 spots, I lose a dollar.\n", "- If the die shows 3 spots or 4 spots, I neither lose money nor gain money.\n", "- If the die shows 5 spots or 6 spots, I gain a dollar.\n", "\n", "We will now use conditional statements to define a function `one_bet` that takes the number of spots on the roll and returns my net gain." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def one_bet(x):\n", " \"\"\"Returns my net gain if the die shows x spots\"\"\"\n", " if x <= 2:\n", " return -1\n", " elif x <= 4:\n", " return 0\n", " elif x <= 6:\n", " return 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's check that the function does the right thing for each different number of spots." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(-1, -1, 0, 0, 1, 1)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "one_bet(1), one_bet(2), one_bet(3), one_bet (4), one_bet(5), one_bet(6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As a review of how conditional statements work, let's see what `one_bet` does when the input is 3.\n", "\n", "- First it evaluates the `if` expression, which is `3 <= 2` which is `False`. So `one_bet` doesn't execute the `if` body.\n", "- Then it evaluates the first `elif` expression, which is `3 <= 4`, which is `True`. So `one_bet` executes the first `elif` body and returns 0.\n", "- Once the body has been executed, the process is complete. The next `elif` expression is not evaluated.\n", "\n", "If for some reason we use an input greater than 6, then the `if` expression evaluates to `False` as do both of the `elif` expressions. So `one_bet` does not execute the `if` body nor the two `elif` bodies, and there is no value when you make the call below." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "one_bet(17)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To play the game based on one roll of a die, you can use `np.random.choice` to generate the number of spots and then use that as the argument to `one_bet`. Run the cell a few times to see how the output changes." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "one_bet(np.random.choice(np.arange(1, 7)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "At this point it is natural to want to collect the results of all the bets so that we can analyze them. In the next section we develop a way to do this without running the cell over and over again." ] } ], "metadata": { "anaconda-cloud": {}, "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.5" } }, "nbformat": 4, "nbformat_minor": 1 }