{ "metadata": { "name": "", "signature": "sha256:dc372127172fa28ae076732c5c6607c2a4f0be3eed0d850c34c59df8b47971c6" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Hour 14: Uderstanding Scope and Storage Classes" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 14.1, Page No 225" ] }, { "cell_type": "code", "collapsed": false, "input": [ "i=32\n", "print \"Within the outer block: i=%d\" % i\n", "#There are no direct blocks in Python. We will do it in different way\n", "print \"Within the inner block\"\n", "def fu():\n", " j = 10\n", " for i in range(11):\n", " print \"i=\", i, \"j=\",j\n", " j -= 1\n", "fu()\n", "print \"Within the outer block: i=%d\" % i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Within the outer block: i=32\n", "Within the inner block\n", "i= 0 j= 10\n", "i= 1 j= 9\n", "i= 2 j= 8\n", "i= 3 j= 7\n", "i= 4 j= 6\n", "i= 5 j= 5\n", "i= 6 j= 4\n", "i= 7 j= 3\n", "i= 8 j= 2\n", "i= 9 j= 1\n", "i= 10 j= 0\n", "Within the outer block: i=32\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 14.2, Page No 227" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Python does'nt provide block creating facility like using just {} with out any name hence i have implemented in this way\n", "\n", "def function_1():\n", " x = 1234\n", " y = 1.234567\n", " print \"From fuction_1:\"\n", " print \"x=\", x, \", y=\", y\n", "\n", "x = 4321\n", "function_1()\n", "print \"Within the main block:\" \n", "print \"x=%d, y=%f\" %(x, y)\n", "\n", "y = 7.654321\n", "function_1()\n", "print \"Within the nested block:\"\n", "print \"x=%d, y=%f\" %(x, y)\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "From fuction_1:\n", "x= 1234 , y= 1.234567\n", "Within the main block:\n", "x=4321, y=7.654321\n", "From fuction_1:\n", "x= 1234 , y= 1.234567\n", "Within the nested block:\n", "x=4321, y=7.654321\n" ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 14.3, Page No 230" ] }, { "cell_type": "code", "collapsed": false, "input": [ "counter =0\n", "def add_two(x,y):\n", " global counter\n", " counter = counter+1\n", " print \"This is the function call of %d,\" % counter\n", " return (x+y)\n", "j = 5\n", "for i in range(5):\n", " print \"The addition of \"+ str(i) +\" and \"+str(j)+\" is \"+str(add_two(i,j))\n", " j=j-1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "This is the function call of 1,\n", "The addition of 0 and 5 is 5\n", "This is the function call of 2,\n", "The addition of 1 and 4 is 5\n", "This is the function call of 3,\n", "The addition of 2 and 3 is 5\n", "This is the function call of 4,\n", "The addition of 3 and 2 is 5\n", "This is the function call of 5,\n", "The addition of 4 and 1 is 5\n" ] } ], "prompt_number": 11 } ], "metadata": {} } ] }