{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Indentation, indentation, indentation" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python cares a lot about indentation.\n", "\n", "You are going see this often in Python code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`for` loops are one of many places that Python depends on indentation. The indentation tells Python which statements are in the loop, and which are outside the loop.\n", "\n", "Remember that the `for` statement:\n", "\n", "* starts with the word `for`, followed by\n", "* a variable name (the *loop variable*) followed by\n", "* the word `in` followed by\n", "* an expression that gives sequence of values followed by\n", "* the character `:` followed by\n", "* an indented block of one or more statements. This is the *body* of\n", " the `for` loop.\n", "\n", "Here was our first `for` loop:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for i in np.arange(3):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Following the sequence above, we have:\n", "\n", "* `for`\n", "* `i` (the loop variable name)\n", "* `in`\n", "* `np.arange(3)` (a sequence with three values \\- 0, 1, 2)\n", "* `:`\n", "* ` print(i)` (the indented block, consisting of one statement." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we want to execute more than one statement in the loop, we need to indent each statement:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "Finished this iteration of the loop\n", "1\n", "Finished this iteration of the loop\n", "2\n", "Finished this iteration of the loop\n" ] } ], "source": [ "for i in np.arange(3):\n", " print(i)\n", " print('Finished this iteration of the loop')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the above, both statements are indented, so Python runs both statements for each run through the loop.\n", "\n", "The first not-indented statement signals that the `for` loop body is over:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "Finished this iteration of the loop\n", "1\n", "Finished this iteration of the loop\n", "2\n", "Finished this iteration of the loop\n", "Now the loop has finished\n" ] } ], "source": [ "for i in np.arange(3):\n", " print(i)\n", " print('Finished this iteration of the loop')\n", "print('Now the loop has finished')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The lines in the `for` block must have the same indentation. Try\n", "knocking one space off the indentation in one of the lines in the loop above, and see what happens.\n", "\n", "The first line must end with a colon character. Try knocking the\n", "colon off the line beginning `for` above, and see what happens.\n", "\n", "There must be a `for` block. Try removing all the indentation from the line `print(i)` above, and see what happens." ] } ], "metadata": { "jupytext": { "text_representation": { "extension": ".Rmd", "format_name": "rmarkdown", "format_version": "1.1", "jupytext_version": "0.8.7" } }, "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.1" } }, "nbformat": 4, "nbformat_minor": 2 }