{ "cells": [ { "cell_type": "markdown", "id": "da313e3c", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "
\n", " \n", "

PYTHON LECTURE 6

\n", "\n", "

WHILE LOOPS

\n", "\n", " \n", "
" ] }, { "cell_type": "markdown", "id": "ddeabe12", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## 6.1.1. Loops and `while`\n", "\n", "* Loops allow **conditional repetition** of a set of statements. \n", "\n", "\n", "* In simple terms, loops **repeatedly execute** a **block of indented statements** as long as a corresponding **boolean condition** evaluates to `True`.\n", "\n", "\n", "* **`while` loops** are the most basic looping construct in the most programming languages, including Python. " ] }, { "cell_type": "markdown", "id": "0f62af21", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Recall, for `if...else` statements:" ] }, { "cell_type": "code", "execution_count": null, "id": "de135eaa", "metadata": {}, "outputs": [], "source": [ "if boolean_condition_A: \n", " #Indented Block B\n", "else:\n", " #Indented Block C" ] }, { "cell_type": "markdown", "id": "47013af8", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "" ] }, { "cell_type": "markdown", "id": "23d1cf66", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "\n", " \n", " \n", " \n", " \n", " \n", "
\n" ] }, { "cell_type": "markdown", "id": "10c9656e", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* It is called a **loop** because **flow of execution keeps looping back to the start** of the while statement **until the condition becomes `False`**. \n", "\n", "\n", "* When the condition becomes `False`, flow of execution moves to the statement that follows the indented `while` block. \n", "\n", "\n", "* The net effect is that the loop’s body is executed repeatedly while the condition at the top is `True`.\n", "\n", "\n", "* Each execution of the loop's body is called an **iteration** and the variable(s) in the condition are called **iterator(s)**.\n" ] }, { "cell_type": "markdown", "id": "2648a4af", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* The `while` statement consists of: \n", " * a header line with a **boolean condition**\n", " * a body of one or more indented statements\n", "
\n", "\n", "statement 0 \n", "\n", " `while` (boolean condition): \n", "> statement 1 \\\n", "> statement 2 \\\n", " .\\\n", " .\\\n", " .\\\n", " statement n\n", " \n", "statement n+1" ] }, { "cell_type": "code", "execution_count": 5, "id": "68e42270", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "Outside loop\n" ] } ], "source": [ "i = 0\n", "\n", "while i < 10:\n", " \n", " print(i)\n", " \n", " i = i + 1\n", " \n", "print(\"Outside loop\")" ] }, { "cell_type": "markdown", "id": "c1b34ee0", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Loop Design Pattern\n", "* Most loops (not all of them) have the following common parts, in this order:\n", "\n", "1. **Initialization of iterator, result and other variables**.\n", "\n", "2. **Continuation condition** (a.k.a. Termination condition)\n", "\n", "3. **Body** of the loop (Indented)\n", "\n", "4. Update of result and iterator variables (Indented)\n", "\n", " * Last lines of the body\n", " \n", "`initialization` \\\n", "`while ( continuation-condition ) {` \n", "> `body ` \\\n", "> `update`" ] }, { "cell_type": "markdown", "id": "3fd2b2ae", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "$summ = \\sum_{i=1}^{n} k$" ] }, { "cell_type": "code", "execution_count": 13, "id": "c70c16b0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100\n" ] } ], "source": [ "def summation_k(k, n):\n", "\n", " #initialization result\n", " summ = 0 \n", "\n", " #initialization iterator\n", " i = 0 \n", " \n", " #condition\n", " while i < n: \n", " \n", " #update of result\n", " summ = summ + k\n", " \n", " #update of iterator\n", " i = i + 1\n", " \n", " return summ\n", "\n", "print(summation_k(10, 10))" ] }, { "cell_type": "markdown", "id": "036f0d44", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "$\\sum_{i=1}^{n} i$\n", "\n", "" ] }, { "cell_type": "code", "execution_count": 12, "id": "8cf60adc", "metadata": {}, "outputs": [], "source": [ "def summation_i(n):\n", "\n", " summ = 0\n", " i = 0\n", " while i <= n:\n", " summ = summ + k\n", " i = i + 1\n", "\n", " return summ\n" ] }, { "cell_type": "markdown", "id": "9cebe3cb", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 6.1.2. Infinite Loops" ] }, { "cell_type": "code", "execution_count": null, "id": "63320dd4", "metadata": {}, "outputs": [], "source": [ "i = 0\n", "\n", "while i >= 0:\n", " \n", " print(i)\n", " i = i + 1\n", "\n", "print(\"Outside loop\")" ] }, { "cell_type": "markdown", "id": "b6ec1856", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 6.1.3. `break`" ] }, { "cell_type": "markdown", "id": "97d73804", "metadata": {}, "source": [ "* For **immediately exiting** the loop i.e. Python jumps out of the loop's body (past the entire loop statement) when it encounters a `break statement`\n", "\n", "\n", "* Generally **used within an `if` statement** within the loop's body\n", "\n", "\n", "* Inside the loop's body, the **code that follows the `break` statement is never executed**" ] }, { "cell_type": "markdown", "id": "5e447f05", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "" ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.12" } }, "nbformat": 4, "nbformat_minor": 5 }