{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lesson A7 – Iterations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is a common purpose of programs to perform a defined task many, many times. We will learn in this part, how to write code that repeats itself a number of times without being literately written multiple times." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's demonstrate this with an example that we will explain in more detail later using a dictionary of solvents and boiling points as in the container lesson." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "solvents_dict = {\n", " \"water\": 100,\n", " \"ethanol\": 78,\n", " \"N,N-dimethylformamide\": 153,\n", " \"dichloromethane\": 40,\n", " }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We could print each entry in the dictionary with a separate line: " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The boling point of water is 100 °C\n", "The boling point of ethanol is 78 °C\n", "The boling point of N,N-dimethylformamide is 153 °C\n", "The boling point of dichloromethane is 40 °C\n" ] } ], "source": [ "# Imaging doing this for hundreds of entries ...\n", "# Or imagine you do not know all the dictionary keys before hand ...\n", "# Or imagine the keys are changing over time ...\n", "print(f\"The boling point of water is {solvents_dict['water']} °C\")\n", "print(f\"The boling point of ethanol is {solvents_dict['ethanol']} °C\")\n", "print(f\"The boling point of N,N-dimethylformamide is {solvents_dict['N,N-dimethylformamide']} °C\")\n", "print(f\"The boling point of dichloromethane is {solvents_dict['dichloromethane']} °C\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But it would be far less tedious and error-prone, if we used a self-repeating code block for this:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The boiling point of water is 100 °C\n", "The boiling point of ethanol is 78 °C\n", "The boiling point of N,N-dimethylformamide is 153 °C\n", "The boiling point of dichloromethane is 40 °C\n" ] } ], "source": [ "# With a loop you can write: \n", "for k, v in solvents_dict.items(): \n", " print(f\"The boiling point of {k} is {v} °C\")\n", "\n", "# You need to write much less code\n", "# It works for arbitrarily many entries\n", "# You do not need to know the entries beforehand" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## For-loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One way to implement a repeating code block, is to use *for*-loops. They are\n", "meant to be used whenever you want to do something for a pre-defined number of times.\n", "In Python, for loops are connected to *iterables*. An iterable is an object you can use to\n", "iterate over. Collections, like lists, can be for example iterables. We use a container for an iteration with the pattern: `for` *element* `in` *iterable*." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for i in [1, 2, 3]: # <- colon after the for statement\n", " # loop over elements\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "