{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h1>Chapter 9: Pointers<h1>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.1, Page number: 282<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Display the address of the variable\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "num = int(raw_input(\"Enter a Number = \"))\n",
      "\n",
      "#Result\n",
      "sys.stdout.write(\"Value of num = %d\\n\"%(num))\n",
      "sys.stdout.write(\"Address of num = %d\\n\"%(id(num)))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a Number = 20\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Value of num = 20\n",
        "Address of num = 19554932\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.2, Page number: 283<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Display the addresses of different variables together with their values.\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "c = raw_input(\"Enter alphabet\")\n",
      "i = int(raw_input(\"Enter number\"))\n",
      "f = float(raw_input(\"Enter float\"))\n",
      "\n",
      "#Result\n",
      "sys.stdout.write(\"\\nAddress of (c) %c = %d\"%(c,id(c)))\n",
      "sys.stdout.write(\"\\nAddress of (i) %d = %d\"%(i,id(i)))\n",
      "sys.stdout.write(\"\\nAddress of (f) %.2f = %d\"%(f,id(f)))\n",
      "#Memory address differs in different systems\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter alphabetC\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter number20\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter float2.5\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Address of (c) C = 48769192\n",
        "Address of (i) 20 = 19554932\n",
        "Address of (f) 2.50 = 44777808"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.3, Page number: 284<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Show pointer of any data type that occupies same space\n",
      "\n",
      "import sys\n",
      "\n",
      "#Result\n",
      "#Garbage collector module is used for python memory management\n",
      "sys.stdout.write(\"char %d byte & its pointer %d bytes\\n\"%(sys.getsizeof(chr),sys.getsizeof(id(chr))))\n",
      "sys.stdout.write(\"int %d byte & its pointer %d bytes\\n\"%(sys.getsizeof(int),sys.getsizeof(id(int))))\n",
      "sys.stdout.write(\"float %d byte & its pointer %d bytes\\n\"%(sys.getsizeof(float),sys.getsizeof(id(float))))\n",
      "\n",
      "#value tagged method is used in python"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "char 36 byte & its pointer 12 bytes\n",
        "int 436 byte & its pointer 12 bytes\n",
        "float 436 byte & its pointer 12 bytes\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.4, Page number: 284<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Display the value of variable and its location using pointer\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "#There is no pointer concept in python\n",
      "v = 10\n",
      "p = v\n",
      "#In python, variables of same value are tagged together instead of storing the value in different locations\n",
      "#Result\n",
      "sys.stdout.write(\"\\nAddress of v = %u\"%(id(v)))\n",
      "sys.stdout.write(\"\\nValue of v = %d\"%(p))\n",
      "sys.stdout.write(\"\\nAddress of p = %d\"%(id(p)))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Address of v = 29808764\n",
        "Value of v = 10\n",
        "Address of p = 29808764"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.5, Page number: 285<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Print the value of variable by different ways.\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "a = int(raw_input(\"Enter Integer Value\"))\n",
      "b = float(raw_input(\"Enter float Value\"))\n",
      "\n",
      "pa = id(a)\n",
      "pb = id(b)\n",
      "\n",
      "#Result\n",
      "#There is no pointer concept in python. content of memory location can't be accessed directly by user.\n",
      "sys.stdout.write(\"\\nAddress of a = %u\"%(pa))\n",
      "sys.stdout.write(\"\\nValue of a = %d\"%(a))\n",
      "sys.stdout.write(\"\\nValue of a = %d\"%(a))\n",
      "sys.stdout.write(\"\\nValue of a = %d\"%(a))\n",
      "\n",
      "sys.stdout.write(\"\\nAddress of b = %u\"%(pb))\n",
      "sys.stdout.write(\"\\nValue of b = %.2f\"%(b))\n",
      "sys.stdout.write(\"\\nValue of b = %.2f\"%(b))\n",
      "sys.stdout.write(\"\\nValue of b = %.2f\"%(b))\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Integer Value4\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter float Value2.2\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Address of a = 19555124\n",
        "Value of a = 4\n",
        "Value of a = 4\n",
        "Value of a = 4\n",
        "Address of b = 44777792\n",
        "Value of b = 2.20\n",
        "Value of b = 2.20\n",
        "Value of b = 2.20"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.6, Page number: 286<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Print value of variable using different pointer notations.\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "v = 10\n",
      "p = id(v)\n",
      "\n",
      "#Result\n",
      "#There is no pointer concept in python and can't access memory location directly by user\n",
      "sys.stdout.write(\"\\nv = %d v = %d v = %d\"%(v,v,v))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "v = 10 v = 10 v = 10"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.7, Page number: 287<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Print element and its address using pointer\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "i = int(raw_input(\"Enter a number : \"))\n",
      "k = id(i)\n",
      "\n",
      "#Result\n",
      "#There is no pointer concept in python and can't access memory location directly by user\n",
      "sys.stdout.write(\"\\nAddress of i is %u\"%(k))\n",
      "sys.stdout.write(\"\\nValue of i is %d\"%(i))\n",
      "#The memory address differs in different systems\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a number : 15\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Address of i is 19554992\n",
        "Value of i is 15"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.8, Page number: 287<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Add two numbers through variables and their pointers\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "a = int(raw_input(\"Enter Two Numbers : \"))\n",
      "b = int(raw_input(\"Enter Two Numbers : \"))\n",
      "ap = a\n",
      "bp = b\n",
      "\n",
      "#Calculation\n",
      "c = a + b\n",
      "d = ap + bp\n",
      "\n",
      "#Result\n",
      "#There is no pointer concept in python and can't access memory location directly by user\n",
      "sys.stdout.write(\"\\nSum of A & B Using Variable : %d\"%(c))\n",
      "sys.stdout.write(\"\\nSum of A & B Using Pointer : %d\"%(d))\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Two Numbers : 8\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Two Numbers : 4\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Sum of A & B Using Variable : 12\n",
        "Sum of A & B Using Pointer : 12"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.9, Page number: 288<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Assign pointer value to another variable\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "a = 5\n",
      "c = id(a)\n",
      "b = a\n",
      "\n",
      "#Result\n",
      "#There is no pointer concept in python and can't access memory location directly by user\n",
      "sys.stdout.write(\"\\nMemory location of 'a' = %u\"%(c))\n",
      "sys.stdout.write(\"\\nThe value of a = %d & b = %d\"%(a,b))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Memory location of 'a' = 29808824\n",
        "The value of a = 5 & b = 5"
       ]
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.10, Page number: 289<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Assign value of 'b' to 'a' through pointers and add the values.\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "a = int(raw_input(\"Enter Value of 'a' & 'b' : \"))\n",
      "b = int(raw_input(\"Enter Value of 'a' & 'b' : \"))\n",
      "pa = id(a)\n",
      "pb = id(b)\n",
      "\n",
      "#Result\n",
      "#There is no pointer concept in python and can't access memory location directly by user\n",
      "sys.stdout.write(\"\\nValue of a = %d & b = %d\"%(a,b))\n",
      "sys.stdout.write(\"\\nAddress of a = %u\"%(pa))\n",
      "sys.stdout.write(\"\\nAddress of b = %u\"%(pb))\n",
      "sys.stdout.write(\"\\n\\nAddition of 'a' & 'b' : %d\"%(a+b))\n",
      "\n",
      "a = b\n",
      "pa = id(a)\n",
      "#In python, variables of same value are tagged together instead of storing the value in different locations\n",
      "\n",
      "sys.stdout.write(\"\\n*pa = %d *pb = %d\"%(a,b))\n",
      "sys.stdout.write(\"\\npa = %u pb = %u\"%(pa,pb))\n",
      "sys.stdout.write(\"\\nAddition of *pa + *pb : %d\"%(a+b))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Value of 'a' & 'b' : 8\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Value of 'a' & 'b' : 4\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Value of a = 8 & b = 4\n",
        "Address of a = 19555076\n",
        "Address of b = 19555124\n",
        "\n",
        "Addition of 'a' & 'b' : 12\n",
        "*pa = 4 *pb = 4\n",
        "pa = 19555124 pb = 19555124\n",
        "Addition of *pa + *pb : 8"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.11, Page number: 290<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#The effect of increment on pointer variables. \n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "x = int(raw_input(\"Enter integer Value\"))\n",
      "y = raw_input(\"Enter Character Value\")\n",
      "z = float(raw_input(\"Enter float Value\"))\n",
      "\n",
      "x1 = id(x)\n",
      "y1 = id(y)\n",
      "z1 = id(z)\n",
      "\n",
      "#Result\n",
      "#There is no pointer concept in python and can't access memory location directly by user\n",
      "\n",
      "sys.stdout.write(\"\\nAddress of x = %u\\n\"%(x1))\n",
      "sys.stdout.write(\"\\nAddress of y = %u\\n\"%(y1))\n",
      "sys.stdout.write(\"\\nAddress of z = %u\\n\"%(z1))\n",
      "\n",
      "x1 += 1\n",
      "y1 += 1\n",
      "z1 += 1\n",
      "#In python, variables of same value are tagged together instead of storing the value in different locations\n",
      "\n",
      "sys.stdout.write(\"\\nAfter Increment in Pointers\\n\")\n",
      "sys.stdout.write(\"\\nNow Address of x = %u\\n\"%(x1))\n",
      "sys.stdout.write(\"Now Address of y = %u\\n\"%(y1))\n",
      "sys.stdout.write(\"Now Address of z = %u\\n\"%(z1))\n",
      "\n",
      "sys.stdout.write(\"\\nSize of Integer : %d\"%(sys.getsizeof(x)))\n",
      "sys.stdout.write(\"\\nSize of Character :  %d\"%(sys.getsizeof(y)))\n",
      "sys.stdout.write(\"\\nSize of Float : %d\"%(sys.getsizeof(z)))\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter integer Value2\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Character ValueA\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter float Value2.2\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Address of x = 19555148\n",
        "\n",
        "Address of y = 48769192\n",
        "\n",
        "Address of z = 44777728\n",
        "\n",
        "After Increment in Pointers\n",
        "\n",
        "Now Address of x = 19555149\n",
        "Now Address of y = 48769193\n",
        "Now Address of z = 44777729\n",
        "\n",
        "Size of Integer : 12\n",
        "Size of Character :  22\n",
        "Size of Float : 16"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.12, Page number: 291<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#The effect of increased and decreased operator used as prefix and suffix with pointer variable.\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "i = int(raw_input(\"Enter Value of i = \"))\n",
      "ii = id(i)\n",
      "\n",
      "#Result\n",
      "sys.stdout.write(\"Address of i = %u\\n\"%(ii))\n",
      "#There is no increment or decrement operator and pointer concept in python\n",
      "ii += 1\n",
      "sys.stdout.write(\"Address of i = %u\\n\"%(ii))\n",
      "sys.stdout.write(\"Address of i = %u\\n\"%(ii))\n",
      "ii += 1\n",
      "ii -= 1\n",
      "sys.stdout.write(\"Address of i = %u\\n\"%(ii))\n",
      "sys.stdout.write(\"Address of i = %u\\n\"%(ii))\n",
      "ii -= 1\n",
      "sys.stdout.write(\"Address of i = %u\\n\"%(ii))\n",
      "\n",
      "#Memory address differs in different systems"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Value of i = 8\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Address of i = 19555076\n",
        "Address of i = 19555077\n",
        "Address of i = 19555077\n",
        "Address of i = 19555077\n",
        "Address of i = 19555077\n",
        "Address of i = 19555076\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.13, Page number: 292<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Perform different arithmetic operations using pointers\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "a = 25\n",
      "b = 10\n",
      "p = id(a)\n",
      "j = id(b)\n",
      "\n",
      "#Result\n",
      "#There is no pointer concept in python and user can't access memory contents directly\n",
      "sys.stdout.write(\"\\nAddition a + b = %d\"%(a+b))\n",
      "sys.stdout.write(\"\\nSubtraction a - b = %d\"%(a-b))\n",
      "sys.stdout.write(\"\\nProduct a * b = %d\"%(a*b))\n",
      "sys.stdout.write(\"\\nDivision a / b = %d\"%(a/b))\n",
      "sys.stdout.write(\"\\na Mod b  = %d\"%(a%b))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Addition a + b = 35\n",
        "Subtraction a - b = 15\n",
        "Product a * b = 250\n",
        "Division a / b = 2\n",
        "a Mod b  = 5"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.14, Page number: 293<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Compare two pointers. \n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "a = 2\n",
      "j = id(a)\n",
      "k = id(a)\n",
      "\n",
      "#Result\n",
      "if j == k:\n",
      "    sys.stdout.write(\"\\nThe Tow Pointers have the same address.\")\n",
      "else:\n",
      "    sys.stdout.write(\"\\nThe Pointers have the different address.\")\n",
      "    \n",
      "sys.stdout.write(\"\\n\\nAddress of a (j) = %u\"%(j))\n",
      "sys.stdout.write(\"\\nAddress of a (k) = %u\"%(k))\n",
      "#Memory address differs in different systems"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "The Tow Pointers have the same address.\n",
        "\n",
        "Address of a (j) = 30726364\n",
        "Address of a (k) = 30726364"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.15, Page number: 294<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Display elements of an array.  \n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "x = [2,4,6,8,10]\n",
      "k = 1\n",
      "\n",
      "#Result\n",
      "while k <= 5:\n",
      "    sys.stdout.write(\"%3d\"%(x[k-1]))\n",
      "    k += 1\n",
      "    \n",
      "#There is no pointer concept in python"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "  2  4  6  8 10"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.16, Page number: 295<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Display array element with their addresses using array name as a pointer\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "x = [2,4,6,8,10]\n",
      "k = 0\n",
      "\n",
      "#Result\n",
      "sys.stdout.write(\"\\nElement No.\\tElement\\tAddress\")\n",
      "\n",
      "while k < 5:\n",
      "    sys.stdout.write(\"\\nx[%d] \\t%13d %10u\"%(k,x[k],id(x[k])))\n",
      "    k += 1\n",
      "    \n",
      "#There is no pointer concept in python"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Element No.\tElement\tAddress\n",
        "x[0] \t            2   30726364\n",
        "x[1] \t            4   30726340\n",
        "x[2] \t            6   30726316\n",
        "x[3] \t            8   30726292\n",
        "x[4] \t           10   30726268"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.17, Page number: 296<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Display array elements with their addresses using array name as a pointer\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "num = [10,25,35,45]\n",
      "\n",
      "#Result\n",
      "sys.stdout.write(\"\\nElement\\t           Address\\n\")\n",
      "\n",
      "for i in range(0,4):\n",
      "    sys.stdout.write(\"num[%d] = %d        \"%(i,num[i]))\n",
      "    sys.stdout.write(\"%8u\\n\"%(id(num[i])))\n",
      "    \n",
      "#There is no pointer concept in python"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Element\t           Address\n",
        "num[0] = 10        30726268\n",
        "num[1] = 25        30726088\n",
        "num[2] = 35        30725968\n",
        "num[3] = 45        30725848\n"
       ]
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.18, Page number: 296<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Access elements of array through different ways using pointer\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "arr = [10,20,30,40,50]\n",
      "p = 0\n",
      "\n",
      "#Result\n",
      "for p in range(0,5):\n",
      "    sys.stdout.write(\"Value of arr[%d] = \"%(p))\n",
      "    sys.stdout.write(\"%d   |  \"%(arr[p]))\n",
      "    sys.stdout.write(\"%d   |  \"%(arr[p]))\n",
      "    sys.stdout.write(\"%d   |  \"%(arr[p]))\n",
      "    sys.stdout.write(\"%d   |  \"%(arr[p]))\n",
      "    sys.stdout.write(\"address of arr[%d] = %u\\n\"%(p,id(arr[p])))\n",
      "    \n",
      "#There is no pointer concept in python\n",
      "#Memory address differs in different systems\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Value of arr[0] = 10   |  10   |  10   |  10   |  address of arr[0] = 30726268\n",
        "Value of arr[1] = 20   |  20   |  20   |  20   |  address of arr[1] = 30726148\n",
        "Value of arr[2] = 30   |  30   |  30   |  30   |  address of arr[2] = 30726028\n",
        "Value of arr[3] = 40   |  40   |  40   |  40   |  address of arr[3] = 30725908\n",
        "Value of arr[4] = 50   |  50   |  50   |  50   |  address of arr[4] = 30725788\n"
       ]
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.19, Page number: 297<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Find sum of all the elements of an array.\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "sum1 = 0        #Since sum is a built-in function in python, sum1 is used\n",
      "i = 0\n",
      "a = [1,2,3,4,5]\n",
      "\n",
      "#Result\n",
      "sys.stdout.write(\"Elements       Values         Address\\n\\n\")\n",
      "\n",
      "while i < 5:\n",
      "    sys.stdout.write(\"a[%d]\\t   %5d\\t    %8u\\n\"%(i,a[i],id(a[i])))\n",
      "    sum1 = sum1 + a[i]\n",
      "    i += 1\n",
      "    \n",
      "#There is no pointer concept in python"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Elements       Values         Address\n",
        "\n",
        "a[0]\t       1\t    30726376\n",
        "a[1]\t       2\t    30726364\n",
        "a[2]\t       3\t    30726352\n",
        "a[3]\t       4\t    30726340\n",
        "a[4]\t       5\t    30726328\n"
       ]
      }
     ],
     "prompt_number": 20
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.20, Page number: 298<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Sum of squares and cubes of array elements using pointer\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "b = [1,2,3,4,5]\n",
      "sumsq = 0\n",
      "sumc = 0\n",
      "\n",
      "#Result\n",
      "for j in range(0,5):\n",
      "    sumsq = sumsq + pow(b[j],2)\n",
      "    sumc = sumc + pow(b[j],3)\n",
      "    \n",
      "sys.stdout.write(\"\\nSum of Squares of array elements : %d\"%(sumsq))\n",
      "sys.stdout.write(\"\\nSum of Cubes of array elements : %d\"%(sumc))\n",
      "\n",
      "#There is no pointer concept in python"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Sum of Squares of array elements : 55\n",
        "Sum of Cubes of array elements : 225"
       ]
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.21, Page number: 299<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Copy element of one array to another array using pointers.\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "so = [10,20,30,40,50]\n",
      "ds = [0 for i in range(0,5)]\n",
      "\n",
      "#Copying\n",
      "for i in range(0,5):\n",
      "    ds[i] = so[i]\n",
      "    \n",
      "sys.stdout.write(\"Original Array      Duplicated Array\")\n",
      "\n",
      "for i in range(0,5):\n",
      "    sys.stdout.write(\"\\n\\t%d\\t\\t%d\"%(so[i],ds[i]))\n",
      "    \n",
      "#There is no pointer concept in python"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Original Array      Duplicated Array\n",
        "\t10\t\t10\n",
        "\t20\t\t20\n",
        "\t30\t\t30\n",
        "\t40\t\t40\n",
        "\t50\t\t50"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.22, Page number: 299<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Copy one array into another array. \n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "arr1 = [15,25,35,45,55]\n",
      "arr2 = [0 for i in range(0,5)]\n",
      "j = 4\n",
      "\n",
      "#Copying & Result\n",
      "sys.stdout.write(\"\\nOriginal Array     Duplicate Array\")\n",
      "for i in range(0,5):\n",
      "    arr2[i] = arr1[j]\n",
      "    j -= 1\n",
      "    sys.stdout.write(\"\\n\\t%d\\t\\t%d\"%(arr1[i],arr2[i]))\n",
      "    \n",
      "#There is no pointer concept in python"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Original Array     Duplicate Array\n",
        "\t15\t\t55\n",
        "\t25\t\t45\n",
        "\t35\t\t35\n",
        "\t45\t\t25\n",
        "\t55\t\t15"
       ]
      }
     ],
     "prompt_number": 24
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.23, Page number: 300<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Display array elements and their address using pointers\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "a = [[1,2,3],[4,5,6],[7,8,9]]\n",
      "j = 1\n",
      "\n",
      "#Result\n",
      "sys.stdout.write(\"\\tElements of An Array with their addresses\\n\\n\")\n",
      "\n",
      "p = id(a[0][0])\n",
      "\n",
      "for i in range(0,3):\n",
      "    for j in range(0,3):\n",
      "        sys.stdout.write(\"%5d  [%5u]\"%(a[i][j],id(a[i][j])))\n",
      "    sys.stdout.write(\"\\n\")\n",
      "    \n",
      "#There is no pointer concept in python\n",
      "#Memory address differs in different systems\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\tElements of An Array with their addresses\n",
        "\n",
        "    1  [30726376]    2  [30726364]    3  [30726352]\n",
        "    4  [30726340]    5  [30726328]    6  [30726316]\n",
        "    7  [30726304]    8  [30726292]    9  [30726280]\n"
       ]
      }
     ],
     "prompt_number": 25
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.24, Page number: 301<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Display array elements and their address. \n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "a = [[1,2,3],[4,5,6],[7,8,9]]\n",
      "\n",
      "#Result\n",
      "sys.stdout.write(\"\\nElements of An Array with their addresses.\\n\\n\")\n",
      "\n",
      "for i in range(0,3):\n",
      "    for j in range(0,3):\n",
      "        sys.stdout.write(\"%5d  [%5u]\"%(a[i][j],id(a[i][j])))\n",
      "    sys.stdout.write(\"\\n\")\n",
      "    \n",
      "#There is no pointer concept in python\n",
      "#Memory address differs in different systems\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Elements of An Array with their addresses.\n",
        "\n",
        "    1  [30726376]    2  [30726364]    3  [30726352]\n",
        "    4  [30726340]    5  [30726328]    6  [30726316]\n",
        "    7  [30726304]    8  [30726292]    9  [30726280]\n"
       ]
      }
     ],
     "prompt_number": 26
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.25, Page number: 302<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Store addresses of different elements of an array using array of pointers\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "arrl = [5,10,15]\n",
      "arrp = [0 for i in range(0,3)]\n",
      "\n",
      "for i in range(0,3):\n",
      "    arrp[i] = id(arrl[i])\n",
      "    \n",
      "#Result\n",
      "sys.stdout.write(\"\\nAddress  \\tElement\\n\")\n",
      "\n",
      "for k in range(0,3):\n",
      "    sys.stdout.write(\"%u\"%(arrp[k]))\n",
      "    sys.stdout.write(\"\\t%7d\\n\"%(arrl[k]))\n",
      "    \n",
      "#There is no pointer concept in python\n",
      "#Memory address differs in different systems\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Address  \tElement\n",
        "30726328\t      5\n",
        "30726268\t     10\n",
        "30726208\t     15\n"
       ]
      }
     ],
     "prompt_number": 29
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.26, Page number: 303<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Display address of elements and pointers\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "a = [0,1,2,3,4]\n",
      "p = [0 for i in range(0,5)]\n",
      "\n",
      "for i in range(0,5):\n",
      "    p[i] = id(a[i])\n",
      "    \n",
      "#Result\n",
      "for i in range(0,5):\n",
      "    sys.stdout.write(\"\\n%d at location\"%(a[i]))\n",
      "    sys.stdout.write(\"\\t%u at location\"%(id(a[i])))\n",
      "    sys.stdout.write(\"   %u\"%(id(p[i])))\n",
      "    \n",
      "#There is no pointer concept in python\n",
      "#Memory address differs in different systems\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "0 at location\t30726388 at location   52456564\n",
        "1 at location\t30726376 at location   52456360\n",
        "2 at location\t30726364 at location   52456456\n",
        "3 at location\t30726352 at location   52456420\n",
        "4 at location\t30726340 at location   52456468"
       ]
      }
     ],
     "prompt_number": 31
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.27, Page number: 304<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Print value of a variable through pointer and pointer to pointer\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "a = 2\n",
      "p = id(a)\n",
      "q = id(p)\n",
      "\n",
      "#Result\n",
      "sys.stdout.write(\"\\n        Value of a = %d Address of a = %u\"%(a,id(a)))\n",
      "sys.stdout.write(\"\\nThrough *p Value of a = %d Address of a = %u\"%(a,p))\n",
      "sys.stdout.write(\"\\nThrough **q Vallue of a = %d Address of a = %d\"%(a,p))\n",
      "\n",
      "#There is no pointer concept in python\n",
      "#Memory address differs in different systems\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "        Value of a = 2 Address of a = 30726364\n",
        "Through *p Value of a = 2 Address of a = 30726364\n",
        "Through **q Vallue of a = 2 Address of a = 30726364"
       ]
      }
     ],
     "prompt_number": 33
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.28, Page number: 305<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Different levels of array of pointer to pointer and display the elements\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "a = [1,2,3]\n",
      "b = [0 for i in range(0,3)]\n",
      "c = [0 for i in range(0,3)]\n",
      "d = [0 for i in range(0,3)]\n",
      "e = [0 for i in range(0,3)]\n",
      "f = [0 for i in range(0,3)]\n",
      "\n",
      "for k in range(0,3):\n",
      "    b[k] = id(a[k])\n",
      "    c[k] = id(b[k])\n",
      "    d[k] = id(c[k])\n",
      "    e[k] = id(d[k])\n",
      "    f[k] = id(e[k])\n",
      "    \n",
      "#Result\n",
      "for k in range(0,3):\n",
      "    sys.stdout.write(\"%3d\"%(a[k]))\n",
      "    sys.stdout.write(\"%3d\"%(a[k]))\n",
      "    sys.stdout.write(\"%3d\"%(a[k]))\n",
      "    sys.stdout.write(\"%3d\"%(a[k]))\n",
      "    sys.stdout.write(\"%3d\\n\"%(a[k]))\n",
      "    \n",
      "#There is no pointer concept in python\n",
      "#Memory address differs in different systems\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "  1  1  1  1  1\n",
        "  2  2  2  2  2\n",
        "  3  3  3  3  3\n"
       ]
      }
     ],
     "prompt_number": 36
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.29, Page number: 306<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Display string using character pointer\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "name = ['K','U','M','A','R']\n",
      "ch = id(name)\n",
      "i = 0\n",
      "\n",
      "#Result\n",
      "while i < len(name):                 #There is no null terminating character in python string\n",
      "    sys.stdout.write(\"%c\"%(name[i]))\n",
      "    i += 1\n",
      "    \n",
      "#There is no pointer concept in python"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "KUMAR"
       ]
      }
     ],
     "prompt_number": 37
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.30, Page number: 307<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Length of a given string including and excluding spaces using pointers\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "p = 0\n",
      "q = 0\n",
      "s = 0\n",
      "str1 = ['P','O','I','N','T','E','R','S',' ','A','R','E',' ','E','A','S','Y']\n",
      "\n",
      "#Result\n",
      "while s < len(str1):          #There is no null termination character for python string\n",
      "    sys.stdout.write(\"%c\"%(str1[s]))\n",
      "    p += 1\n",
      "    if ord(str1[s]) == 32:\n",
      "        q += 1\n",
      "    s += 1\n",
      "\n",
      "sys.stdout.write(\"\\nLength of String including spaces : %d\"%(p))\n",
      "sys.stdout.write(\"\\nLength of String excluding spaces : %d\"%(p-q))\n",
      "\n",
      "#There is no pointer concept in python"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "POINTERS ARE EASY\n",
        "Length of String including spaces : 17\n",
        "Length of String excluding spaces : 15"
       ]
      }
     ],
     "prompt_number": 40
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.31, Page number: 308<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Interchange elements of character array using pointer\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "names = [\"kapil\",\"manoj\",\"amit\",\"amol\",\"pavan\",\"mahesh\"]\n",
      "\n",
      "#Result\n",
      "sys.stdout.write(\"Original : %s  %s\"%(names[3],names[4]))\n",
      "\n",
      "tmp = names[3]\n",
      "names[3] = names[4]\n",
      "names[4] = tmp\n",
      "\n",
      "sys.stdout.write(\"\\nNew    : %s  %s\"%(names[3],names[4]))\n",
      "\n",
      "#There is no pointer concept in python"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Original : amol  pavan\n",
        "New    : pavan  amol"
       ]
      }
     ],
     "prompt_number": 41
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.32, Page number: 308<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#String comparison.\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "c = 0\n",
      "str1 = raw_input(\"Enter First String : \")\n",
      "str2 = raw_input(\"Enter Second String : \")\n",
      "a = 0\n",
      "l = 0\n",
      "\n",
      "#Result\n",
      "sys.stdout.write(\"\\nSimilar Characters Found in Both String\")\n",
      "\n",
      "while a < len(str1):\n",
      "    if str1[a] == str2[a]:\n",
      "        sys.stdout.write(\"\\n\\t%c\\t%c\"%(str1[a],str2[a]))\n",
      "        l += 1\n",
      "    else:\n",
      "        c += 1\n",
      "    a += 1\n",
      "        \n",
      "if c == 0:\n",
      "    sys.stdout.write(\"\\nThe Strings are Identical\")\n",
      "else:\n",
      "    sys.stdout.write(\"\\nThe Strings are different at %d places.\"%(c))\n",
      "    sys.stdout.write(\"\\nThe String Characters are similar at %d places.\"%(l))\n",
      "    \n",
      "#There is no pointer concept in python"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter First String : SUNDAY\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Second String : MONDAY\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Similar Characters Found in Both String\n",
        "\tN\tN\n",
        "\tD\tD\n",
        "\tA\tA\n",
        "\tY\tY\n",
        "The Strings are different at 2 places.\n",
        "The String Characters are similar at 4 places."
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.33, Page number: 310<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Compare three characters using pointers.\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "x = raw_input(\"Enter Three Characters\")\n",
      "y = raw_input(\"Enter Three Characters\")\n",
      "z = raw_input(\"Enter Three Characters\")\n",
      "\n",
      "stat = 0\n",
      "\n",
      "stat = y > x\n",
      "if x == y:\n",
      "    sys.stdout.write(\"\\n1st and 2nd Character are same\\n\")\n",
      "else:\n",
      "    if stat < 0:\n",
      "        sys.stdout.write(\"\\n2nd Character appears after the 1st Character in Alphabetic\\n\")\n",
      "    else:\n",
      "        sys.stdout.write(\"2nd Character appears before the first Character in Alphabetic\\n\")\n",
      "    \n",
      "stat = y > z\n",
      "\n",
      "if y == z:\n",
      "    sys.stdout.write(\"\\n2nd and 3rd Character are same.\")\n",
      "else:\n",
      "    if stat > 0:\n",
      "        sys.stdout.write(\"\\n2nd Character appears after the 3rd Character in Alphabetic\")\n",
      "    else:\n",
      "        sys.stdout.write(\"\\n2nd Character appears before the 3rd Character in Alphabetic\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Three CharactersC\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Three CharactersC\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Three CharactersA\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "1st and 2nd Character are same\n",
        "\n",
        "2nd Character appears after the 3rd Character in Alphabetic"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.34, Page number: 311<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Compare two strings irrespective of case.  \n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "buf1 = \"computer\"\n",
      "buf2 = \"computer\"\n",
      "\n",
      "#Comparison\n",
      "stat = buf1 == buf2\n",
      "\n",
      "#Result\n",
      "sys.stdout.write(\"The Characters upto 4th position are \")\n",
      "if stat == 0:\n",
      "    sys.stdout.write(\"not \")\n",
      "sys.stdout.write(\"same\")\n",
      "\n",
      "#There is no pointer concept in python and memicmp() function"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The Characters upto 4th position are same"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.35, Page number: 311<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Print the string upto the first occurrence of a character\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "\n",
      "src = raw_input(\"Enter a String : \")\n",
      "f = raw_input(\"Enter a Character to find in the text : \")\n",
      "dest = ['0' for i in range(0,len(src))]\n",
      "ptr = 0\n",
      "\n",
      "while ptr < len(src):\n",
      "    if src[ptr] == f:\n",
      "        dest[ptr] = src[ptr]\n",
      "        break\n",
      "    else:\n",
      "        dest[ptr] = src[ptr]\n",
      "    ptr += 1\n",
      "\n",
      "if ptr == len(src):\n",
      "    sys.stdout.write(\"The character wasn't found\\n\")\n",
      "else:\n",
      "    sys.stdout.write(\"String upto that Character : %s\"%(dest[0:ptr+1]))\n",
      "    \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a String : FUNCTIONS\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a Character to find in the text : T\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "String upto that Character : ['F', 'U', 'N', 'C', 'T']"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.36, Page number: 312<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Replace the contents of second string with the first string. \n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "src = raw_input(\"Enter a Source String : \")\n",
      "dest = raw_input(\"Enter a Destination String :\")\n",
      "\n",
      "#Result\n",
      "sys.stdout.write(\"\\n\\nDestination before memcpy : %s\\n\"%(dest))\n",
      "dest = src[:len(src)] + dest[len(src):]\n",
      "\n",
      "sys.stdout.write(\"Destination after memcpy : %s\\n\"%(dest))\n",
      "\n",
      "#There is no pointer concept in python and memcpy function"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a Source String : Tomorrow\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a Destination String :Today   is Sunday\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "\n",
        "Destination before memcpy : Today   is Sunday\n",
        "Destination after memcpy : Tomorrowis Sunday\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.37, Page number: 313<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Display the string through their pointer\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization\n",
      "c = \"Central Processing Unit\"\n",
      "m = \"Math Co- Processor\"\n",
      "\n",
      "#Result\n",
      "sys.stdout.write(\"'c' is pointing the string '%s'\\n\"%(c))\n",
      "sys.stdout.write(\"'m' is pointing the string '%s'\\n\"%(m))\n",
      "\n",
      "#There is no pointer concept in python"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "'c' is pointing the string 'Central Processing Unit'\n",
        "'m' is pointing the string 'Math Co- Processor'\n"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 9.38, Page number: 314<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Use void pointer to display the value of different variables.\n",
      "\n",
      "import sys\n",
      "\n",
      "#Variable Initialization & Result\n",
      "pt = 12\n",
      "sys.stdout.write(\"\\nP = %d\"%(pt))\n",
      "pt = 5.4\n",
      "sys.stdout.write(\"\\nR = %.1f\"%(pt))\n",
      "pt = 'S'\n",
      "sys.stdout.write(\"\\nC = %c\"%(pt))\n",
      "\n",
      "#There is no pointer concept in python"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "P = 12\n",
        "R = 5.4\n",
        "C = S"
       ]
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}