{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Iteration/Loop\n", "life is full of loops; everyday routines such as going to college, cooking food, slicing vegetables, etc.\n", "\n", "- loops make computer repeatedly execute some block of code\n", "- two types of loops: **for** and **while**\n", " - some keywords that may appear in loops:\n", " - **for, while, break, continue, in, for ... else, while... else**\n", " \n", "### Slice a carrot\n", "1. get cutting board\n", "- get knife\n", "- place carrot on cutting board\n", "- lift knife\n", "- advance carrot\n", "- slice carrot\n", "- if you see only your finger, then quit \n", "- else go to step 4\n", " - steps 4-8 form a loop!\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## for loop\n", "- works with range of values\n", "- syntax \n", "```python\n", "for val in rangeofValues:\n", " # loop body\n", "```\n", "- useful when you know exactly how many times the loop should be executed" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 hello world\n", "2 hello world\n", "3 hello world\n", "4 hello world\n", "5 hello world\n", "6 hello world\n", "7 hello world\n", "8 hello world\n", "9 hello world\n", "10 hello world\n" ] } ], "source": [ "for i in range(1, 11):\n", " print(i, 'hello world')\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n", "12\n", "13\n", "14\n", "15\n", "16\n", "17\n", "18\n", "19\n", "20\n", "21\n", "22\n", "23\n", "24\n", "25\n", "26\n", "27\n", "28\n", "29\n", "30\n", "31\n", "32\n", "33\n", "34\n", "35\n", "36\n", "37\n", "38\n", "39\n", "40\n", "41\n", "42\n", "43\n", "44\n", "45\n", "46\n", "47\n", "48\n", "49\n", "50\n", "51\n", "52\n", "53\n", "54\n", "55\n", "56\n", "57\n", "58\n", "59\n", "60\n", "61\n", "62\n", "63\n", "64\n", "65\n", "66\n", "67\n", "68\n", "69\n", "70\n", "71\n", "72\n", "73\n", "74\n", "75\n", "76\n", "77\n", "78\n", "79\n", "80\n", "81\n", "82\n", "83\n", "84\n", "85\n", "86\n", "87\n", "88\n", "89\n", "90\n", "91\n", "92\n", "93\n", "94\n", "95\n", "96\n", "97\n", "98\n", "99\n", "100\n", "done\n" ] } ], "source": [ "for num in range(0, 100):\n", " if num == 10:\n", " continue # exit the loop\n", " print(num+1)\n", "print('done')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# for loop example - range()\n", "for i in range(1, 21): # range ... 1...20\n", " if i%2 == 0:\n", " continue\n", " print(i)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# for loop example - range()\n", "for i in range(1, 21):\n", " if i%2 == 0:\n", " #break\n", " continue\n", " print(i)\n", "else:\n", " print('end')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(range)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## for ... else\n", "- The else clause executes when the loop completes normally. \n", "- This means that the loop did not encounter any break." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# for... else example\n", "# write a program to test whether a given number is prime\n", "n = 50\n", "#isPrime = True\n", "for i in range(2, n):\n", " if n%i == 0:\n", " #isPrime = False\n", " print(n, ' is not prime')\n", " break\n", "else:\n", " print(n, 'is prime')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## while loop \n", "- while loop is used when we typically not sure how many times the loop should be executed\n", "- syntax:\n", "``` python\n", "while condition:\n", " # loop body\n", "```\n", "- demonstrate countdown as a script\n", "- time.sleep(secs)\n", "- os.system(‘clear’); os.name - 'posix', 'nt', 'mac', 'os2', 'ce', 'java', 'riscos'\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# while loop - countdown\n", "import time\n", "import os\n", "i = 10\n", "while i >= 1:\n", " print(i)\n", " time.sleep(1)\n", " os.system('clear')\n", " i -= 1\n", "#time.sleep(1)\n", "else:\n", " print('Hapy new year!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## while... else\n", "- else clause executes when the loop condition becomes false, i.e., the loop executes normally\n", "- this means that the loop didn't encounter any break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## exercise 1\n", "Write a program to count the number of digits in a positive integer" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = 1234\n", "print(len(str(n)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## exercise 2\n", "Write a program to count the number of digits 0 or 5 in a positive ingeger." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = 123450\n", "n =str(n)\n", "for c in n:\n", " print(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## exercise 3\n", "FizzBuzz program - Write a program that prints numbers between 1 and 100 with the following requirements. If the number is divisible by 3, print 'Fizz'. If the number is divisible by 5, print 'Buzz'. If the number is divisible by both 3 and 5, print 'FizzBuzz'." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## exercise 4 \n", "What are the output(s) of the following code snippets?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "a = 11\n", "while a < 10:\n", " print(a)\n", " a += 1\n", " #break\n", "else:\n", " print('Done')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(1, 10):\n", " if i == 5:\n", " break\n", " print(i)\n", "else:\n", " print('done')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for c in 'hello':\n", " print(c, end=' ')" ] } ], "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.2" } }, "nbformat": 4, "nbformat_minor": 2 }