{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Conditional statements\n", "\n", "## Sources\n", "This lesson is based on the [Software Carpentry group's](http://software-carpentry.org) lessons on [Programming with Python](http://swcarpentry.github.io/python-novice-inflammation/).\n", "\n", "## Basics of conditional statements\n", "Conditional statements can change the code behavior based on meeting certain conditions.\n", "\n", "### A starting example\n", "\n", "Let's take a simple conditional statement example." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "num = 37\n", "if num > 100:\n", " print('greater')\n", "else:\n", " print('not greater')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What did we do here?\n", "First, we used the `if` and `else` statements to determine what parts of the code to execute.\n", "Note that both lines containing `if` or `else` end with a `:` and the text beneath is indented.\n", "What do these tests do?\n", "The `if` test checks to see whether the variable value for `num` is greater than 100.\n", "If so, 'greater' would be written to the screen.\n", "Since 37 is smaller than 100, the code beneath the `else` is executed.\n", "The `else` statement code will run whenever the `if` test is false.\n", "\n", "### If, but not else\n", "\n", "The combination of `if` and `else` is very common, but both are not strictly required." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "num = 53\n", "if num > 100:\n", " print('53 is greater than 100')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that here we use only the `if` statement, and because 53 is not greater than 100, nothing is printed to the screen.\n", "\n", "### The else-if case\n", "\n", "We can also have a second test for an `if` statment by using the `elif` (else-if) statement." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "num = -3\n", "if num > 0:\n", " print(num, 'is positive')\n", "elif num == 0:\n", " print(num, 'is zero')\n", "else:\n", " print(num, 'is negative')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Makes sense, right?\n", "Note here that we use the `==` to test if a value is equal to another.\n", "The complete list of these comparison operators is given in the table below.\n", "\n", "| Operator | Meaning |\n", "| ---------- | -------------------------- |\n", "| `<` | Less than |\n", "| `<=` | Less than or equal to |\n", "| `==` | Equal to |\n", "| `>=` | Greater than or equal to |\n", "| `>` | Greater than |\n", "| `!=` | Not equal to |\n", "\n", "### Multiple conditions\n", "\n", "We can also use `and` and `or` to have multiple conditions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if (1 > 0) and (-1 > 0):\n", " print('Both parts are true')\n", "else:\n", " print('One part is not true')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if (1 < 0) or (-1 < 0):\n", " print('At least one test is true')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This can be quite handy." ] } ], "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.8.2" } }, "nbformat": 4, "nbformat_minor": 2 }