{ "metadata": { "name": "", "signature": "sha256:7e105f47cd21746fb8dd2ed76992ed00bd717837d537834fb5013cd214e21609" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Hour 7: Working with loops" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.1, Page No.106" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c=' '\n", "print \"Enter a character:\\n(Eneter X to exit)\"\n", "while (c!='x'):\n", " c=raw_input()\n", " print c\n", "print \"Out of the while loop. Bye\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a character:\n", "(Eneter X to exit)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "H\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "H\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "i\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "i\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "x\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "x\n", "Out of the while loop. Bye\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.2, Page No.108" ] }, { "cell_type": "code", "collapsed": false, "input": [ "i=65\n", "while (i<72):\n", " print \"The numeric value of \",chr(i),\" is \",i\n", " i=i+1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The numeric value of A is 65\n", "The numeric value of B is 66\n", "The numeric value of C is 67\n", "The numeric value of D is 68\n", "The numeric value of E is 69\n", "The numeric value of F is 70\n", "The numeric value of G is 71\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.3, Page No.110" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Hex(uppercase) Hex(lowercase) Decimal\"\n", "for i in range (0,16):\n", " print \"{:01X} {:01x} {:d}\".format(i,i,i)\n", " i=i+1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hex(uppercase) Hex(lowercase) Decimal\n", "0 0 0\n", "1 1 1\n", "2 2 2\n", "3 3 3\n", "4 4 4\n", "5 5 5\n", "6 6 6\n", "7 7 7\n", "8 8 8\n", "9 9 9\n", "A a 10\n", "B b 11\n", "C c 12\n", "D d 13\n", "E e 14\n", "F f 15\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.4, Page No.114" ] }, { "cell_type": "code", "collapsed": false, "input": [ "j=8\n", "for i in range(0,8):\n", " print i,\"+\",j,\"=\",i+j\n", " j=j-1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 + 8 = 8\n", "1 + 7 = 8\n", "2 + 6 = 8\n", "3 + 5 = 8\n", "4 + 4 = 8\n", "5 + 3 = 8\n", "6 + 2 = 8\n", "7 + 1 = 8\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.5, Page No.115" ] }, { "cell_type": "code", "collapsed": false, "input": [ "j=1\n", "for i in range(0,8):\n", " print j,\"-\",i,\"=\",j-i\n", " j=j+1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 - 0 = 1\n", "2 - 1 = 1\n", "3 - 2 = 1\n", "4 - 3 = 1\n", "5 - 4 = 1\n", "6 - 5 = 1\n", "7 - 6 = 1\n", "8 - 7 = 1\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.6, Page No.116" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for i in range(1,4):\n", " print \"The Start of iteration\",i,\"of the outer loop\"\n", " for j in range(1,5):\n", " print \"\\tIteration\",j,\"of the inner loop\"\n", " print \"The end of iteration\",i,\"of the outer loop\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The Start of iteration 1 of the outer loop\n", "\tIteration 1 of the inner loop\n", "\tIteration 2 of the inner loop\n", "\tIteration 3 of the inner loop\n", "\tIteration 4 of the inner loop\n", "The end of iteration 1 of the outer loop\n", "The Start of iteration 2 of the outer loop\n", "\tIteration 1 of the inner loop\n", "\tIteration 2 of the inner loop\n", "\tIteration 3 of the inner loop\n", "\tIteration 4 of the inner loop\n", "The end of iteration 2 of the outer loop\n", "The Start of iteration 3 of the outer loop\n", "\tIteration 1 of the inner loop\n", "\tIteration 2 of the inner loop\n", "\tIteration 3 of the inner loop\n", "\tIteration 4 of the inner loop\n", "The end of iteration 3 of the outer loop\n" ] } ], "prompt_number": 5 } ], "metadata": {} } ] }