{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Conditional Statements\n", "\n", "A conditional statement is a programming concept that describes whether a region of code runs based on if a condition is true or false. The keywords involved in conditional statements are `if`, and optionally `elif` and `else`.\n", "\n", "## The `if` Statement\n", "\n", "A conditional statement starts with the `if` keyword, followed by the condition to check, and ends with a `:`. After the `:` is an indented block of code that will only run if the condition evaluates to a value of `True`. For example," ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Temperature is above freezing.\n" ] } ], "source": [ "temperature = 35\n", "if temperature > 32:\n", " print('Temperature is above freezing.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The code above is checking whether the value of the variable `temperature` is greater than 32, the freezing point of water in degrees Fahrenheit at mean sea level pressure. If the condition is true, the program prints \n", "\n", " Temperature is above freezing. \n", "\n", "Note the `>` is the greater-than \"operator\" in the usual arithmetic sense. If the condition evaluates to `False`, then the code in the indented block is not executed. We can check the results by looking at the value of the condition, as we do here:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temperature > 32" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We see that the value of the condition is `True`, which explains why the print function was called. We can change the value of `temperature` to see what happens if the condition is `False`:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "temperature = 30\n", "if temperature > 32:\n", " print('Temperature is above freezing.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, nothing is printed since the value of `temperature` is not greater than 32. What do we do if we want to run one part of the code if a condition is true, and another if the condition is false? We can make use of the `else` keyword.\n", "\n", "## The Optional `else` Block\n", "\n", "To run code if a condition is false, we employ the `else` keyword. The `else` part of the code is created by following the indented code of an `if` statement by an unindented `else:`, followed by an indented block of code. This `else` block of code will execute if the condition in the `if` statement evaluates to `False`. For example, going back to the code we examined earlier, we can augement it to print the message \n", "\n", "`Temperature is at or below freezing.` \n", "\n", "in the `else` block for the case where the condition is `False` (i.e., `temperature` is not greater than 32):" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Temperature is at or below freezing.\n" ] } ], "source": [ "temperature = 30\n", "if temperature > 32:\n", " print('Temperature is above freezing.')\n", "else:\n", " print('Temperature is at or below freezing.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Optional `elif` Block(s)\n", "\n", "The `if` and `else` keywords are adequate when you have a binary choice such as examining a temperature value that is above or below freezing, but consider a scenario where you want more options. In this case, you will employ the `elif` keyword that will follow the same `:`, indented code block syntax. There can be zero or more `elif` keywords in an `if` statement. Let's write code that will categorize maximum sustained winds in units of knots according to the [Saffir–Simpson hurricane intensity scale](https://en.wikipedia.org/wiki/Saffir%25E2%2580%2593Simpson_scale)." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Category one hurricane\n" ] } ], "source": [ "wind_speed = 65\n", "\n", "if wind_speed >= 64 and wind_speed <= 82:\n", " print('Category one hurricane')\n", "elif wind_speed >= 83 and wind_speed <= 95:\n", " print('Category two hurricane')\n", "elif wind_speed >= 96 and wind_speed <= 112:\n", " print('Category three hurricane')\n", "elif wind_speed >= 113 and wind_speed <= 136:\n", " print('Category four hurricane')\n", "elif wind_speed > 136:\n", " print('Category five hurricane')\n", "else:\n", " print('Below hurricane level wind speed')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note, the `>=` arithmetic \"operator\" signifies greater than or equal, and the `<=` means less than or equal. We are also using the `and` keyword to describe a range of values. E.g., `wind_speed >= 64 and wind_speed <= 82` describes the range of values between `64` and `82` knots, end points inclusive. Also, observe that if the `wind_speed` value does not meet any of the `if` or `elif` choices, the code ends up executing the `else` block. For example, a `wind_speed` of `15` will print:\n", "\n", " Below hurricane level wind speed." ] } ], "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.5.1" }, "widgets": { "state": {}, "version": "1.1.1" } }, "nbformat": 4, "nbformat_minor": 0 }