{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 4 Decisions: if.. elif.. else" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Decision making plays a key role in programming and Python provides several ways to do that." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4.1 The If statement" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `if` statement is used to check if a condition is fulfilled and then a task (or set of tasks) is executed. The following example shows how it is used to check if a number is even:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "number = 2\n", "if number % 2 == 0:\n", " print(\"number is even.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The operator `%` is called modulus and gives the remainder of an integer division (see section 1). The *conditional expression* follows directly after the word `if` and ends with a colon `:` (just as in the syntax for loops, see section 3). The task(s) to be executed in case the condition is `True`, need to be indented (again as in the syntax for loops). Note that if the condition is not `True`, the task is simply not executed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "number = 3\n", "if number % 2 == 0:\n", " print(\"number is even.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4.2 The if.. else statement" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An `else` statement can be combined with an `if` statement. An else statement contains the block of code that is executed if the conditional expression in the `if` statement resolves as `False`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "number = 3\n", "if number % 2 == 0:\n", " print(\"number is even.\")\n", "else:\n", " print(\"number is odd.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Just as loops can be nested within loops, `if` statements can be nested within `if` statements." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that you know the concept of loops and decision making, by combining them you can do a range of different tasks!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 4.2.1\n", "\n", "While `n < 10`, print whether each number is odd or even." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise 4.2.1\n", "for n in range(1,11):\n", " if n % 2 == 0:\n", " print('%d is even' % n)\n", " else:\n", " print('%d is odd' % n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 4.2.2\n", "\n", "While `n < 10`, find the number of values that are even." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise 4.2.2\n", "counter = 0\n", "for n in range(1,11):\n", " if n % 2 == 0:\n", " counter += 1\n", "print(f\"Number of even values: {counter}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If there is more than one condition you want to check, you can add `elif` statements to check each condition. You can have as many `elif`s between an `if` and `else` as you want." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for n in range(-2, 2):\n", " if n < 0:\n", " print(f'{n} is negative')\n", " elif n > 0:\n", " print(f'{n} is positive')\n", " else:\n", " print(f'{n} is zero')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is worth noting that in an `if`.. `elif`.. `else` block, the first statement that returns as `True` is executed." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Review:\n", "\n", "In this section, we have covered:\n", "- How to do decision making\n", "- The `if` statement.\n", "- The `if .. else` statment.\n", "- The `if .. elif .. else` statement." ] } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 1 }