{ "metadata": { "name": "", "signature": "sha256:2b98cee86e6322a41a7dca7a85df32b68e0995a4fde2b6249b408a952c9387e4" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 6: Scope & Extent" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example scope1.c, page no. 202" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#as mentioned in the textbook, the statement j = 30 will not give an error as in Python you can declare a vairable anywhere\n", "#however, the statement does not refer to the variable j of the function.\n", "def func():\n", " j = 20\n", " print \"j is a variable in the function\"\n", " print \"Value of j is \",j\n", "i = 10\n", "print \"i is a variable in main.\"\n", "print \"Value of i is \", i\n", "print \"Calling func...\"\n", "func()\n", "j = 30" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "i is a variable in main.\n", "Value of i is 10\n", "Calling func...\n", "j is a variable in the function\n", "Value of j is 20\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example global.c, page no. 202" ] }, { "cell_type": "code", "collapsed": false, "input": [ "g = 0\n", "def func():\n", " global g\n", " print \"In func. g is visbile here, since it is global\"\n", " print \"Incrementing g in func...\"\n", " g += 1\n", " return g\n", "print \"In main. g is visible here, since it is global\"\n", "print \"assigning 20 to g in main...\"\n", "g = 20\n", "print \"Callint func...\"\n", "func()\n", "print \"func returned g is \", g" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "In main. g is visible here, since it is global\n", "assigning 20 to g in main...\n", "Callint func...\n", "In func. g is visbile here, since it is global\n", "Incrementing g in func...\n", "func returned g is 21\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example colcolon.c, page no. 203" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#there is no concept of scope resolution operator in Python. we will do it in different way\n", "a = 10\n", "def func():\n", " global a\n", " b = 20\n", " print b\n", " print a\n", "func()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "20\n", "10\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.1, page no. 204" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#there is no concept of block in Python. writing if True is smiliar to it but still, output will differ\n", "j = 144\n", "print \"The value of j outside the block is \", j\n", "if True:\n", " j = 12\n", " print \"The value of j inside the block is \", j\n", "print \"The value of j outside the block is \", j" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The value of j outside the block is 144\n", "The value of j inside the block is 12\n", "The value of j outside the block is 12\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.2, page no. 205" ] }, { "cell_type": "code", "collapsed": false, "input": [ "i = 144\n", "j = 132\n", "print \"i = \", i\n", "if True:\n", " k = 12\n", " k = int(raw_input())\n", " i = i%k\n", "if i == 0:\n", " print \"i is a divisor of %d \" %k" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "i = 144\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "25\n" ] } ], "prompt_number": 14 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.3, page no. 205" ] }, { "cell_type": "code", "collapsed": false, "input": [ "iNum = 5\n", "jNum = 30\n", "if True:\n", " iNum = 10\n", " print \"%d %d\" %(iNum, jNum)\n", "print \"%d %d\" %(iNum, jNum)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10 30\n", "10 30\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7, page no. 207" ] }, { "cell_type": "code", "collapsed": false, "input": [ "cName = raw_input(\"Enter a string: \")\n", "print \"The reverse of the string is: \"\n", "print cName[::-1]" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a string: this is a string\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The reverse of the string is: \n", "gnirts a si siht\n" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.4 pageno : 206" ] }, { "cell_type": "code", "collapsed": false, "input": [ "iGlobal = 35\n", "Number = 47\n", "iVal = 99\n", "Number = 1000\n", "print \"The value of local variable Number is %d\"%Number\n", "print \"The value of global variable iGlobal is %d\"%iGlobal\n", "print \"The value of global variable Number is %d\"%Number\n", "print \"The value of static global variable iVal is %d\"%iVal\n", "\n", "# Python has no static functionality. and also local variable changes value when value assigned to it." ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The value of local variable Number is 1000\n", "The value of global variable iGlobal is 35\n", "The value of global variable Number is 1000\n", "The value of static global variable iVal is 99\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.5, page no. 208" ] }, { "cell_type": "code", "collapsed": false, "input": [ "Count = 1\n", "def PrintCount():\n", " global Count\n", " print \"Count = %d\" %Count\n", " Count = Count + 1\n", "PrintCount()\n", "PrintCount()\n", "PrintCount()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Count = 1\n", "Count = 2\n", "Count = 3\n" ] } ], "prompt_number": 19 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.6, page no.208" ] }, { "cell_type": "code", "collapsed": false, "input": [ "uF1 = 0\n", "uF2 = 1\n", "def PrintNextFibo():\n", " global uF1,uF2\n", " uFpre = uF1 + uF2\n", " print \"%d\" %uFpre ,\n", " uF2 = uF1\n", " uF1 = uFpre\n", "print \"Fibonacci sequences using static variables :\"\n", "print \"Enter how many number are to be generated :\",\n", "itotNum = int(raw_input())\n", "print \"The first %d fibonacci numbers are\" %itotNum\n", "for iIndex in range(1,itotNum+1):\n", " PrintNextFibo()\n", "\n", "# it starts with zero but here function created called uses global variable so we need to print/assume manually zero value." ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Fibonacci sequences using static variables :\n", "Enter how many number are to be generated :" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The first 5 fibonacci numbers are\n", "1 1 2 3 5\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example on page no. 209" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import linsrch\n", "import bubble\n", "arr = []\n", "num = 5\n", "i = 0\n", "pos = 1\n", "print \"Enter five elements\"\n", "while i < num:\n", " arr.append(int(raw_input()))\n", " i+=1\n", "print \"Enter element to be searched: \",\n", "key = int(raw_input())\n", "pos = linsrch.LinSrch(arr, num, key)\n", "if pos:\n", " print \"Element %d found at %d position\" %(key, pos)\n", "else:\n", " print \"Ellement %d not found\" %key\n", "bubble.BubSort(arr, num)\n", "print \"The sorted element are\"\n", "for ele in arr:\n", " print ele," ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter five elements\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "12\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "15\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "56\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "48\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "22\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter element to be searched: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "22\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Element 22 found at 5 position\n", "The sorted element are\n", "12 15 22 48 56\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example on page no. 211" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import myglobal\n", "globalVar = 10\n", "print \"globalVar before calling = \", globalVar\n", "myglobal.inc_global(globalVar)\n", "print \"globalVar after calling \", globalVar" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "globalVar before calling = 10\n", "globalVar after calling 10\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example on page no. 212" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import myglobal\n", "\n", "globalVar = 10\n", "print \"globalVar before calling = \", globalVar\n", "g = myglobal.inc_global(globalVar)\n", "print \"globalVar after calling \", g" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "globalVar before calling = 10\n", "globalVar after calling 11\n" ] } ], "prompt_number": 7 } ], "metadata": {} } ] }