{ "metadata": { "name": "", "signature": "sha256:ebda2195f5f56b03f9c5aecb268e628cfb74a1a1fa1ea18ae277cc3f964d54bd" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 6: Pointing to data" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.1, Page No.98" ] }, { "cell_type": "code", "collapsed": false, "input": [ "num=100\n", "sum=0.0123456789\n", "text=\"C++ Fun\"\n", "print \"Integer variable starts at\",hex(id(num))\n", "print \"Double variable starts at\",hex(id(sum))\n", "print \"String variable starts at\",hex(id(text))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Integer variable starts at 0x126eb04\n", "Double variable starts at 0x279b490\n", "String variable starts at 0x2b7a160\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.2, Page No.100" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a=8\n", "b=16\n", "aPtr=id(a)\n", "bPtr=id(b)\n", "print \"Address of pointers...\"\n", "print \"aPtr:\",hex(id(aPtr))\n", "print \"bPtr:\",hex(id(bPtr))\n", "print \"Values in pointers...\"\n", "print \"aPtr:\",hex(aPtr)\n", "print \"bPtr:\",hex(bPtr)\n", "#As python does not support pointers values can't be displayed using * symbol" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Address of pointers...\n", "aPtr: 0x2808ac8\n", "bPtr: 0x2808b1c\n", "Values in pointers...\n", "aPtr: 0x126e794\n", "bPtr: 0x126e734\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.3, Page No.102" ] }, { "cell_type": "code", "collapsed": false, "input": [ "nums={1,2,3,4,5,6,7,8,9,10}\n", "ptr=id(nums)\n", "#As python does not support concept of pointers we can not use pointer arithmetic to print numbers" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.4, Page No.104" ] }, { "cell_type": "code", "collapsed": false, "input": [ "num=5\n", "def writeOutput(value):\n", " print \"Current Value: \",value\n", "def computeTriple(value):\n", " global num\n", " num=num*3\n", "\n", "\n", "writeOutput(num)\n", "num=num+15\n", "writeOutput(num)\n", "computeTriple(num)\n", "writeOutput(num)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Current Value: 5\n", "Current Value: 20\n", "Current Value: 60\n" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.5, Page No.106" ] }, { "cell_type": "code", "collapsed": false, "input": [ "letters=['C','+','+',' ','F','u','n','\\0']\n", "text=\"C++ Fun\"\n", "term=\"Element\"\n", "lang=\"C++\"\n", "ap1=[\"Great \",\"Program \",\"Code \"]\n", "ap2=[lang,\"is \",\"Fun\"]\n", "ap3=[ap2[0],ap2[1],ap1[0]]\n", "ap4=[ap1[2],ap2[1],ap2[2]]\n", "print \"\".join(letters)\n", "print text\n", "for i in range(0,3):\n", " print term,i,\" \",\n", " print ap1[i],\" \",\n", " print ap2[i],\" \",\n", " print ap3[i],\" \",\n", " print ap4[i],\" \"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "C++ Fun\u0000\n", "C++ Fun\n", "Element 0 Great C++ C++ Code \n", "Element 1 Program is is is \n", "Element 2 Code Fun Great Fun \n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.6, Page No.108" ] }, { "cell_type": "code", "collapsed": false, "input": [ "num=0\n", "rNum=400\n", "num=rNum\n", "print \"Value direct\",num\n", "print \"Value via reference \",rNum\n", "\n", "print \"Address direct\",hex(id(num))\n", "print \"Address via reference\",hex(id(rNum))\n", "\n", "rNum=rNum*2\n", "num=rNum\n", "\n", "print \"Value direct\",num\n", "print \"Value via reference \",rNum\n", "\n", "#as python does not support pointers" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Value direct 400\n", "Value via reference 400\n", "Address direct 0x27cd294\n", "Address via reference 0x27cd294\n", "Value direct 800\n", "Value via reference 800\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.7, Page No. 110" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def writeOutput(value):\n", " print \"Current Value: \",value\n", "\n", "def computeTriple(value):\n", " value=value*3\n", " print \"Current Value: \",value\n", " \n", "num=5\n", "ref=num\n", "writeOutput(num)\n", "num=num+15\n", "writeOutput(num)\n", "computeTriple(num)\n", "\n", "#as python does not support pointers I dont have used pointer in this program" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Current Value: 5\n", "Current Value: 20\n", "Current Value: 60\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.8, Page No.113" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def add(a,b):\n", " total=a+b\n", " print \"total:\",total\n", "\n", "num=100\n", "sum=500\n", "rNum=num\n", "ptr=num\n", "print \"Reference:\",rNum\n", "print \"Pointer:\",ptr\n", "ptr=sum\n", "print \"Pointer now:\",ptr\n", "add(rNum,ptr)\n", "\n", "\n", "#as python does not support pointers I dont have used pointer in this progaram" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Reference: 100\n", "Pointer: 100\n", "Pointer now: 500\n", "total: 600\n" ] } ], "prompt_number": 10 } ], "metadata": {} } ] }