{ "metadata": { "name": "", "signature": "sha256:d985967b3e8d0e583d78d0a3306851320f330cc48579a1f62da314746570750f" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 4 Looping Structures" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.1 , Page No.88" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x=0\n", "print \"The value of x is \",x\n", "x+=1\n", "print \"The value of x is \",x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The value of x is 0\n", "The value of x is 1\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.2, Page No.89" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x=0\n", "y=0\n", "print \"The value of y is \", y+1\n", "print \"The value of x is \", x+1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The value of y is 1\n", "The value of x is 1\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.3, Page No.89" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x=0\n", "y=1\n", "x=(y)*2\n", "print \"The Value of x \",x\n", "x=0\n", "y=1\n", "x=(y+1)*2\n", "print \"The Value of x \",x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The Value of x 2\n", "The Value of x 4\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.4, Page No.90" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x=0\n", "y=0\n", "x=(y)*4\n", "print \"The Value of x\", x\n", "y=0\n", "x=(y+1)*4\n", "print \"The Value of x\", x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The Value of x 0\n", "The Value of x 4\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.5, Page No.92" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x=1\n", "y=1\n", "x=(y)*4\n", "print \"The value of x is \",x\n", "y=1\n", "x=(y-1)*4\n", "print \"The value of x is \",x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The value of x is 4\n", "The value of x is 0\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.6, Page No. 93" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x=1\n", "y=2\n", "x=y*x+1\n", "print \"The value of x is: \",x\n", "x=1\n", "y=2\n", "x+=y*x+1\n", "print \"The value of x is: \",x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The value of x is: 3\n", "The value of x is: 4\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.7, Page No.94" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x=1\n", "y=2\n", "x=y*x+1\n", "print \"The value of x is: \",x\n", "x=1\n", "y=2\n", "x-=y*x+1\n", "print \"The value of x is: \",x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The value of x is: 3\n", "The value of x is: -2\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.8, Page No.95" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x=0\n", "while (x<10):\n", " print \"The value of x is \",x\n", " x=x+1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The value of x is 0\n", "The value of x is 1\n", "The value of x is 2\n", "The value of x is 3\n", "The value of x is 4\n", "The value of x is 5\n", "The value of x is 6\n", "The value of x is 7\n", "The value of x is 8\n", "The value of x is 9\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.9, Page No.97" ] }, { "cell_type": "code", "collapsed": false, "input": [ "iSelection=0\n", "while (iSelection!=4):\n", " print \"1\\tDeposit Funds\"\n", " print \"2\\tWithdraw Funds\"\n", " print \"3\\tPrint Balance\"\n", " print \"4\\tQuit\"\n", " iSelection=int(raw_input(\"Enter Your Selection(1-4): \"))\n", "print \"Thank you\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\tDeposit Funds\n", "2\tWithdraw Funds\n", "3\tPrint Balance\n", "4\tQuit\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Your Selection(1-4): 1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "1\tDeposit Funds\n", "2\tWithdraw Funds\n", "3\tPrint Balance\n", "4\tQuit\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Your Selection(1-4): 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Thank you\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.10, Page No.99" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x=9\n", "while (x<10):\n", " print \"This printf statement is executed at least once\"\n", " x=x+1\n", "while (x<10):\n", " print \"This printf statement is never executed \"\n", " x=x+1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "This printf statement is executed at least once\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.11, Page No.100" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for x in range(10,5,-1):\n", " print \"The value of x is \",x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The value of x is 10\n", "The value of x is 9\n", "The value of x is 8\n", "The value of x is 7\n", "The value of x is 6\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.12, Page No.101" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import random\n", "iNumQuestions=int(raw_input(\"Enter the number of questions to ask: \"))\n", "for x in range(iNumQuestions):\n", " iRndNum1=(random.randint(0,100)%10)+1\n", " iRndNum2=(random.randint(0,100)%10)+1\n", " iResponse=int(raw_input(\"what is \"+ str(iRndNum1)+\" * \"+str(iRndNum2)+\" : \"))\n", " if(iResponse==(iRndNum1*iRndNum2)):\n", " print \"Correct\"\n", " else:\n", " print \"The Corrent answer was \", iRndNum1*iRndNum2" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the number of questions to ask: 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "what is 6 * 9 : 54\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Correct\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "what is 8 * 5 : 32\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The Corrent answer was 40\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.13, Page No.102" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for x in range(10,5,-1):\n", " if x==7:\n", " break\n", "print x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "7\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.14, Page No.103" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for x in range(10,5,-1):\n", " if x==7:\n", " continue\n", " print x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10\n", "9\n", "8\n", "6\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.15, Page No.104" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for x in range(25):\n", " print \"\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.16, Page No. 106" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import time\n", "import random\n", "import os\n", "cYesNo=raw_input(\"Play a game of Concentration?(y or n)\")\n", "if(cYesNo=='y' or cYesNo=='Y'):\n", " i1=random.randint(0,100) % 100\n", " i2=random.randint(0,100) % 100\n", " i3=random.randint(0,100) % 100\n", " print \"Concentrate on the next three numbers\\n\",i1,i2,i3\n", " iCurrentTime=int(round(time.time()*1000))\n", " iElapsedTime=int(round(time.time()*1000))\n", " while((iCurrentTime-iElapsedTime)<3):\n", " iElapsedTime=int(round(time.time()*1000))\n", " os.system('cls')\n", " iResp1=int(raw_input(\"Enter a number:\"))\n", " iResp2=int(raw_input(\"Enter a number:\"))\n", " iResp3=int(raw_input(\"Enter a number:\"))\n", " \n", " if(i1==iResp1 and i2==iResp2 and i3==iResp3):\n", " print \"Congratulations!\"\n", " else:\n", " print \"Sorry, Correct numbers were \",i1,i2,i3" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Play a game of Concentration?(y or n)n\n" ] } ], "prompt_number": 1 } ], "metadata": {} } ] }