{ "metadata": { "name": "", "signature": "sha256:16010659f0ef4e7689cf0cba4bf13a7155f0a2c561ad3c2947d5e5c9a18c6603" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "CHAPTER 6: DATA TYPES REVISITED" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:199" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "ch=291\n", "if ch>255:\n", " ch=ch-256\n", "print \"\\n%d %c\\n\" % (ch,ch)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "35 #\n", "\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:199" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "for ch in range(0,256,1):\n", " print \"%d %c\\n\" % (ch,ch)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 \u0000\n", "\n", "1 \u0001\n", "\n", "2 \u0002\n", "\n", "3 \u0003\n", "\n", "4 \u0004\n", "\n", "5 \u0005\n", "\n", "6 \u0006\n", "\n", "7 \u0007\n", "\n", "8 \b\n", "\n", "9 \t\n", "\n", "10 \n", "\n", "\n", "11 \u000b", "\n", "\n", "12 \f", "\n", "\n", "13 \r\n", "\n", "14 \u000e\n", "\n", "15 \u000f\n", "\n", "16 \u0010\n", "\n", "17 \u0011\n", "\n", "18 \u0012\n", "\n", "19 \u0013\n", "\n", "20 \u0014\n", "\n", "21 \u0015\n", "\n", "22 \u0016\n", "\n", "23 \u0017\n", "\n", "24 \u0018\n", "\n", "25 \u0019\n", "\n", "26 \u001a\n", "\n", "27 \u001b\n", "\n", "28 \u001c", "\n", "\n", "29 \u001d", "\n", "\n", "30 \u001e", "\n", "\n", "31 \u001f\n", "\n", "32 \n", "\n", "33 !\n", "\n", "34 \"\n", "\n", "35 #\n", "\n", "36 $\n", "\n", "37 %\n", "\n", "38 &\n", "\n", "39 '\n", "\n", "40 (\n", "\n", "41 )\n", "\n", "42 *\n", "\n", "43 +\n", "\n", "44 ,\n", "\n", "45 -\n", "\n", "46 .\n", "\n", "47 /\n", "\n", "48 0\n", "\n", "49 1\n", "\n", "50 2\n", "\n", "51 3\n", "\n", "52 4\n", "\n", "53 5\n", "\n", "54 6\n", "\n", "55 7\n", "\n", "56 8\n", "\n", "57 9\n", "\n", "58 :\n", "\n", "59 ;\n", "\n", "60 <\n", "\n", "61 =\n", "\n", "62 >\n", "\n", "63 ?\n", "\n", "64 @\n", "\n", "65 A\n", "\n", "66 B\n", "\n", "67 C\n", "\n", "68 D\n", "\n", "69 E\n", "\n", "70 F\n", "\n", "71 G\n", "\n", "72 H\n", "\n", "73 I\n", "\n", "74 J\n", "\n", "75 K\n", "\n", "76 L\n", "\n", "77 M\n", "\n", "78 N\n", "\n", "79 O\n", "\n", "80 P\n", "\n", "81 Q\n", "\n", "82 R\n", "\n", "83 S\n", "\n", "84 T\n", "\n", "85 U\n", "\n", "86 V\n", "\n", "87 W\n", "\n", "88 X\n", "\n", "89 Y\n", "\n", "90 Z\n", "\n", "91 [\n", "\n", "92 \\\n", "\n", "93 ]\n", "\n", "94 ^\n", "\n", "95 _\n", "\n", "96 `\n", "\n", "97 a\n", "\n", "98 b\n", "\n", "99 c\n", "\n", "100 d\n", "\n", "101 e\n", "\n", "102 f\n", "\n", "103 g\n", "\n", "104 h\n", "\n", "105 i\n", "\n", "106 j\n", "\n", "107 k\n", "\n", "108 l\n", "\n", "109 m\n", "\n", "110 n\n", "\n", "111 o\n", "\n", "112 p\n", "\n", "113 q\n", "\n", "114 r\n", "\n", "115 s\n", "\n", "116 t\n", "\n", "117 u\n", "\n", "118 v\n", "\n", "119 w\n", "\n", "120 x\n", "\n", "121 y\n", "\n", "122 z\n", "\n", "123 {\n", "\n", "124 |\n", "\n", "125 }\n", "\n", "126 ~\n", "\n", "127 \u007f\n", "\n", "128 \ufffd\n", "\n", "129 \ufffd\n", "\n", "130 \ufffd\n", "\n", "131 \ufffd\n", "\n", "132 \ufffd\n", "\n", "133 \ufffd\n", "\n", "134 \ufffd\n", "\n", "135 \ufffd\n", "\n", "136 \ufffd\n", "\n", "137 \ufffd\n", "\n", "138 \ufffd\n", "\n", "139 \ufffd\n", "\n", "140 \ufffd\n", "\n", "141 \ufffd\n", "\n", "142 \ufffd\n", "\n", "143 \ufffd\n", "\n", "144 \ufffd\n", "\n", "145 \ufffd\n", "\n", "146 \ufffd\n", "\n", "147 \ufffd\n", "\n", "148 \ufffd\n", "\n", "149 \ufffd\n", "\n", "150 \ufffd\n", "\n", "151 \ufffd\n", "\n", "152 \ufffd\n", "\n", "153 \ufffd\n", "\n", "154 \ufffd\n", "\n", "155 \ufffd\n", "\n", "156 \ufffd\n", "\n", "157 \ufffd\n", "\n", "158 \ufffd\n", "\n", "159 \ufffd\n", "\n", "160 \ufffd\n", "\n", "161 \ufffd\n", "\n", "162 \ufffd\n", "\n", "163 \ufffd\n", "\n", "164 \ufffd\n", "\n", "165 \ufffd\n", "\n", "166 \ufffd\n", "\n", "167 \ufffd\n", "\n", "168 \ufffd\n", "\n", "169 \ufffd\n", "\n", "170 \ufffd\n", "\n", "171 \ufffd\n", "\n", "172 \ufffd\n", "\n", "173 \ufffd\n", "\n", "174 \ufffd\n", "\n", "175 \ufffd\n", "\n", "176 \ufffd\n", "\n", "177 \ufffd\n", "\n", "178 \ufffd\n", "\n", "179 \ufffd\n", "\n", "180 \ufffd\n", "\n", "181 \ufffd\n", "\n", "182 \ufffd\n", "\n", "183 \ufffd\n", "\n", "184 \ufffd\n", "\n", "185 \ufffd\n", "\n", "186 \ufffd\n", "\n", "187 \ufffd\n", "\n", "188 \ufffd\n", "\n", "189 \ufffd\n", "\n", "190 \ufffd\n", "\n", "191 \ufffd\n", "\n", "192 \ufffd\n", "\n", "193 \ufffd\n", "\n", "194 \ufffd\n", "\n", "195 \ufffd\n", "\n", "196 \ufffd\n", "\n", "197 \ufffd\n", "\n", "198 \ufffd\n", "\n", "199 \ufffd\n", "\n", "200 \ufffd\n", "\n", "201 \ufffd\n", "\n", "202 \ufffd\n", "\n", "203 \ufffd\n", "\n", "204 \ufffd\n", "\n", "205 \ufffd\n", "\n", "206 \ufffd\n", "\n", "207 \ufffd\n", "\n", "208 \ufffd\n", "\n", "209 \ufffd\n", "\n", "210 \ufffd\n", "\n", "211 \ufffd\n", "\n", "212 \ufffd\n", "\n", "213 \ufffd\n", "\n", "214 \ufffd\n", "\n", "215 \ufffd\n", "\n", "216 \ufffd\n", "\n", "217 \ufffd\n", "\n", "218 \ufffd\n", "\n", "219 \ufffd\n", "\n", "220 \ufffd\n", "\n", "221 \ufffd\n", "\n", "222 \ufffd\n", "\n", "223 \ufffd\n", "\n", "224 \ufffd\n", "\n", "225 \ufffd\n", "\n", "226 \ufffd\n", "\n", "227 \ufffd\n", "\n", "228 \ufffd\n", "\n", "229 \ufffd\n", "\n", "230 \ufffd\n", "\n", "231 \ufffd\n", "\n", "232 \ufffd\n", "\n", "233 \ufffd\n", "\n", "234 \ufffd\n", "\n", "235 \ufffd\n", "\n", "236 \ufffd\n", "\n", "237 \ufffd\n", "\n", "238 \ufffd\n", "\n", "239 \ufffd\n", "\n", "240 \ufffd\n", "\n", "241 \ufffd\n", "\n", "242 \ufffd\n", "\n", "243 \ufffd\n", "\n", "244 \ufffd\n", "\n", "245 \ufffd\n", "\n", "246 \ufffd\n", "\n", "247 \ufffd\n", "\n", "248 \ufffd\n", "\n", "249 \ufffd\n", "\n", "250 \ufffd\n", "\n", "251 \ufffd\n", "\n", "252 \ufffd\n", "\n", "253 \ufffd\n", "\n", "254 \ufffd\n", "\n", "255 \ufffd\n", "\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:200" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "for ch in range(0,255,1):\n", " print \"%d %c\\n\" % (ch,ch)\n", "print \"%d %c\\n\" % (ch,ch)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 \u0000\n", "\n", "1 \u0001\n", "\n", "2 \u0002\n", "\n", "3 \u0003\n", "\n", "4 \u0004\n", "\n", "5 \u0005\n", "\n", "6 \u0006\n", "\n", "7 \u0007\n", "\n", "8 \b\n", "\n", "9 \t\n", "\n", "10 \n", "\n", "\n", "11 \u000b", "\n", "\n", "12 \f", "\n", "\n", "13 \r\n", "\n", "14 \u000e\n", "\n", "15 \u000f\n", "\n", "16 \u0010\n", "\n", "17 \u0011\n", "\n", "18 \u0012\n", "\n", "19 \u0013\n", "\n", "20 \u0014\n", "\n", "21 \u0015\n", "\n", "22 \u0016\n", "\n", "23 \u0017\n", "\n", "24 \u0018\n", "\n", "25 \u0019\n", "\n", "26 \u001a\n", "\n", "27 \u001b\n", "\n", "28 \u001c", "\n", "\n", "29 \u001d", "\n", "\n", "30 \u001e", "\n", "\n", "31 \u001f\n", "\n", "32 \n", "\n", "33 !\n", "\n", "34 \"\n", "\n", "35 #\n", "\n", "36 $\n", "\n", "37 %\n", "\n", "38 &\n", "\n", "39 '\n", "\n", "40 (\n", "\n", "41 )\n", "\n", "42 *\n", "\n", "43 +\n", "\n", "44 ,\n", "\n", "45 -\n", "\n", "46 .\n", "\n", "47 /\n", "\n", "48 0\n", "\n", "49 1\n", "\n", "50 2\n", "\n", "51 3\n", "\n", "52 4\n", "\n", "53 5\n", "\n", "54 6\n", "\n", "55 7\n", "\n", "56 8\n", "\n", "57 9\n", "\n", "58 :\n", "\n", "59 ;\n", "\n", "60 <\n", "\n", "61 =\n", "\n", "62 >\n", "\n", "63 ?\n", "\n", "64 @\n", "\n", "65 A\n", "\n", "66 B\n", "\n", "67 C\n", "\n", "68 D\n", "\n", "69 E\n", "\n", "70 F\n", "\n", "71 G\n", "\n", "72 H\n", "\n", "73 I\n", "\n", "74 J\n", "\n", "75 K\n", "\n", "76 L\n", "\n", "77 M\n", "\n", "78 N\n", "\n", "79 O\n", "\n", "80 P\n", "\n", "81 Q\n", "\n", "82 R\n", "\n", "83 S\n", "\n", "84 T\n", "\n", "85 U\n", "\n", "86 V\n", "\n", "87 W\n", "\n", "88 X\n", "\n", "89 Y\n", "\n", "90 Z\n", "\n", "91 [\n", "\n", "92 \\\n", "\n", "93 ]\n", "\n", "94 ^\n", "\n", "95 _\n", "\n", "96 `\n", "\n", "97 a\n", "\n", "98 b\n", "\n", "99 c\n", "\n", "100 d\n", "\n", "101 e\n", "\n", "102 f\n", "\n", "103 g\n", "\n", "104 h\n", "\n", "105 i\n", "\n", "106 j\n", "\n", "107 k\n", "\n", "108 l\n", "\n", "109 m\n", "\n", "110 n\n", "\n", "111 o\n", "\n", "112 p\n", "\n", "113 q\n", "\n", "114 r\n", "\n", "115 s\n", "\n", "116 t\n", "\n", "117 u\n", "\n", "118 v\n", "\n", "119 w\n", "\n", "120 x\n", "\n", "121 y\n", "\n", "122 z\n", "\n", "123 {\n", "\n", "124 |\n", "\n", "125 }\n", "\n", "126 ~\n", "\n", "127 \u007f\n", "\n", "128 \ufffd\n", "\n", "129 \ufffd\n", "\n", "130 \ufffd\n", "\n", "131 \ufffd\n", "\n", "132 \ufffd\n", "\n", "133 \ufffd\n", "\n", "134 \ufffd\n", "\n", "135 \ufffd\n", "\n", "136 \ufffd\n", "\n", "137 \ufffd\n", "\n", "138 \ufffd\n", "\n", "139 \ufffd\n", "\n", "140 \ufffd\n", "\n", "141 \ufffd\n", "\n", "142 \ufffd\n", "\n", "143 \ufffd\n", "\n", "144 \ufffd\n", "\n", "145 \ufffd\n", "\n", "146 \ufffd\n", "\n", "147 \ufffd\n", "\n", "148 \ufffd\n", "\n", "149 \ufffd\n", "\n", "150 \ufffd\n", "\n", "151 \ufffd\n", "\n", "152 \ufffd\n", "\n", "153 \ufffd\n", "\n", "154 \ufffd\n", "\n", "155 \ufffd\n", "\n", "156 \ufffd\n", "\n", "157 \ufffd\n", "\n", "158 \ufffd\n", "\n", "159 \ufffd\n", "\n", "160 \ufffd\n", "\n", "161 \ufffd\n", "\n", "162 \ufffd\n", "\n", "163 \ufffd\n", "\n", "164 \ufffd\n", "\n", "165 \ufffd\n", "\n", "166 \ufffd\n", "\n", "167 \ufffd\n", "\n", "168 \ufffd\n", "\n", "169 \ufffd\n", "\n", "170 \ufffd\n", "\n", "171 \ufffd\n", "\n", "172 \ufffd\n", "\n", "173 \ufffd\n", "\n", "174 \ufffd\n", "\n", "175 \ufffd\n", "\n", "176 \ufffd\n", "\n", "177 \ufffd\n", "\n", "178 \ufffd\n", "\n", "179 \ufffd\n", "\n", "180 \ufffd\n", "\n", "181 \ufffd\n", "\n", "182 \ufffd\n", "\n", "183 \ufffd\n", "\n", "184 \ufffd\n", "\n", "185 \ufffd\n", "\n", "186 \ufffd\n", "\n", "187 \ufffd\n", "\n", "188 \ufffd\n", "\n", "189 \ufffd\n", "\n", "190 \ufffd\n", "\n", "191 \ufffd\n", "\n", "192 \ufffd\n", "\n", "193 \ufffd\n", "\n", "194 \ufffd\n", "\n", "195 \ufffd\n", "\n", "196 \ufffd\n", "\n", "197 \ufffd\n", "\n", "198 \ufffd\n", "\n", "199 \ufffd\n", "\n", "200 \ufffd\n", "\n", "201 \ufffd\n", "\n", "202 \ufffd\n", "\n", "203 \ufffd\n", "\n", "204 \ufffd\n", "\n", "205 \ufffd\n", "\n", "206 \ufffd\n", "\n", "207 \ufffd\n", "\n", "208 \ufffd\n", "\n", "209 \ufffd\n", "\n", "210 \ufffd\n", "\n", "211 \ufffd\n", "\n", "212 \ufffd\n", "\n", "213 \ufffd\n", "\n", "214 \ufffd\n", "\n", "215 \ufffd\n", "\n", "216 \ufffd\n", "\n", "217 \ufffd\n", "\n", "218 \ufffd\n", "\n", "219 \ufffd\n", "\n", "220 \ufffd\n", "\n", "221 \ufffd\n", "\n", "222 \ufffd\n", "\n", "223 \ufffd\n", "\n", "224 \ufffd\n", "\n", "225 \ufffd\n", "\n", "226 \ufffd\n", "\n", "227 \ufffd\n", "\n", "228 \ufffd\n", "\n", "229 \ufffd\n", "\n", "230 \ufffd\n", "\n", "231 \ufffd\n", "\n", "232 \ufffd\n", "\n", "233 \ufffd\n", "\n", "234 \ufffd\n", "\n", "235 \ufffd\n", "\n", "236 \ufffd\n", "\n", "237 \ufffd\n", "\n", "238 \ufffd\n", "\n", "239 \ufffd\n", "\n", "240 \ufffd\n", "\n", "241 \ufffd\n", "\n", "242 \ufffd\n", "\n", "243 \ufffd\n", "\n", "244 \ufffd\n", "\n", "245 \ufffd\n", "\n", "246 \ufffd\n", "\n", "247 \ufffd\n", "\n", "248 \ufffd\n", "\n", "249 \ufffd\n", "\n", "250 \ufffd\n", "\n", "251 \ufffd\n", "\n", "252 \ufffd\n", "\n", "253 \ufffd\n", "\n", "254 \ufffd\n", "\n", "254 \ufffd\n", "\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:200-201" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", " \n", "c=raw_input()\n", "d=raw_input()\n", "print \"%c %c\\n\" % (c,d)\n", "#int\n", "i=eval(raw_input())\n", "j=eval(raw_input())\n", "print \"%d %u\\n\" % (i,j)\n", "#short int\n", "k=eval(raw_input())\n", "l=eval(raw_input())\n", "print \"%d %u\\n\" % (k,l)\n", "#long int\n", "m=eval(raw_input())\n", "n=eval(raw_input())\n", "print \"%ld %lu\\n\" % (m,n)\n", "#float,double,long double\n", "x=eval(raw_input())\n", "y=eval(raw_input())\n", "z=eval(raw_input())\n", "print \"%f %lf %Lf\\n\" % (x,y,z)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "a\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "b\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "a b\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "10000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "7389\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "10000 7389\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "34\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "585\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "34 585\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "34676\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "500000\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "34676 500000\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "445.55\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "57846.44\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "55666885.6655\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "445.550000 57846.440000 55666885.665500\n", "\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:205" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def main():\n", " i=1\n", " def main2():\n", " def main3():\n", " print \"%d\" % (i) #prints 1\n", " main3()\n", " print \"%d\" % (i) #prints 1\n", " main2()\n", " print \"%d\\n\" % (i) #prints 1\n", "main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "1\n", "1\n", "\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:205" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def main():\n", " i=1\n", " def main2():\n", " i=2\n", " def main3():\n", " i=3\n", " print \"%d\" % (i) #prints 3\n", " main3()\n", " print \"%d\" % (i) #prints 2\n", " main2()\n", " print \"%d\\n\" % (i) #prints 1\n", "main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3\n", "2\n", "1\n", "\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:206" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "for i in range(1,11,1):\n", " print \"%d\\n\" % (i)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "\n", "2\n", "\n", "3\n", "\n", "4\n", "\n", "5\n", "\n", "6\n", "\n", "7\n", "\n", "8\n", "\n", "9\n", "\n", "10\n", "\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:208" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def increment():\n", " i=1\n", " print \"%d\\n\" % (i) #prints 1 every time\n", " i=i+1\n", "increment()\n", "increment()\n", "increment()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "\n", "1\n", "\n", "1\n", "\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:208" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def static_var(varname, value): #function to make it static\n", " def decorate(func):\n", " setattr(func, varname, value)\n", " return func\n", " return decorate\n", "@static_var(\"i\", 1) #i is static variable\n", "def increment():\n", " print \"%d\\n\" % increment.i\n", " increment.i += 1\n", "increment()\n", "increment()\n", "increment()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "\n", "2\n", "\n", "3\n", "\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:209" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def fun():\n", " k=35\n", " return id(k) #returns address of k\n", "j=fun() #stores address of k\n", "print \"%d\\n\" % (j) #prints address of k" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "19639600\n", "\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:210" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def increment():\n", " global i #gloabal declaration\n", " i=i+1\n", " print \"on incrementing i=%d\\n\" % (i)\n", "def decrement():\n", " global i #global declaration\n", " i=i-1\n", " print \"on decrementing i=%d\\n\" % (i)\n", "i=0\n", "print \"\\ni=%d\" % (i)\n", "increment()\n", "increment()\n", "decrement()\n", "decrement()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "i=0\n", "on incrementing i=1\n", "\n", "on incrementing i=2\n", "\n", "on decrementing i=1\n", "\n", "on decrementing i=0\n", "\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:211" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "x=21\n", "def main():\n", " global x,y\n", " print \"%d %d\\n\" % (x,y)\n", "y=31\n", "main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "21 31\n", "\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:211" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def display():\n", " global x #global variable\n", " print \"%d\\n\" % (x) #prints 10\n", "x=10\n", "def main():\n", " x=20 #local variable for main\n", " print \"%d\\n\" % (x) #prints 20\n", " display()\n", "main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "20\n", "\n", "10\n", "\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:212-213" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def fun1():\n", " global i\n", " i+=1\n", " print \"%d\\n\" % (i)\n", "def fun2():\n", " global i\n", " i-=1\n", " print \"%d\\n\" % (i)\n", "i=35\n", "print \"%d\\n\" % (i)\n", "fun1()\n", "fun2()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "35\n", "\n", "36\n", "\n", "35\n", "\n" ] } ], "prompt_number": 13 } ], "metadata": {} } ] }