{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 11: Storage Class

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 11.1, Page number: 376

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Working of auto variable\n", "\n", "import sys\n", "\n", "#Functio definitions\n", "def call1():\n", " v = 20\n", " sys.stdout.write(\"\\nV = %d\"%(v))\n", " \n", "def call2():\n", " v = 30\n", " call1()\n", " sys.stdout.write(\"\\nV = %d\"%(v))\n", " \n", "#Variable Initialization\n", "v = 10\n", "\n", "#Function call\n", "call2()\n", "\n", "#Result\n", "sys.stdout.write(\"\\nV = %d\"%(v))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "V = 20\n", "V = 30\n", "V = 10" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 11.2, Page number: 376

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Working of auto variables in different blocks\n", "\n", "import sys\n", "\n", "#Variable Initialization\n", "x = 10\n", "\n", "sys.stdout.write(\"\\nX = %d\"%(x))\n", "\n", "x = 20\n", "sys.stdout.write(\"\\nX = %d\"%(x))\n", "\n", "x = 10\n", "sys.stdout.write(\"\\nX = %d\"%(x))\n", "\n", "#In python, block of statements are indicated using intendation.\n", "#block of statements can be written for conditional,loop or function statements" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "X = 10\n", "X = 20\n", "X = 10" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 11.3, Page number: 377

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Use same variable in different blocks with different data types\n", "\n", "import sys\n", "\n", "#Variable Initialization & Result\n", "x = 10\n", "sys.stdout.write(\"\\nX = %d\"%(x))\n", "\n", "x = 2.22\n", "sys.stdout.write(\"\\nX = %g\"%(x))\n", "\n", "x = \"Auto Storage Class\"\n", "sys.stdout.write(\"\\nX = %s\"%(x))\n", "\n", "x = 10\n", "sys.stdout.write(\"\\nX = %d\"%(x))\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "X = 10\n", "X = 2.22\n", "X = Auto Storage Class\n", "X = 10" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 11.4, Page number: 378

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Working of external variables\n", "\n", "import sys\n", "\n", "#Function definitions\n", "def call1():\n", " sys.stdout.write(\"\\nIn Call1() V = %d\"%(v))\n", " \n", "def call2():\n", " sys.stdout.write(\"\\nIn call2() V = %d\"%(v))\n", " \n", "#Variable Initialization\n", "global v\n", "v = 10\n", "\n", "call1()\n", "call2()\n", "sys.stdout.write(\"\\nIn main() V = %d\"%(v))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "In Call1() V = 10\n", "In call2() V = 10\n", "In main() V = 10" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 11.5, Page number: 379

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Working of auto and global variables with same name\n", "\n", "import sys\n", "\n", "#Function definitions\n", "def call1():\n", " v = 20\n", " sys.stdout.write(\"\\nIn Call1() V = %d\"%(v))\n", " \n", "def call2():\n", " sys.stdout.write(\"\\nIn call2() V = %d\"%(v))\n", " \n", "#Variable Initialization\n", "global v\n", "v = 10\n", "\n", "#Function call\n", "call1()\n", "call2()\n", "\n", "#Result\n", "sys.stdout.write(\"\\nIn main() V = %d\"%(v))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "In Call1() V = 20\n", "In call2() V = 10\n", "In main() V = 10" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 11.6, Page number: 380

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Declare external variables using extern keyword\n", "\n", "#Variable Initialization\n", "global m\n", "m = 10\n", "\n", "#There is no extern keyword in python, global is used instead\n", "sys.stdout.write(\"\\nM = %d\"%(m))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "M = 10" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 11.7, Page number: 380

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Use of static variable\n", "\n", "import sys\n", "\n", "#Function definition\n", "def print1(m):\n", " sys.stdout.write(\"\\nm = %d\"%(m))\n", " if m == 3:\n", " return\n", " \n", "m = 0\n", "\n", "for i in range(0,3):\n", " m += 1\n", " print1(m)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "m = 1\n", "m = 2\n", "m = 3" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 11.8, Page number: 381

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Difference between variables of auto and static class\n", "\n", "import sys\n", "\n", "#Result\n", "y = 0\n", "sys.stdout.write(\"\\nx = %d & y = %d\"%(x,y))\n", "\n", "#Since x display the junk value which stored already, it differs in different systems" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "x = 10 & y = 0" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 11.9, Page number: 382

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Register class variable\n", "\n", "import sys\n", "\n", "#There is no register keyword in python\n", "m = 1\n", "\n", "#Result\n", "for m in range(1,5+1):\n", " sys.stdout.write(\"\\t%d\"%(m))\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\t1\t2\t3\t4\t5" ] } ], "prompt_number": 18 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 11.10, Page number: 382

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Register class variable\n", "\n", "import sys\n", "\n", "#There is no register class variable in python\n", "m = 1\n", "\n", "for m in range(1,6):\n", " sys.stdout.write(\"\\t%d\"%(m))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\t1\t2\t3\t4\t5" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }