{ "metadata": { "name": "chapter-8.ipynb" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 8: Arrays

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

Average Marks, Page number: 272

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "s = 0\n", "marks = [] # array declaration\n", "\n", "#for i in range(0,30):\n", " # marks.append(int(raw_input(\"Enter marks: \" ))) # store data in array\n", "marks = [89,85,57,25,90,45,87,48,98,12,39,66,75,30,87,100,5,78,56,99,84,0,39,79,93,61,87,45,90,56] \n", "print \"Enter marks: \"\n", "for i in range(0,30):\n", " print marks[i]\n", "\n", "for i in range(0,30):\n", " s = s + marks[i] # read data from array\n", "\n", "#Calculation\n", "avg = s / 30 #Average formula\n", "\n", "#Result\n", "print \"Average marks = \", avg \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter marks: \n", "89\n", "85\n", "57\n", "25\n", "90\n", "45\n", "87\n", "48\n", "98\n", "12\n", "39\n", "66\n", "75\n", "30\n", "87\n", "100\n", "5\n", "78\n", "56\n", "99\n", "84\n", "0\n", "39\n", "79\n", "93\n", "61\n", "87\n", "45\n", "90\n", "56\n", "Average marks = 63\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Call By Value, Page number: 277

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Funcion definition\n", "def display(m):\n", " print m \n", " \n", "#Variable declaration\n", "marks = [ 55, 65, 75, 56, 78, 78, 90 ] #array\n", "\n", "for i in range(0,7):\n", " display(marks[i]) #function call \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "55\n", "65\n", "75\n", "56\n", "78\n", "78\n", "90\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Call By Reference , Page number: 278

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Funcion definition\n", "def display(n):\n", " print n #return\n", " \n", "#Variable declaration\n", "marks = [ 55, 65, 75, 56, 78, 78, 90 ] #array\n", "\n", "for i in range(0,7):\n", " display(marks[i]) #function call\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "55\n", "65\n", "75\n", "56\n", "78\n", "78\n", "90\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Pointer Arithmetic, Page number: 279

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "i = 3\n", "j = 1.5\n", "k = 'c'\n", "\n", "print \"Value of i = \", i \n", "print \"Value of j = \", j \n", "print \"Value of k = \", k \n", "\n", "#addresses of the variables\n", "x = id(i)\n", "y = id(j)\n", "z = id(k)\n", "\n", "print \"Original address in x = \", x \n", "print \"Original address in y = \", y \n", "print \"Original address in z = \", z \n", "\n", "x += 2\n", "y += 4\n", "z += 1\n", "\n", "print \"New address in x = \", x \n", "print \"New address in y = \", y \n", "print \"New address in z = \", z \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Value of i = 3\n", "Value of j = 1.5\n", "Value of k = c\n", "Original address in x = 32529512\n", "Original address in y = 105587440\n", "Original address in z = 32744192\n", "New address in x = 32529514\n", "New address in y = 105587444\n", "New address in z = 32744193\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Pointer Subtraction , Page number: 281

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "arr = [ 10, 20, 30, 45, 67, 56, 74 ]\n", "i = id(arr[1]) #address \n", "j = id(arr[5]) #Address\n", "\n", "#Result\n", "print j - i, arr[5] - arr[1] \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1128 36\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Pointer Comparison, Page number: 282

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "arr = [ 10, 20, 36, 72, 45, 36 ]\n", "j = id(arr[ 4 ])\n", "k = id( arr[0 + 4] )\n", "\n", "#Result \n", "if j == k : #comparison\n", " print \"The two pointers point to the same location\" \n", "else:\n", " print \"The two pointers do not point to the same location\" \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The two pointers point to the same location\n" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Memory Locations, Page number: 283

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "num = [ 24, 34, 12, 44, 56, 17]\n", "\n", "#Result\n", "for i in range(0,6):\n", " print \"element no. \", i \n", " print \"address = \", id(num[i]) \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "element no. 0\n", "address = 32529008\n", "element no. 1\n", "address = 32528768\n", "element no. 2\n", "address = 32529296\n", "element no. 3\n", "address = 32530520\n", "element no. 4\n", "address = 32530232\n", "element no. 5\n", "address = 32529176\n" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Accessing Array Elements , Page number: 284

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "num = [ 24, 34, 12, 44, 56, 17]\n", "\n", "#Result\n", "for i in range(0,6):\n", " print \"address = \", id(num[i]) \n", " print \"element = \", num[i] \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "address = 32529008\n", "element = 24\n", "address = 32528768\n", "element = 34\n", "address = 32529296\n", "element = 12\n", "address = 32530520\n", "element = 44\n", "address = 32530232\n", "element = 56\n", "address = 32529176\n", "element = 17\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Accessing Array Elements Using Pointers, Page number: 284

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "num = [ 24, 34, 12, 44, 56, 17]\n", "j = id(num[0]) # assign address of zeroth element\n", "\n", "#Result\n", "for i in range(0,6):\n", " print \"address = \", j \n", " print \"element = \", num[i] \n", " j = id(num[(i+1)%6]) # increment pointer to point to next location \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "address = 32529008\n", "element = 24\n", "address = 32528768\n", "element = 34\n", "address = 32529296\n", "element = 12\n", "address = 32530520\n", "element = 44\n", "address = 32530232\n", "element = 56\n", "address = 32529176\n", "element = 17\n" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Passing An Array to a Function , Page number: 286

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "num = [ 24, 34, 12, 44, 56, 17 ]\n", "\n", "#Function definition:\n", "def display ( j,n ):\n", " for i in range(0,n):\n", " print \"element = \", j\n", " j = num[(i+1)%n] #increment pointer to point to next element \n", "\n", "display ( num[0], 6 ) #function call\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "element = 24\n", "element = 34\n", "element = 12\n", "element = 44\n", "element = 56\n", "element = 17\n" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Accessing Array Elements in Different Ways, Page number: 288

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "num = [ 24, 34, 12, 44, 56, 17]\n", "\n", "for i in range(0,6):\n", " print \"address = \", id(num[i])\n", " print \"element = \", num[i], num[(0+ i)]\n", " print num[(i+0)], num[i] \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "address = 32529008\n", "element = 24 24\n", "24 24\n", "address = 32528768\n", "element = 34 34\n", "34 34\n", "address = 32529296\n", "element = 12 12\n", "12 12\n", "address = 32530520\n", "element = 44 44\n", "44 44\n", "address = 32530232\n", "element = 56 56\n", "56 56\n", "address = 32529176\n", "element = 17 17\n", "17 17\n" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Two Dimensional Array , Page number: 289

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "stud = [] #array\n", "\n", "#for i in range(0,4):\n", " # stud.append((raw_input(\"Enter roll no and marks: \").split())) # storing data in the 2D array\n", "\n", "stud = [\"1 38\",\"2 78\",\"3 93\",\"4 48\"]\n", "for i in range (0,4):\n", " print \"Enter roll no and marks: \"\n", " print stud[i]\n", " \n", "#Result\n", "for i in range(0,4):\n", " print stud[i][0],stud[i][1:] \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter roll no and marks: \n", "1 38\n", "Enter roll no and marks: \n", "2 78\n", "Enter roll no and marks: \n", "3 93\n", "Enter roll no and marks: \n", "4 48\n", "1 38\n", "2 78\n", "3 93\n", "4 48\n" ] } ], "prompt_number": 19 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

