{ "metadata": { "name": "", "signature": "sha256:10a432341f6033fcf678cc580d953b0c94ad7350aa1431ba3e0517904d3e90c8" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 8: More control statements" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 8.1, Page number: 144" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "# Variable declaration\n", "total = 0\n", "counter = 0\n", "\n", "# Calculation\n", "while (counter < 5) :\n", " current = 3\n", " total += current\n", " counter += 1\n", "\n", "# Result\n", "print ('The grand total is %d' % total)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The grand total is 15\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 8.2, Page number: 145" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "# Variable declaration\n", "total = 0\n", "\n", "# Calculation\n", "for counter in range (0, 5) :\n", " current = 5\n", " total += current\n", "\n", "# Result\n", "print ('The grand total is %d' % total)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The grand total is 25\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 8.3, Page number: 146" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "# Variable declaration, calculation and result\n", "for celsius in range (0, 101) :\n", " print ('Celsius: %d Fahrenheit: %d' % (celsius, (celsius * 9) / 5 +32))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Celsius: 0 Fahrenheit: 32\n", "Celsius: 1 Fahrenheit: 33\n", "Celsius: 2 Fahrenheit: 35\n", "Celsius: 3 Fahrenheit: 37\n", "Celsius: 4 Fahrenheit: 39\n", "Celsius: 5 Fahrenheit: 41\n", "Celsius: 6 Fahrenheit: 42\n", "Celsius: 7 Fahrenheit: 44\n", "Celsius: 8 Fahrenheit: 46\n", "Celsius: 9 Fahrenheit: 48\n", "Celsius: 10 Fahrenheit: 50\n", "Celsius: 11 Fahrenheit: 51\n", "Celsius: 12 Fahrenheit: 53\n", "Celsius: 13 Fahrenheit: 55\n", "Celsius: 14 Fahrenheit: 57\n", "Celsius: 15 Fahrenheit: 59\n", "Celsius: 16 Fahrenheit: 60\n", "Celsius: 17 Fahrenheit: 62\n", "Celsius: 18 Fahrenheit: 64\n", "Celsius: 19 Fahrenheit: 66\n", "Celsius: 20 Fahrenheit: 68\n", "Celsius: 21 Fahrenheit: 69\n", "Celsius: 22 Fahrenheit: 71\n", "Celsius: 23 Fahrenheit: 73\n", "Celsius: 24 Fahrenheit: 75\n", "Celsius: 25 Fahrenheit: 77\n", "Celsius: 26 Fahrenheit: 78\n", "Celsius: 27 Fahrenheit: 80\n", "Celsius: 28 Fahrenheit: 82\n", "Celsius: 29 Fahrenheit: 84\n", "Celsius: 30 Fahrenheit: 86\n", "Celsius: 31 Fahrenheit: 87\n", "Celsius: 32 Fahrenheit: 89\n", "Celsius: 33 Fahrenheit: 91\n", "Celsius: 34 Fahrenheit: 93\n", "Celsius: 35 Fahrenheit: 95\n", "Celsius: 36 Fahrenheit: 96\n", "Celsius: 37 Fahrenheit: 98\n", "Celsius: 38 Fahrenheit: 100\n", "Celsius: 39 Fahrenheit: 102\n", "Celsius: 40 Fahrenheit: 104\n", "Celsius: 41 Fahrenheit: 105\n", "Celsius: 42 Fahrenheit: 107\n", "Celsius: 43 Fahrenheit: 109\n", "Celsius: 44 Fahrenheit: 111\n", "Celsius: 45 Fahrenheit: 113\n", "Celsius: 46 Fahrenheit: 114\n", "Celsius: 47 Fahrenheit: 116\n", "Celsius: 48 Fahrenheit: 118\n", "Celsius: 49 Fahrenheit: 120\n", "Celsius: 50 Fahrenheit: 122\n", "Celsius: 51 Fahrenheit: 123\n", "Celsius: 52 Fahrenheit: 125\n", "Celsius: 53 Fahrenheit: 127\n", "Celsius: 54 Fahrenheit: 129\n", "Celsius: 55 Fahrenheit: 131\n", "Celsius: 56 Fahrenheit: 132\n", "Celsius: 57 Fahrenheit: 134\n", "Celsius: 58 Fahrenheit: 136\n", "Celsius: 59 Fahrenheit: 138\n", "Celsius: 60 Fahrenheit: 140\n", "Celsius: 61 Fahrenheit: 141\n", "Celsius: 62 Fahrenheit: 143\n", "Celsius: 63 Fahrenheit: 145\n", "Celsius: 64 Fahrenheit: 147\n", "Celsius: 65 Fahrenheit: 149\n", "Celsius: 66 Fahrenheit: 150\n", "Celsius: 67 Fahrenheit: 152\n", "Celsius: 68 Fahrenheit: 154\n", "Celsius: 69 Fahrenheit: 156\n", "Celsius: 70 Fahrenheit: 158\n", "Celsius: 71 Fahrenheit: 159\n", "Celsius: 72 Fahrenheit: 161\n", "Celsius: 73 Fahrenheit: 163\n", "Celsius: 74 Fahrenheit: 165\n", "Celsius: 75 Fahrenheit: 167\n", "Celsius: 76 Fahrenheit: 168\n", "Celsius: 77 Fahrenheit: 170\n", "Celsius: 78 Fahrenheit: 172\n", "Celsius: 79 Fahrenheit: 174\n", "Celsius: 80 Fahrenheit: 176\n", "Celsius: 81 Fahrenheit: 177\n", "Celsius: 82 Fahrenheit: 179\n", "Celsius: 83 Fahrenheit: 181\n", "Celsius: 84 Fahrenheit: 183\n", "Celsius: 85 Fahrenheit: 185\n", "Celsius: 86 Fahrenheit: 186\n", "Celsius: 87 Fahrenheit: 188\n", "Celsius: 88 Fahrenheit: 190\n", "Celsius: 89 Fahrenheit: 192\n", "Celsius: 90 Fahrenheit: 194\n", "Celsius: 91 Fahrenheit: 195\n", "Celsius: 92 Fahrenheit: 197\n", "Celsius: 93 Fahrenheit: 199\n", "Celsius: 94 Fahrenheit: 201\n", "Celsius: 95 Fahrenheit: 203\n", "Celsius: 96 Fahrenheit: 204\n", "Celsius: 97 Fahrenheit: 206\n", "Celsius: 98 Fahrenheit: 208\n", "Celsius: 99 Fahrenheit: 210\n", "Celsius: 100 Fahrenheit: 212\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 8.4, Page number: 147" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "# Variable declaration\n", "seven_count = 0\n", "three_count = 0\n", "data = []\n", "\n", "# Calculation\n", "for i in range (0, 5) :\n", " x = 7\n", " data.append(int(x))\n", "print (data)\n", "\n", "for index in range (0, 5) :\n", " if data[index] == 3 :\n", " three_count += 1\n", "\n", " if data[index] == 7 :\n", " seven_count += 1\n", "\n", "# Result\n", "print ('Threes %d Sevens %d' % (three_count, seven_count))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[7, 7, 7, 7, 7]\n", "Threes 0 Sevens 5\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 8.6, Page number: 149" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "# Variable declaration\n", "result = 0\n", "i = 0\n", "\n", "# Calculation and result\n", "while (i < 3) :\n", " print ('Result: %d' % result)\n", " operator = '-'\n", " value = 10\n", "\n", " if operator == 'q' or operator == 'Q' :\n", " break\n", "\n", " elif operator == '+' :\n", " result += value\n", "\n", " elif operator == '-' :\n", " result -= value\n", "\n", " elif operator == '*' :\n", " result *= value\n", "\n", " elif operator == '/' :\n", " if value == 0 :\n", " print ('Error: Divide by zero')\n", " print ('operation ignored') \n", " else :\n", " result /= value\n", "\n", " else :\n", " print ('Unknown operator %c' % operator)\n", " i = i + 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Result: 0\n", "Result: -10\n", "Result: -20\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }