{ "metadata": { "name": "", "signature": "sha256:84d44e5e34aa0042556dda8bb39ee06beeff8eaea8bb2913d8710f888ef51867" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 7 - The For Loop" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.1, page no. 142" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "num = 2\n", "num += 1\n", "print num" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.2, page no. 142" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "num = 2\n", "num += 1\n", "print num" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.3, page no. 142" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "num = 2\n", "print num\n", "num += 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.4, page no. 143" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "num = 2\n", "num -= 1\n", "print num" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.5, page no. 143" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "\n", "num = 2\n", "num -= 1\n", "print num" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.6, page no. 144" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "num = 2\n", "num -= 1\n", "print num" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.7, page no. 146" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "num = 1\n", "print num\n", "num += 1\n", "print num\n", "num += 1\n", "print num\n", "num += 1\n", "print num\n", "num += 1\n", "print num\n", "num += 1\n", "print num\n", "num += 1\n", "print num\n", "num += 1\n", "print num\n", "num += 1\n", "print num\n", "num += 1\n", "print num" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.8, page no. 146" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "for num in range(1, 11):\n", " print num\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.9, page no. 147" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "for num in range(1, 101):\n", " print num," ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.10, page no. 149" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "num = 1\n", "while num <= 10:\n", " print num,\n", " num += 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 2 3 4 5 6 7 8 9 10\n" ] } ], "prompt_number": 14 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.11, page no. 149" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "num = 1\n", "while num <= 10:\n", " print num," ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.12, page no. 150" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "total = 1;\n", "print \"Enter a number: \",\n", "num = int(raw_input())\n", "print \"The factorial of \", num, \" is \",\n", "for counter in range(1, num+1):\n", " total *= counter\n", "print total" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The factorial of 5 is 120\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.13, page no. 150" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "secret = 3\n", "print \"Guess a number between 1 and 10\"\n", "print \"You have 3 tries\"\n", "for counter in range(1,4):\n", " print \"Enter the number now: \",\n", " num = int(raw_input())\n", " if (num == secret):\n", " print \"You guessed the secret number!\"\n", " break\n", "print \"Program over\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Guess a number between 1 and 10\n", "You have 3 tries\n", "Enter the number now: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter the number now: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter the number now: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Program over\n" ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.14, page no. 151" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "secret = 3\n", "print \"Guess a number between 1 and 10\\n\"\n", "print \"You have 3 tries\\n\"\n", "keepgoing = True\n", "for counter in range(1, 4):\n", " if keepgoing == True:\n", " print \"Enter the number now: \",\n", " num = int(raw_input())\n", " if (num == secret):\n", " print \"You guessed the secret number!\"\n", " keepgoing = False\n", " else:\n", " break\n", "print \"Program over\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Guess a number between 1 and 10\n", "\n", "You have 3 tries\n", "\n", "Enter the number now: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " You guessed the secret number!\n", "Program over\n" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.15, page no. 152" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "num = 1\n", "while True:\n", " if (num > 10):\n", " break\n", " else:\n", " print num, \" \",\n", " num += 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 2 3 4 5 6 7 8 9 10 \n" ] } ], "prompt_number": 20 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.16, page no 153" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "total = 0\n", "print \"How many items do you want to buy: \",\n", "num = int(raw_input())\n", "for counter in range(1, num+1):\n", " if (counter % 13 == 0):\n", " continue\n", " total += 3\n", "print \"Total for \", num, \" items is $\", total" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many items do you want to buy: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Total for 5 items is $ 15\n" ] } ], "prompt_number": 21 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.17, page no. 154" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "total = 0\n", "print \"How many items do you want to buy: \",\n", "num = int(raw_input())\n", "keepgoing = True\n", "for counter in range(1, num+1):\n", " if (not(counter % 13 == 0 )):\n", " total += 3\n", "print \"Total for \", num, \" items is $\", total" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many items do you want to buy: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Total for 6 items is $ 18\n" ] } ], "prompt_number": 22 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.18, page no. 154" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "for x in range(1, 6):\n", " for y in range(1, 11):\n", " print \"X\",\n", " print \"\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "X X X X X X X X X X \n", "\n", "X X X X X X X X X X \n", "\n", "X X X X X X X X X X \n", "\n", "X X X X X X X X X X \n", "\n", "X X X X X X X X X X \n", "\n" ] } ], "prompt_number": 23 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.19, page no. 155" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter number of salespersons: \",\n", "persons = int(raw_input())\n", "print \"Enter number of sales per salesperson: \",\n", "numSales = int(raw_input())\n", "for x in range(1, persons+1):\n", " total = 0\n", " for y in range(1, numSales+1):\n", " print \"Enter sale \", y, \" for salesperson \", x, \": \"\n", " sale = int(raw_input())\n", " total += sale\n", " average = float(total/numSales)\n", " print \"Average sales for salesperson #\", x, \" is \", average" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter number of salespersons: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter number of sales per salesperson: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter sale 1 for salesperson 1 : \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter sale 2 for salesperson 1 : \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Average sales for salesperson # 1 is 1.0\n", "Enter sale 1 for salesperson 2 : \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter sale 2 for salesperson 2 : \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Average sales for salesperson # 2 is 3.0\n", "Enter sale 1 for salesperson 3 : \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter sale 2 for salesperson 3 : \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Average sales for salesperson # 3 is 5.0\n" ] } ], "prompt_number": 24 } ], "metadata": {} } ] }