2-D Array Demo , Page number: 293

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "s = [ [1234, 56 ], [ 1212, 33], [ 1434, 80 ], [ 1312, 78 ]]\n", "\n", "#Result\n", "for i in range(0,4):\n", " print \"Address of %d th 1-D array = %u\"%( i, id(s[i]) )\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Address of 0 th 1-D array = 134002248\n", "Address of 1 th 1-D array = 134654472\n", "Address of 2 th 1-D array = 134791816\n", "Address of 3 th 1-D array = 134792008\n" ] } ], "prompt_number": 21 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Accessing 2-D Array Elements, Page number: 295

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "s = [ [ 1234, 56 ], [ 1212, 33 ], [ 1434, 80 ], [ 1312, 78 ] ]\n", "\n", "for i in range(0,4):\n", " for j in range(0,2):\n", " print s[i][j] \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1234\n", "56\n", "1212\n", "33\n", "1434\n", "80\n", "1312\n", "78\n" ] } ], "prompt_number": 22 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Pointer To An Array , Page number: 296

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "s = [ [ 1234, 56 ], [ 1212, 33 ], [ 1434, 80 ], [ 1312, 78 ]]\n", "\n", "#Result\n", "for i in range(0,4):\n", " p = s[i]\n", " pint = p\n", " print \"\\n\" \n", " for j in range(0,2):\n", " print pint[j] \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "1234\n", "56\n", "\n", "\n", "1212\n", "33\n", "\n", "\n", "1434\n", "80\n", "\n", "\n", "1312\n", "78\n" ] } ], "prompt_number": 23 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Passing 2-D Array To a Function , Page number: 297

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "a = [[1, 2, 3, 4] , [5, 6, 7, 8] , [9, 0, 1, 6 ]]\n", "\n", "#Function definitions\n", "def display ( q, row, col ):\n", " for i in range(0,row):\n", " for j in range(0,col):\n", " print q [ (i * col)%3][j]\n", " print \"\\n\" \n", " print \"\\n\" \n", "\n", "\n", "def show ( q, row, col ):\n", " for i in range(0,row):\n", " p = q[i]\n", " for j in range(0,col):\n", " print p[j]\n", " print \"\\n\" \n", " print \"\\n\" \n", "\n", "def Print ( q, row, col ):\n", " for i in range(0,row):\n", " for j in range(0,col):\n", " print q[i][j]\n", " print \"\\n\" \n", " print \"\\n\" \n", " \n", "#function calls\n", "display ( a, 3, 4 ) \n", "show ( a, 3, 4 ) \n", "Print ( a, 3, 4 )\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "2\n", "3\n", "4\n", "\n", "\n", "5\n", "6\n", "7\n", "8\n", "\n", "\n", "9\n", "0\n", "1\n", "6\n", "\n", "\n", "\n", "\n", "1\n", "2\n", "3\n", "4\n", "\n", "\n", "5\n", "6\n", "7\n", "8\n", "\n", "\n", "9\n", "0\n", "1\n", "6\n", "\n", "\n", "\n", "\n", "1\n", "2\n", "3\n", "4\n", "\n", "\n", "5\n", "6\n", "7\n", "8\n", "\n", "\n", "9\n", "0\n", "1\n", "6\n", "\n", "\n", "\n", "\n" ] } ], "prompt_number": 25 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Array of Pointers , Page number: 300

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from ctypes import *\n", "\n", "#Variable declaration\n", "arr = [] # array of integer pointers \n", "i = c_int(31)\n", "j = c_int(5)\n", "k = c_int(19)\n", "l = c_int(71)\n", "arr.append(pointer(i))\n", "arr.append(pointer(j))\n", "arr.append(pointer(k))\n", "arr.append(pointer(l))\n", "\n", "for m in range(0,4):\n", " print arr[m].contents " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "c_long(31)\n", "c_long(5)\n", "c_long(19)\n", "c_long(71)\n" ] } ], "prompt_number": 26 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Address Array , Page number: 301

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "a = [ 0, 1, 2, 3, 4 ]\n", "p = [ a[0], a[1], a[2], a[3], a[4] ]\n", "\n", "#Result\n", "print p, id(p), id(id(p ) )\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[0, 1, 2, 3, 4] 134794120 134005648\n" ] } ], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }