{ "cells": [ { "metadata": { "collapsed": true }, "cell_type": "markdown", "source": "# Introduction to Programming with Python\n\n# Unit 4: Loops\n\nOur task of generating a problem book with quadratic equations will only be useful, if we can generate many equations of the same type, not just one. Computer is very good at repeating the same computations, so the ability for us to express the idea of repeating something over and over is very important. This concept is called **a loop**.\n\nThe best way to create a loop is by using **for** operator: " }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "for color in [\"red\",\"green\",\"blue\"]:\n print(\"Roses are \"+color)", "execution_count": 1, "outputs": [ { "output_type": "stream", "text": "Roses are red\nRoses are green\nRoses are blue\n", "name": "stdout" } ] }, { "metadata": {}, "cell_type": "markdown", "source": "In this example, we have a list of colors (written in square brackets `[]`), and **for loop** repeats the `print` operator several times, one time for each color in the list. In the code block below **for**, variable `color` takes the value of each color in turn.\n\nVery often we want to repeat something several times. In order for us not to write a list, we can use the special function `range(n)` to specify the range of number from 0 to the given number. For example, to print all the numbers from 1 to 10, we can write: " }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "for i in range(10):\n print(i+1, end=' ')", "execution_count": 2, "outputs": [ { "output_type": "stream", "text": "1 2 3 4 5 6 7 8 9 10 ", "name": "stdout" } ] }, { "metadata": {}, "cell_type": "markdown", "source": "Notice two things here:\n* We print `i+1` and not `i`, because `i` changes from 0 to 9. It is important to remember that in Python numbering starts with 0.\n* We use `end=' '` construction to indicate what `print` shoud do after printing the specified value. By default `print` goes to the new line, and by specifying space as an end-characted we can print all numbers in one line " }, { "metadata": {}, "cell_type": "markdown", "source": "## Drawing a Street\n\nRemember that we defined functon to draw a house earlier? Let's now explore our notion of loops and draw a street of houses! But first of all, let's also simplify our function of drawing a square using for loop:" }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "import jturtle as turtle", "execution_count": 1, "outputs": [] }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "def square(x):\n for t in range(4):\n turtle.forward(x)\n turtle.right(90)\n \nsquare(10)\nturtle.done()", "execution_count": 2, "outputs": [ { "output_type": "display_data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s8RAhdogQO0SIHSLEDhFihwixQ4TYIULsECF2iBA7RIgdIsQOEWKHCLFDhNghQuwQIXaIEDtEiB0ixA4RYocIsUOE2CFC7BAhdogQO0SIHSLEDhFihwixQ4TYIULsECF2iBA7RIgdIsQOEWKHCLFDhNgh4gfoIxDxKdJhIQAAAABJRU5ErkJggg==\n", "text/plain": "
" }, "metadata": { "needs_background": "light" } } ] }, { "metadata": {}, "cell_type": "markdown", "source": "Now the function to draw the house itself. We will add a few more lines to the end of the function in order to return the turtle to the original position in the lower left corner of the drawing:" }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "def house(size):\n square(size)\n turtle.forward(size)\n turtle.right(30)\n turtle.forward(size)\n turtle.right(120)\n turtle.forward(size)\n turtle.right(30)\n\n turtle.penup()\n turtle.forward(2*size/3)\n turtle.right(90)\n turtle.forward(size/3)\n turtle.pendown()\n\n square(size/3)\n turtle.penup()\n turtle.forward(2*size/3)\n turtle.left(90)\n turtle.forward(size/3)\n turtle.left(180)\n turtle.pendown()\n\nhouse(10)\nturtle.done()", "execution_count": 6, "outputs": [ { "output_type": "display_data", "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQYAAAD4CAYAAAAO2kjhAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4wLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvqOYd8AAAC3dJREFUeJzt3X2wbXVdx/H3V4GoCInAtEIdB8V4KLRsNCctMwlwwqxo1CxJaKQsQElIlIwmE+WxYsLUsnyILBy11JpJzTKjIYEyuViMGdpgNvlASoTppz/Wrq589XLvuXvv3z57v1//MMC5Z39mzrnvOWvts9aqJEjSzu4xeoCk1WMYJDWGQVJjGCQ1hkFSYxgkNYZBUmMYJDWGQVJjGCQ1hkFSYxgkNYZBUmMYJDWGQVJjGCQ1hkFSYxgkNYZBUmMYJDWGQVJjGCQ1hm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6TGMEhqDIOkxjBIagyDpMYwSGoMg6TGMEhqDIOkxjBIagyDpMYwSGr2GT1g3fkoem1HhmGBktToDdJWeCghqTEMkhrDIKkxDJIawyCpMQySGsMgqTEMkhrDIKkxDJIawyCpMQySGsMgqTEMkhrDIKkxDJIawyCpMQySGsMgqTEMkhrDIKkxDJIawyCpMQySGsMgqTEMkhrDIKkxDJIawyCpMQySGsMgqdln9IBFq6qM3iBtN5X490bSF/JQQlJjGCQ1hkFSYxgkNYZBUmMYJDWGQVJjGCQ1hkFSYxgkNYZBUmMYJDWGQVJjGCQ1hkFSYxgkNYZBUmMYJDWGQVJjGCQ1hkFSYxgkNYZBUmMYJDWGQVJjGCQ1hkFSYxgkNYZBUmMYJDWGQVJjGCQ1hkFSYxgkNYZBUmMYJDWGQVJjGCQ1hkFSYxgkNf8DVWR5kyH9ffwAAAAASUVORK5CYII=\n", "text/plain": "
" }, "metadata": { "needs_background": "light" } } ] }, { "metadata": {}, "cell_type": "markdown", "source": "To draw 5 houses in a street, we basically need to call the `house` function 5 times in a loop. However, if we just do that, all houses will be rendered on top of each other, and we will not actually see them. " }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "for i in range(5):\n house(10)\nturtle.done()", "execution_count": 7, "outputs": [ { "output_type": "display_data", "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQYAAAD4CAYAAAAO2kjhAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4wLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvqOYd8AAACv9JREFUeJzt3W3IZGUZwPH/Veaq26qZZinpWmkZa1YaliVlkPYegqRiGBSCRVqJYRQF9aEoiCIiKXqhUhSFElRsxV5JM7Ha0NJS15dIQhOzVNJ0rz7MbT3u1e76PDsz98yc/+/LeXRnZy5c98/MOXPuOzITSVrqKb0HkDR7DIOkwjBIKgyDpMIwSCoMg6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3DNJK+FFCUmEYJBWGQVJhGCQVhkFSYRgkFYZBUmEYJBWGQVJhGCQVhkFSYRgkFYZBUmEYJBWGQVJhGCQVhkFSYRgkFYZBUmEYJBWGQVJhGCQVhkFSYRgkFYZBUmEYJBWGQVJhGCQVhkFSYRgkFYZBUrFD7wEmLSKy9wzSvIlM/95IeiI/SkgqDIOkwjBIKgyDpMIwSCoMg6TCMEgqDIOkwjBIKgyDpMIwSCoMg6TCMEgqDIOkwjBIKgyDpMIwSCoMg6TCMEgqDIOkwjBIKgyDpMIwSCoMg6TCMEgqDIOkwjBIKgyDpMIwSCoMg6TCMEgqDIOkwjBIKgyDpMIwSCoMg6TCMEgqDIOkwjBIKgyDpOI/KAZEPjFf8hgAAAAASUVORK5CYII=\n", "text/plain": "
" }, "metadata": { "needs_background": "light" } } ] }, { "metadata": {}, "cell_type": "markdown", "source": "To draw all houses in a row, we need to add some code to move the turtle in between houses, i.e. to shift to the right each time:" }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "for i in range(5):\n house(10)\n turtle.penup()\n turtle.right(90)\n turtle.forward(11)\n turtle.left(90)\n turtle.pendown()\nturtle.done()", "execution_count": 8, "outputs": [ { "output_type": "display_data", "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4wLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvqOYd8AAADNBJREFUeJzt3WmsJUUZh/GnZhgYRUAU9x0VdySCO5iIRkAlEY0RcEVJ3AKiURTQACoo4AQGFYKKC2hEo2AUd1ETIIoRF9wQFQVFwTUu0UQ/lB+qTu7xepezdHfdO+/zSzpk+vTynu7771PdXd2knDOStn0bWhcgaRiGXQrCsEtBGHYpCMMuBWHYpSAMuxSEYZeCMOxSEIZdCsKwS0EYdikIwy4FYdilIAy7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xvrDOD8gde5YpMQuAA4deCalm0SsvCOvameIOugpsNY4h1t9bMNNHjHHnBM3Rb/d2GMRu/Yo3TJ/tBg6xvyy3W4ke5fj4p3brDu84Azlxj/aDrqpjlDTZcAb1hi/NNo0E2ThW7LRyzx2YvrZ4NejWah2/LTlvjsOOCSBvttJ+C3wKMGWd/QX7CjjfRp4NhG6/6f96rXcRsoV6Jf2Kim/2sSsvBe/AMb1bRP/UPeeWzczjR8Lz5wUN0m24+NG70X/76NanpR/dvp7Em95YZ1d4EupXQA5Tx0a4v155z/QHlRwpljF8aeSwn8hY1q+gXlNt+pY6OPAq7LOX+hUU3fpjSbTxgb/Ubg8/WzFjV9ntIF9qix0W8D3lu3YQsXUO4qHd77mloczeY8Eo4ezVwTQ63pqNZ1jA0ba00nroFaRkOqw5Y1UMv4ftsInNO6lvGa+hzW5f/YMaWUc87N/xe543WshZoW12BN67emPqy7Zryk2Rh2KQjDLgVh2KUgDLsUhGGXgjDsUhCGXQrCsEtBrPYihnUvpTRVF8EhelJZ02SsqVvbfNhh8g0+7Y6chzVNxpq6YzNeCsKwS0EYdikIwy4FYdilIAy7FIRhl4Iw7FIQhl0KIkQPurXWkwmsaVLW1J1tPuxrqW/yiDVNxpq6ZTNeCsKwS0EYdikIwy4FYdilIAy7FIRhl4Iw7FIQhl0KwrBLQazb7rJrsX+yNU3GmtpIOW/z31ESNuOlMAy7FIRhl4Iw7FIQhl0KwrBLQRh2KQjDLgVh2KUgDLsUhGGXgjDsUhCGXQrCsEtBGHYpCMMuBWHYpSAMuxSEYZeCMOxSEIZdCsKwS0EYdikIwy4FYdilIAy7FIRhl4Iw7FIQhl0KwrBLQRh2KQjDLgVh2KUgDLsUhGGXgjDsUhCGXQrCsEtBGHYpCMMuBfFf4Kd24io02UUAAAAASUVORK5CYII=\n", "text/plain": "
" }, "metadata": { "needs_background": "light" } } ] }, { "metadata": {}, "cell_type": "markdown", "source": "Congratulations, now you have mastered the **for**-loop! However, let us have a little bit more practice with loops." }, { "metadata": {}, "cell_type": "markdown", "source": "## Creating a Problem Book\n\nNot that we know loops, we can easily produce a problem book with many different problems. In order not to duplicate functions defined in previous modules here, we collected them into an external module, and just write the `import` statement here to use them. If you have doubts how a function is defined - you can either have a look into the previous module, or into [pycourse.py](pycourse.py) file that contains all definitions." }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "import random,math\nfrom IPython.display import display, Math\n\nfrom pycourse import coef, equation, solve\n\ndef random_equation():\n a = random.randint(1,5)*random.choice([-1,1])\n b = random.randint(-10,10)\n c = random.randint(-20,20)\n return (a,b,c)\n\ndef print_random_equation():\n a,b,c = random_equation()\n if solve(a,b,c) == None:\n display(Math(equation(a,b,c)+\"\\quad (\\mbox{no solutions})\"))\n else:\n x1,x2 = solve(a,b,c)\n display(Math(equation(a,b,c)+f\"\\quad (x_1={x1:.2f},x_2={x2:.2f})\"))", "execution_count": 9, "outputs": [] }, { "metadata": { "trusted": true }, "cell_type": "markdown", "source": "This code mostly duplicates the code from last unit, but there are a couple of new things here as well. First of all, there are weird things what we print out, such as `\\quad` and `\\mbox`. Do not worry about them, those are used to make equations look a bit better. They are not part of Python language, but rather they are used inside Azure Notebooks to render math formulae.\n\nSecond new feature is that we can see here is called **string interpolation** and **formatting**. Often we have the situation when we need to print the value of some varible `x` inside the string, for example:" }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "x = 10/3 # Suppose I have spent 2/3 of my daily pocket money of $10\nprint('I have',x,'dollars left')", "execution_count": 10, "outputs": [ { "output_type": "stream", "text": "I have 3.3333333333333335 dollars left\n", "name": "stdout" } ] }, { "metadata": {}, "cell_type": "markdown", "source": "If I want for some reason to store the whole message in a variable to print it later, I would need to put in some manual type conversion, like this:" }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "message = 'I have '+str(x)+' dollars left.'\nprint(message)", "execution_count": 11, "outputs": [ { "output_type": "stream", "text": "I have 3.3333333333333335 dollars left.\n", "name": "stdout" } ] }, { "metadata": {}, "cell_type": "markdown", "source": "The same effect can be accomplished using special Python syntax for string interpolation:" }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "message = f'I have {x} dollars left'\nprint(message)", "execution_count": 12, "outputs": [ { "output_type": "stream", "text": "I have 3.3333333333333335 dollars left\n", "name": "stdout" } ] }, { "metadata": {}, "cell_type": "markdown", "source": "String interpolation starts with `f'`, and any variable or expression that appears inside curly braces `{..}` will be substituted by its value.\n\nOne more thing that can be used with interpolation is **formatting**. You may have noticed that in our case the number `3.333333` does not look nice. It would be better to round it up to, say, 2 decimal places. While we can achieve it with `math.round` function, we can also specify **format** for printing the number, i.e. how many decimal places we want:" }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "print(f'I have {x:0.2f} dollars left')", "execution_count": 13, "outputs": [ { "output_type": "stream", "text": "I have 3.33 dollars left\n", "name": "stdout" } ] }, { "metadata": {}, "cell_type": "markdown", "source": "Now that we have explained all interesting places in the code, let's use `print_random_equation` function to generate a number or random equations!" }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "for i in range(10):\n print_random_equation()", "execution_count": 14, "outputs": [ { "output_type": "display_data", "data": { "text/latex": "$\\displaystyle -x^2+9x-12\\quad (x_1=1.63,x_2=7.37)$", "text/plain": "" }, "metadata": {} }, { "output_type": "display_data", "data": { "text/latex": "$\\displaystyle -5x^2-x+12\\quad (x_1=-1.65,x_2=1.45)$", "text/plain": "" }, "metadata": {} }, { "output_type": "display_data", "data": { "text/latex": "$\\displaystyle -2x^2+10x-5\\quad (x_1=0.56,x_2=4.44)$", "text/plain": "" }, "metadata": {} }, { "output_type": "display_data", "data": { "text/latex": "$\\displaystyle -4x^2-6x+3\\quad (x_1=-1.90,x_2=0.40)$", "text/plain": "" }, "metadata": {} }, { "output_type": "display_data", "data": { "text/latex": "$\\displaystyle 5x^2-10x\\quad (x_1=2.00,x_2=0.00)$", "text/plain": "" }, "metadata": {} }, { "output_type": "display_data", "data": { "text/latex": "$\\displaystyle x^2-5x+5\\quad (x_1=3.62,x_2=1.38)$", "text/plain": "" }, "metadata": {} }, { "output_type": "display_data", "data": { "text/latex": "$\\displaystyle -x^2-x+14\\quad (x_1=-4.27,x_2=3.27)$", "text/plain": "" }, "metadata": {} }, { "output_type": "display_data", "data": { "text/latex": "$\\displaystyle -x^2-9x-4\\quad (x_1=-8.53,x_2=-0.47)$", "text/plain": "" }, "metadata": {} }, { "output_type": "display_data", "data": { "text/latex": "$\\displaystyle -5x^2-5x+10\\quad (x_1=-2.00,x_2=1.00)$", "text/plain": "" }, "metadata": {} }, { "output_type": "display_data", "data": { "text/latex": "$\\displaystyle -x^2+x-10\\quad (\\mbox{no solutions})$", "text/plain": "" }, "metadata": {} } ] }, { "metadata": { "trusted": true }, "cell_type": "markdown", "source": "This concludes our module. Please complete some exercises before going to the next one!" }, { "metadata": {}, "cell_type": "markdown", "source": "## Exercise: Factorial\n\nA factorial of number $n$ (denoted by $n!$) a number that is obtained by multiplying all natural numbers from $1$ to $n$, i.e.\n$$n! = 1\\cdot2\\cdot3\\cdot\\dots\\cdot n$$\nYou need to:\n1. Write a function `fact` that will calculate the factorial of any number `n`\n2. Print a table of factorals for $n$ from 1 to 7" }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "", "execution_count": null, "outputs": [] } ], "metadata": { "kernelspec": { "name": "python36", "display_name": "Python 3.6", "language": "python" }, "language_info": { "mimetype": "text/x-python", "nbconvert_exporter": "python", "name": "python", "pygments_lexer": "ipython3", "version": "3.6.6", "file_extension": ".py", "codemirror_mode": { "version": 3, "name": "ipython" } } }, "nbformat": 4, "nbformat_minor": 2 }