{ "metadata": { "name": "", "signature": "sha256:16d6a5d7a0f2261d7c4bbdc27385a261d6311ef3eb7e569b90710460f45fff6c" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 3 : Selections and Loops" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.1, Page No 48" ] }, { "cell_type": "code", "collapsed": false, "input": [ "dLimit = 500\n", "iNo = int(raw_input(\"Specify Quantity : \"))\n", "dUnitPrice = float(raw_input(\"Specify Unit Price : \"))\n", "dGross = iNo * dUnitPrice\n", "if(dGross > dLimit):\n", " dDisc = 10\n", "else:\n", " dDisc = 0;\n", "dNet = (100 - dDisc) * dGross / 100\n", "print \"Total Price : %d\" % dNet" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Specify Quantity : 100\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Specify Unit Price : 10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Total Price : 900\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.2, Page No 54" ] }, { "cell_type": "code", "collapsed": false, "input": [ "dNo1 = int(raw_input(\"Enter Number 1 : \"))\n", "dNo2 = int(raw_input(\"Enter Number 2 : \"))\n", "\n", "print \"1. Greatest\"\n", "print \"2. Least\"\n", "print \"3. Average\"\n", "\n", "iOpt = int(raw_input(\"Select : \"))\n", "\n", "if iOpt == 1:\n", " if dNo1 > dNo2:\n", " print dNo1\n", " else:\n", " print dNo2\n", " print \" is the greatest\"\n", "elif iOpt == 2:\n", " if dNo1 < dNo2:\n", " print dNo1\n", " else:\n", " print dNo2\n", " print \" is the least\"\n", "elif iOpt == 3:\n", " print \"The average is : \" , (dNo1 + dNo2)/2\n", "else:\n", " print \"wrong Choice\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number 1 : 10\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number 2 : 15\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "1. Greatest\n", "2. Least\n", "3. Average\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Select : 1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "15\n", " is the greatest\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.3, Page No 58" ] }, { "cell_type": "code", "collapsed": false, "input": [ "iLimit = int(raw_input(\"Enter limit : \"))\n", "iSum = 0\n", "for i in range(1,iLimit+1):\n", " iSum = iSum + i\n", "\n", "print \"The Sum Is : \" , iSum" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Example 3.4, Page No 61" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Calculation Of Product\"\n", "\n", "for i in range(1,37):\n", " for j in range(1,37):\n", " if (i * j) == 36:\n", " print i , \" and \" , j" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Calculation Of Product\n", "1 and 36\n", "2 and 18\n", "3 and 12\n", "4 and 9\n", "6 and 6\n", "9 and 4\n", "12 and 3\n", "18 and 2\n", "36 and 1\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.5, Page No 62" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import random\n", "\n", "iRoll = 0\n", "iNoOfRolls = 0\n", "\n", "while iRoll != 6:\n", " iRoll = random.randint(1,200) % 6 + 1\n", " iNoOfRolls = iNoOfRolls + 1\n", "\n", "print iNoOfRolls" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "8\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.6, Page No 63" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Do while concept is not in python thats way the program is implemented using WHILE\n", "import random\n", "\n", "iRoll = 0\n", "iNoOfRolls = 0\n", "\n", "while iRoll != 6:\n", " iRoll = random.randint(1,200) % 6 +1\n", " iNoOfRolls = iNoOfRolls + 1\n", "\n", "print iNoOfRolls" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.7, Page No 64" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import random\n", "\n", "iCounter = 0\n", "iRoll1 = random.randint(1,200) % 6 + 1\n", "iRoll2 = random.randint(1,200) % 6 + 1\n", " \n", "\n", "while iRoll1 != iRoll2:\n", " iRoll1 = random.randint(1,200) % 6 + 1\n", " iRoll2 = random.randint(1,200) % 6 + 1\n", " iCounter = iCounter + 1\n", "\n", "print \"The rolls were = \" , iRoll1\n", "print \"Number of attempts = \" , iCounter" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The rolls were = 4\n", "Number of attempts = 2\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.8, Page No 64" ] }, { "cell_type": "code", "collapsed": false, "input": [ "iSum = 0\n", "i = 0\n", "iNo = raw_input(\"Enter a number : \")\n", "\n", "while iNo.isdigit():\n", " iSum = iSum + int(iNo)\n", " i = i + 1\n", " iNo = raw_input(\"Enter one more number : \")\n", "\n", "print \"Average = \" , (float)((iSum)/i)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a number : 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter one more number : 6\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter one more number : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter one more number : c\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Average = 5.0\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.9, Page No 66" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import random\n", "iNO = 0\n", "for i in range(1,14):\n", " iNo = random.randint(1,200) % 3\n", " if iNo == 0:\n", " print \"1\"\n", " elif iNo == 1:\n", " print \"X\"\n", " elif iNO == 2:\n", " print \"2\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "X\n", "X\n", "X\n", "X\n", "1\n", "1\n", "X\n", "1\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.10, Page No 68" ] }, { "cell_type": "code", "collapsed": false, "input": [ "iNo = 0\n", "iRoot = 1\n", "while iNo < 2 and iRoot <=100:\n", " LP = iRoot * (iRoot - 6) * (iRoot + 8)\n", " if LP == 0:\n", " iNo = iNo + 1\n", " print iRoot\n", " iRoot = iRoot + 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.11, Page No 69" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import math\n", "\n", "while 1 == 1:\n", " dNo = int(raw_input(\"Enter a number : \"))\n", " if dNo <= 0:\n", " break\n", " print \"The square root of the number is \",math.sqrt(dNo)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a number : 100\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The square root of the number is 10.0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a number : 0\n" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }