{
 "metadata": {
  "name": "",
  "signature": "sha256:3856d250c0b05978b896337a2d03bfbc47b52567398ed488a9db68479c713e89"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 11 - What\u2019s the Address? Pointers"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.1, page no. 232"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "import sys\n",
      "\n",
      "iPtr = 0\n",
      "fPtr = 0.0\n",
      "cPtr = 'c'\n",
      "\n",
      "print sys.getsizeof(iPtr)\n",
      "print sys.getsizeof(fPtr)\n",
      "print sys.getsizeof(cPtr)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "24\n",
        "24\n",
        "38\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.2, page no. 233 "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "iPtr = 0\n",
      "print \"The value of iPtr is\", iPtr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The value of iPtr is 0\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.3, page no. 234"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "iPtr = None\n",
      "print \"The value of iPtr is \", iPtr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The value of iPtr is  None\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.4, page no. 235"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "num = 5\n",
      "iPtr = id(num)\n",
      "print \"The address of x using id() is \", id(num)\n",
      "print \"The address of x using iPtr is \", iPtr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The address of x using id() is  42658888\n",
        "The address of x using iPtr is  42658888\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.5, page no. 236"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "num = 5\n",
      "iPtr = id(num)\n",
      "print \"The value of num is \", num\n",
      "num = 10\n",
      "print \"The value of num after num = 10 is \", num\n",
      "iPtr = 15\n",
      "print \"The value of num after iPtr = 15 is \", num"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The value of num is  5\n",
        "The value of num after num = 10 is  10\n",
        "The value of num after iPtr = 15 is  10\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.6, page no. 238"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "num1 = 5\n",
      "num2 = 14\n",
      "iPtr = id(num1)\n",
      "print \"The value of num1 is \", num1\n",
      "iPtr = 2\n",
      "print \"The value of num1 after iPtr = 2 is \", iPtr\n",
      "iPtr = id(num2)\n",
      "print \"The value of num2 is \", num2\n",
      "iPtr /= 2\n",
      "print \"The value of num after iPtr /= 2 is \", iPtr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The value of num1 is  5\n",
        "The value of num1 after iPtr = 2 is  2\n",
        "The value of num2 is  14\n",
        "The value of num after iPtr /= 2 is  21329336\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "exmple 11.7, page no. 239"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "testScore = [4, 7, 1]\n",
      "print \"The address of the array using testScore is \", testScore\n",
      "print \"The address of the first element of the array using &testScore[0] is \", id(testScore[0])\n",
      "print \"The value of the first element of the array using *testScore is \", testScore[0]\n",
      "print \"The value of the first element of the array using testScore[0] is \", testScore[0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The address of the array using testScore is  [4, 7, 1]\n",
        "The address of the first element of the array using &testScore[0] is  42658912\n",
        "The value of the first element of the array using *testScore is  4\n",
        "The value of the first element of the array using testScore[0] is  4\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.8, page no. 240"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "MAX = 3\n",
      "\n",
      "testScore = [4, 7, 1]\n",
      "for i in range(MAX):\n",
      "    print \"The address of index \", i, \" of the array is \", id(testScore[i])\n",
      "    print \"The value at index \", i, \" of the array is \", testScore[i]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The address of index  0  of the array is  42658912\n",
        "The value at index  0  of the array is  4\n",
        "The address of index  1  of the array is  42658840\n",
        "The value at index  1  of the array is  7\n",
        "The address of index  2  of the array is  42658984\n",
        "The value at index  2  of the array is  1\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.9, page no. 241"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "MAX = 3\n",
      "\n",
      "testScore = [4, 7, 1]\n",
      "iPtr = testScore\n",
      "for i in range(MAX):\n",
      "    print \"The address of index \", i, \" of the array is \", id(iPtr[i])\n",
      "    print \"The value at index \", i, \" of the array is \", iPtr[i]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The address of index  0  of the array is  42658912\n",
        "The value at index  0  of the array is  4\n",
        "The address of index  1  of the array is  42658840\n",
        "The value at index  1  of the array is  7\n",
        "The address of index  2  of the array is  42658984\n",
        "The value at index  2  of the array is  1\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.10, page no. 241"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "MAX = 3\n",
      "\n",
      "testScore = [4, 7, 1]\n",
      "iPtr = testScore\n",
      "for i in range(MAX):\n",
      "    print \"The address of index \", i, \" of the array is \", id(iPtr[i])\n",
      "    print \"The value at index \", i, \" of the array is \", iPtr[i]\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The address of index  0  of the array is  42658912\n",
        "The value at index  0  of the array is  4\n",
        "The address of index  1  of the array is  42658840\n",
        "The value at index  1  of the array is  7\n",
        "The address of index  2  of the array is  42658984\n",
        "The value at index  2  of the array is  1\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.11, page no. 243"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "import sys\n",
      "MAX = 3\n",
      "\n",
      "testScore = [4, 7, 1]\n",
      "iPtr = testScore\n",
      "i = 0\n",
      "while (id(iPtr[MAX-1]) <= id(testScore[MAX-1])):\n",
      "    try:\n",
      "        print \"The address of index \", i, \" of the array is \", id(iPtr[i])\n",
      "        print \"The value at index \", i, \" of the array is \", iPtr[i]\n",
      "        i += 1\n",
      "    except IndexError:\n",
      "        print \"\\n\\nEnd of program\"\n",
      "        sys.exit()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "SystemExit",
       "evalue": "",
       "output_type": "pyerr",
       "traceback": [
        "An exception has occurred, use %tb to see the full traceback.\n",
        "\u001b[1;31mSystemExit\u001b[0m\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The address of index  0  of the array is  42658912\n",
        "The value at index  0  of the array is  4\n",
        "The address of index  1  of the array is  42658840\n",
        "The value at index  1  of the array is  7\n",
        "The address of index  2  of the array is  42658984\n",
        "The value at index  2  of the array is  1\n",
        "The address of index  3  of the array is  \n",
        "\n",
        "End of program\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stderr",
       "text": [
        "To exit: use 'exit', 'quit', or Ctrl-D.\n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.12, page no. 243"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "import sys\n",
      "MAX = 3\n",
      "\n",
      "testScore = [4, 7, 1]\n",
      "iPtr = id(testScore[MAX-1])\n",
      "i = MAX - 1\n",
      "while (iPtr >= id(testScore[0])):\n",
      "    print \"The address of index \", i, \" of the array is \", iPtr\n",
      "    print \"The value at index \", i, \" of the array is \", testScore[i]\n",
      "    i -= 1\n",
      "    if i < 0:\n",
      "        break"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The address of index  2  of the array is  42658984\n",
        "The value at index  2  of the array is  1\n",
        "The address of index  1  of the array is  42658984\n",
        "The value at index  1  of the array is  7\n",
        "The address of index  0  of the array is  42658984\n",
        "The value at index  0  of the array is  4\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.13, page no. 245"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "MAX = 3\n",
      "\n",
      "def assignValues(tests, num):\n",
      "    for i in range(num):\n",
      "        print \"Enter test score #\", i + 1, \": \",\n",
      "        tests.append(int(raw_input()))\n",
      "\n",
      "def displayValues(scores, elems):\n",
      "    for i in range(elems):\n",
      "        print \"Test score #\", i + 1, \": \", scores[i]\n",
      "\n",
      "testScore = []\n",
      "assignValues(testScore, MAX)\n",
      "displayValues(testScore, MAX)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter test score # 1 : "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "4\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Enter test score # 2 : "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Enter test score # 3 : "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "6\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Test score # 1 :  4\n",
        "Test score # 2 :  5\n",
        "Test score # 3 :  6\n"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.14, page no. 246"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "MAX = 3\n",
      "\n",
      "def assignValues(tests, num):\n",
      "    for i in range(num):\n",
      "        print \"Enter test score #\", i + 1, \": \",\n",
      "        tests.append(int(raw_input()))\n",
      "\n",
      "def displayValues(scores, elems):\n",
      "    for i in range(elems):\n",
      "        print \"Test score #\", i + 1, \": \", scores[i]\n",
      "\n",
      "testScore = []\n",
      "assignValues(testScore, MAX)\n",
      "displayValues(testScore, MAX)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter test score # 1 : "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "3\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Enter test score # 2 : "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "4\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Enter test score # 3 : "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Test score # 1 :  3\n",
        "Test score # 2 :  4\n",
        "Test score # 3 :  5\n"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.15, page no. 247"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "def doubleIt(x):\n",
      "    print \"The number to be doubled is \", x\n",
      "    x *= 2\n",
      "    print \"The number doubled in doubleIt is \", x\n",
      "\n",
      "\n",
      "print \"Enter number: \",\n",
      "num = int(raw_input())\n",
      "doubleIt(num)\n",
      "print \"The number doubled in main is \", num"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter number: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "4\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " The number to be doubled is  4\n",
        "The number doubled in doubleIt is  8\n",
        "The number doubled in main is  4\n"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.16, page no. 248"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "def doubleIt(x):\n",
      "    print \"The number to be doubled is \", x\n",
      "    x *= 2\n",
      "    print \"The number doubled in doubleIt is \", x\n",
      "\n",
      "\n",
      "print \"Enter number: \",\n",
      "num = int(raw_input())\n",
      "doubleIt(num)\n",
      "print \"The number doubled in main is \", num"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter number: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "4\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " The number to be doubled is  4\n",
        "The number doubled in doubleIt is  8\n",
        "The number doubled in main is  4\n"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.17, page no. 250"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "print \"Enter the number of test scores: \",\n",
      "numTests = int(raw_input())\n",
      "iPtr = []\n",
      "for i in range(numTests):\n",
      "    print \"Enter test score #\", i + 1,\": \",\n",
      "    iPtr.append(int(raw_input()))\n",
      "\n",
      "for i in range(numTests):\n",
      "    print \"Test score #\", i + 1, \" is \", iPtr[i]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the number of test scores: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "3\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Enter test score # 1 : "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Enter test score # 2 : "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "6\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Enter test score # 3 : "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "7\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Test score # 1  is  5\n",
        "Test score # 2  is  6\n",
        "Test score # 3  is  7\n"
       ]
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.18, page no. 253"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "str = \"Jeff Kent\"\n",
      "print str"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Jeff Kent\n"
       ]
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.19, page no. 253"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def setName():\n",
      "    print \"Enter your name: \",\n",
      "    name = raw_input()\n",
      "    return name\n",
      "\n",
      "str = setName()\n",
      "print str\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter your name: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Jeff\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Jeff\n"
       ]
      }
     ],
     "prompt_number": 19
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.20, page no. 254"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "def setName():\n",
      "    print \"Enter your name: \",\n",
      "    name = raw_input()\n",
      "    return name\n",
      "\n",
      "str = setName()\n",
      "print str\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter your name: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Jeff\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Jeff\n"
       ]
      }
     ],
     "prompt_number": 20
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 11.21, page no. 255"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "def setName():\n",
      "    print \"Enter your name: \",\n",
      "    name = raw_input()\n",
      "    return name\n",
      "\n",
      "str = setName()\n",
      "print str\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter your name: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Jeff\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Jeff\n"
       ]
      }
     ],
     "prompt_number": 21
    }
   ],
   "metadata": {}
  }
 ]
}