summaryrefslogtreecommitdiff
path: root/ANSI_C_Programming/chapter8.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'ANSI_C_Programming/chapter8.ipynb')
-rw-r--r--ANSI_C_Programming/chapter8.ipynb2798
1 files changed, 1399 insertions, 1399 deletions
diff --git a/ANSI_C_Programming/chapter8.ipynb b/ANSI_C_Programming/chapter8.ipynb
index 9dbb6099..50b1b566 100644
--- a/ANSI_C_Programming/chapter8.ipynb
+++ b/ANSI_C_Programming/chapter8.ipynb
@@ -1,1400 +1,1400 @@
-{
- "metadata": {
- "name": "chapter8.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "CHAPTER 8: ARRAYS"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:256"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#reassigning value to a variable\n",
- "x=5\n",
- "x=10\n",
- "print \"x=%d\\n\" % (x) #prints 10"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "x=10\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:257-258"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program to find average marks obtained by a class of 30 students\n",
- "sum=0\n",
- "marks=[] #array declaration\n",
- "for i in range(30):\n",
- " marks.append(0)\n",
- "for i in range(0,30,1):\n",
- " print \"Enter marks\"\n",
- " marks[i]=eval(raw_input()) #store data in array\n",
- "for i in range(0,30,1):\n",
- " sum=sum+marks[i]\n",
- "avg=sum/30\n",
- "print \"Average marks=%d\\n\" % (avg)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "98\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "88\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "97\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "96\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "87\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "89\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "80\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "86\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "95\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "99\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "90\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "100\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "99\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "94\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "85\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "84\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "92\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "91\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "87\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "76\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "75\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "96\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "83\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "80\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "93\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "79\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "70\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "78\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "85\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "86\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Average marks=87\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:261"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#storing elements into an array\n",
- "num=[]\n",
- "for i in range(40):\n",
- " num.append(0)\n",
- "for i in range(0,40,1): #be carefull about the size of the array\n",
- " num[i]=i"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:261-262"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Demonstration of call by value\n",
- "def display(m):\n",
- " print \"%d\" % (m)\n",
- "marks=[55,65,75,56,78,78,90]\n",
- "for i in range(0,7,1):\n",
- " display(marks[i])"
- ],
- "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": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:263"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#calling of function inside function to print an array\n",
- "def show(n):\n",
- " print \"%d\" % (n)\n",
- "def disp(n):\n",
- " show(n) #calls show()\n",
- "marks=[55,65,75,56,78,78,90]\n",
- "for i in range(0,7,1):\n",
- " disp(marks[i])"
- ],
- "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": 4
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:263-264"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing address and values of variables\n",
- "i=3\n",
- "j=1.5\n",
- "k='c'\n",
- "print \"Value of i=%d\\n\" % (i)\n",
- "print \"Value of j=%f\\n\" % (j)\n",
- "print \"Value of k=%c\\n\" % (k)\n",
- "x=id(i)\n",
- "y=id(j)\n",
- "z=id(k)\n",
- "print \"Original address in x=%u\\n\" % (x)\n",
- "print \"Original address in y=%u\\n\" % (y)\n",
- "print \"Original address in z=%u\\n\" % (z)\n",
- "x+=1\n",
- "y+=1\n",
- "z+=1\n",
- "print \"New address in x=%u\\n\" % (x)\n",
- "print \"New address in y=%u\\n\" % (y)\n",
- "print \"New address in z=%u\\n\" % (z)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Value of i=3\n",
- "\n",
- "Value of j=1.500000\n",
- "\n",
- "Value of k=c\n",
- "\n",
- "Original address in x=21147312\n",
- "\n",
- "Original address in y=88181272\n",
- "\n",
- "Original address in z=21311640\n",
- "\n",
- "New address in x=21147313\n",
- "\n",
- "New address in y=88181273\n",
- "\n",
- "New address in z=21311641\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 5
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:265"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing subtraction of addresses & values of 2 elements of array\n",
- "arr=[10,20,30,45,67,56,74]\n",
- "i=id(arr[1])\n",
- "j=id(arr[5])\n",
- "print \"%d %d\\n\" % (j-i,arr[5]-arr[1])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "-432 36\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 6
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:266"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Comparison of addresses of elements of same array\n",
- "arr=[10,20,36,72,45,36]\n",
- "j=id(arr[4])\n",
- "k=id(arr[0+4]) #didn't get any other way\n",
- "if j==k:\n",
- " print \"The two pointers point to the same location\\n\"\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",
- "\n"
- ]
- }
- ],
- "prompt_number": 7
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:267"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "##printing memory location of elements of array\n",
- "num=[24,34,12,44,56,17]\n",
- "for i in range(0,6,1):\n",
- " print \"element no.%d\" % (i)\n",
- " print \"address=%u\\n\" % (id(num[i]))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "element no.0\n",
- "address=21147060\n",
- "\n",
- "element no.1\n",
- "address=21146940\n",
- "\n",
- "element no.2\n",
- "address=21147204\n",
- "\n",
- "element no.3\n",
- "address=21146820\n",
- "\n",
- "element no.4\n",
- "address=21146676\n",
- "\n",
- "element no.5\n",
- "address=21147144\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 8
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:268"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#print addresses & elements of array\n",
- "num=[24,34,12,44,56,17]\n",
- "for i in range(0,6,1):\n",
- " print \"address=%u\" % (id(num[i]))\n",
- " print \"element=%d\\n\" % (num[i])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "address=21147060\n",
- "element=24\n",
- "\n",
- "address=21146940\n",
- "element=34\n",
- "\n",
- "address=21147204\n",
- "element=12\n",
- "\n",
- "address=21146820\n",
- "element=44\n",
- "\n",
- "address=21146676\n",
- "element=56\n",
- "\n",
- "address=21147144\n",
- "element=17\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 10
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:268"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program for accessing the elements of array\n",
- "num=[24,34,12,44,56,17]\n",
- "j=id(num[0]) #assign address of zeroth element\n",
- "for i in range(0,6,1):\n",
- " print \"address=%u\" % (id(num[i]))\n",
- " print \"element=%d\\n\" % (num[i]) "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "address=21147060\n",
- "element=24\n",
- "\n",
- "address=21146940\n",
- "element=34\n",
- "\n",
- "address=21147204\n",
- "element=12\n",
- "\n",
- "address=21146820\n",
- "element=44\n",
- "\n",
- "address=21146676\n",
- "element=56\n",
- "\n",
- "address=21147144\n",
- "element=17\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 11
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:270"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Demonstration of passing an entire array to a function\n",
- "def display(j,n):\n",
- " for item in j:\n",
- " print \"element=%d\\n\" % (item)\n",
- "num=[24,34,12,44,56,17]\n",
- "display(num,6)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "element=24\n",
- "\n",
- "element=34\n",
- "\n",
- "element=12\n",
- "\n",
- "element=44\n",
- "\n",
- "element=56\n",
- "\n",
- "element=17\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 12
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:271"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Another way of accessing the elements of array\n",
- "num=[24,34,12,44,56,17]\n",
- "for i in range(0,6,1):\n",
- " print \"address=%u\" % (id(num[i]))\n",
- " print \"element=%d %d\" % (num[i],num[i]) #no other way\n",
- " print \"%d %d\\n\" % (num[i],num[i])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "address=21147060\n",
- "element=24 24\n",
- "24 24\n",
- "\n",
- "address=21146940\n",
- "element=34 34\n",
- "34 34\n",
- "\n",
- "address=21147204\n",
- "element=12 12\n",
- "12 12\n",
- "\n",
- "address=21146820\n",
- "element=44 44\n",
- "44 44\n",
- "\n",
- "address=21146676\n",
- "element=56 56\n",
- "56 56\n",
- "\n",
- "address=21147144\n",
- "element=17 17\n",
- "17 17\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 13
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:272"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Storing roll no. & marks of 2 students using 2-D array\n",
- "stud=[[0,0],[0,0],[0,0],[0,0]]\n",
- "for i in range(0,4,1):\n",
- " print \"Enter roll no. and marks\"\n",
- " stud[i][0]=eval(raw_input())\n",
- " stud[i][1]=eval(raw_input())\n",
- "for i in range(0,4,1):\n",
- " print \"%d %d\\n\" % (stud[i][0],stud[i][1])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter roll no. and marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1206030\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "100\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter roll no. and marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1206007\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "95\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter roll no. and marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1206034\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "97\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter roll no. and marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1206026\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "88\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1206030 100\n",
- "\n",
- "1206007 95\n",
- "\n",
- "1206034 97\n",
- "\n",
- "1206026 88\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 14
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:275"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Demo:2-D array is an array of arrays\n",
- "s=[[1234,56],[1212,33],[1434,80],[1312,78]]\n",
- "for i in range(0,4,1):\n",
- " print \"Address of %dth 1-D array=%u\\n\" % (i,id(s[i]))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Address of 0th 1-D array=88430392\n",
- "\n",
- "Address of 1th 1-D array=89514192\n",
- "\n",
- "Address of 2th 1-D array=88430712\n",
- "\n",
- "Address of 3th 1-D array=88430312\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 15
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:277"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Accessing elements of 2-D array\n",
- "s=[[1234,56],[1212,33],[1434,80],[1312,78]]\n",
- "for i in range(0,4,1):\n",
- " for j in range(0,2,1):\n",
- " print \"%d\" % (s[i][j])\n",
- " print \"\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1234\n",
- "56\n",
- "\n",
- "\n",
- "1212\n",
- "33\n",
- "\n",
- "\n",
- "1434\n",
- "80\n",
- "\n",
- "\n",
- "1312\n",
- "78\n",
- "\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 16
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:277-278"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Usage of pointer to an array\n",
- "s=[[1234,56],[1212,33],[1434,80],[1312,78]]\n",
- "p=[]\n",
- "for i in range(2):\n",
- " p.append(0)\n",
- "for i in range(0,4,1):\n",
- " p=s\n",
- " pint=p\n",
- " print \"\\n\"\n",
- " for j in range(0,2,1):\n",
- " print \"%d\" % (pint[i][j])"
- ],
- "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": 18
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:279-280"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Passing 2-D array to a function\n",
- "#can only implement only one way due to absence of pointers in python\n",
- "def display(q,row,col):\n",
- " for item in q:\n",
- " print item\n",
- " print \"\\n\"\n",
- "a=[[1,2,3,4],[5,6,7,8],[9,0,1,6]]\n",
- "display(a,3,4)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "[1, 2, 3, 4]\n",
- "[5, 6, 7, 8]\n",
- "[9, 0, 1, 6]\n",
- "\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 20
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:281-282"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Assigning values to the elements of an array seperately\n",
- "arr=[]\n",
- "for i in range(4):\n",
- " arr.append(0)\n",
- "i=31\n",
- "j=5\n",
- "k=19\n",
- "l=71\n",
- "arr[0]=i\n",
- "arr[1]=j\n",
- "arr[2]=k\n",
- "arr[3]=l\n",
- "for m in range(0,4,1):\n",
- " print \"%d\\n\" % (arr[m])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "31\n",
- "\n",
- "5\n",
- "\n",
- "19\n",
- "\n",
- "71\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 21
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:282"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing address of an element of array\n",
- "a=[0,1,2,3,4]\n",
- "print \"%u %u %d\\n\" % (id(id(a[0])),id(a[0]),a[0])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "88518416 21147348 0\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 22
- }
- ],
- "metadata": {}
- }
- ]
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:7a02054929dffb84c1e468572cd4b1d9ed109e9267a67821f68ad3b293946118"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "CHAPTER 8: ARRAYS"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:256"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "x=5\n",
+ "x=10\n",
+ "print \"x=%d\\n\" % (x) #prints 10"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x=10\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:257-258"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "sum=0\n",
+ "marks=[] #array declaration\n",
+ "for i in range(30):\n",
+ " marks.append(0)\n",
+ "for i in range(0,30,1):\n",
+ " print \"Enter marks\"\n",
+ " marks[i]=eval(raw_input()) #store data in array\n",
+ "for i in range(0,30,1):\n",
+ " sum=sum+marks[i]\n",
+ "avg=sum/30\n",
+ "print \"Average marks=%d\\n\" % (avg)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "98\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "88\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "97\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "96\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "87\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "89\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "80\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "86\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "95\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "99\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "90\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "100\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "99\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "94\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "85\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "84\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "92\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "91\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "87\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "76\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "75\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "96\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "83\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "80\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "93\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "79\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "70\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "78\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "85\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "86\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Average marks=87\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:261"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "num=[]\n",
+ "for i in range(40):\n",
+ " num.append(0)\n",
+ "for i in range(0,40,1): #be carefull about the size of the array\n",
+ " num[i]=i"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:261-262"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "def display(m):\n",
+ " print \"%d\" % (m)\n",
+ "marks=[55,65,75,56,78,78,90]\n",
+ "for i in range(0,7,1):\n",
+ " display(marks[i])"
+ ],
+ "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": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:263"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "def show(n):\n",
+ " print \"%d\" % (n)\n",
+ "def disp(n):\n",
+ " show(n) #calls show()\n",
+ "marks=[55,65,75,56,78,78,90]\n",
+ "for i in range(0,7,1):\n",
+ " disp(marks[i])"
+ ],
+ "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": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:263-264"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "i=3\n",
+ "j=1.5\n",
+ "k='c'\n",
+ "print \"Value of i=%d\\n\" % (i)\n",
+ "print \"Value of j=%f\\n\" % (j)\n",
+ "print \"Value of k=%c\\n\" % (k)\n",
+ "x=id(i)\n",
+ "y=id(j)\n",
+ "z=id(k)\n",
+ "print \"Original address in x=%u\\n\" % (x)\n",
+ "print \"Original address in y=%u\\n\" % (y)\n",
+ "print \"Original address in z=%u\\n\" % (z)\n",
+ "x+=1\n",
+ "y+=1\n",
+ "z+=1\n",
+ "print \"New address in x=%u\\n\" % (x)\n",
+ "print \"New address in y=%u\\n\" % (y)\n",
+ "print \"New address in z=%u\\n\" % (z)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Value of i=3\n",
+ "\n",
+ "Value of j=1.500000\n",
+ "\n",
+ "Value of k=c\n",
+ "\n",
+ "Original address in x=21147312\n",
+ "\n",
+ "Original address in y=88181272\n",
+ "\n",
+ "Original address in z=21311640\n",
+ "\n",
+ "New address in x=21147313\n",
+ "\n",
+ "New address in y=88181273\n",
+ "\n",
+ "New address in z=21311641\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:265"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "arr=[10,20,30,45,67,56,74]\n",
+ "i=id(arr[1])\n",
+ "j=id(arr[5])\n",
+ "print \"%d %d\\n\" % (j-i,arr[5]-arr[1])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "-432 36\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:266"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "arr=[10,20,36,72,45,36]\n",
+ "j=id(arr[4])\n",
+ "k=id(arr[0+4]) #didn't get any other way\n",
+ "if j==k:\n",
+ " print \"The two pointers point to the same location\\n\"\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",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:267"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "num=[24,34,12,44,56,17]\n",
+ "for i in range(0,6,1):\n",
+ " print \"element no.%d\" % (i)\n",
+ " print \"address=%u\\n\" % (id(num[i]))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "element no.0\n",
+ "address=21147060\n",
+ "\n",
+ "element no.1\n",
+ "address=21146940\n",
+ "\n",
+ "element no.2\n",
+ "address=21147204\n",
+ "\n",
+ "element no.3\n",
+ "address=21146820\n",
+ "\n",
+ "element no.4\n",
+ "address=21146676\n",
+ "\n",
+ "element no.5\n",
+ "address=21147144\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:268"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "num=[24,34,12,44,56,17]\n",
+ "for i in range(0,6,1):\n",
+ " print \"address=%u\" % (id(num[i]))\n",
+ " print \"element=%d\\n\" % (num[i])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "address=21147060\n",
+ "element=24\n",
+ "\n",
+ "address=21146940\n",
+ "element=34\n",
+ "\n",
+ "address=21147204\n",
+ "element=12\n",
+ "\n",
+ "address=21146820\n",
+ "element=44\n",
+ "\n",
+ "address=21146676\n",
+ "element=56\n",
+ "\n",
+ "address=21147144\n",
+ "element=17\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:268"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "num=[24,34,12,44,56,17]\n",
+ "j=id(num[0]) #assign address of zeroth element\n",
+ "for i in range(0,6,1):\n",
+ " print \"address=%u\" % (id(num[i]))\n",
+ " print \"element=%d\\n\" % (num[i]) "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "address=21147060\n",
+ "element=24\n",
+ "\n",
+ "address=21146940\n",
+ "element=34\n",
+ "\n",
+ "address=21147204\n",
+ "element=12\n",
+ "\n",
+ "address=21146820\n",
+ "element=44\n",
+ "\n",
+ "address=21146676\n",
+ "element=56\n",
+ "\n",
+ "address=21147144\n",
+ "element=17\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:270"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "def display(j,n):\n",
+ " for item in j:\n",
+ " print \"element=%d\\n\" % (item)\n",
+ "num=[24,34,12,44,56,17]\n",
+ "display(num,6)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "element=24\n",
+ "\n",
+ "element=34\n",
+ "\n",
+ "element=12\n",
+ "\n",
+ "element=44\n",
+ "\n",
+ "element=56\n",
+ "\n",
+ "element=17\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:271"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "num=[24,34,12,44,56,17]\n",
+ "for i in range(0,6,1):\n",
+ " print \"address=%u\" % (id(num[i]))\n",
+ " print \"element=%d %d\" % (num[i],num[i]) #no other way\n",
+ " print \"%d %d\\n\" % (num[i],num[i])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "address=21147060\n",
+ "element=24 24\n",
+ "24 24\n",
+ "\n",
+ "address=21146940\n",
+ "element=34 34\n",
+ "34 34\n",
+ "\n",
+ "address=21147204\n",
+ "element=12 12\n",
+ "12 12\n",
+ "\n",
+ "address=21146820\n",
+ "element=44 44\n",
+ "44 44\n",
+ "\n",
+ "address=21146676\n",
+ "element=56 56\n",
+ "56 56\n",
+ "\n",
+ "address=21147144\n",
+ "element=17 17\n",
+ "17 17\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:272"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "stud=[[0,0],[0,0],[0,0],[0,0]]\n",
+ "for i in range(0,4,1):\n",
+ " print \"Enter roll no. and marks\"\n",
+ " stud[i][0]=eval(raw_input())\n",
+ " stud[i][1]=eval(raw_input())\n",
+ "for i in range(0,4,1):\n",
+ " print \"%d %d\\n\" % (stud[i][0],stud[i][1])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter roll no. and marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1206030\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "100\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter roll no. and marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1206007\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "95\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter roll no. and marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1206034\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "97\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter roll no. and marks\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1206026\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "88\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1206030 100\n",
+ "\n",
+ "1206007 95\n",
+ "\n",
+ "1206034 97\n",
+ "\n",
+ "1206026 88\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:275"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "s=[[1234,56],[1212,33],[1434,80],[1312,78]]\n",
+ "for i in range(0,4,1):\n",
+ " print \"Address of %dth 1-D array=%u\\n\" % (i,id(s[i]))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Address of 0th 1-D array=88430392\n",
+ "\n",
+ "Address of 1th 1-D array=89514192\n",
+ "\n",
+ "Address of 2th 1-D array=88430712\n",
+ "\n",
+ "Address of 3th 1-D array=88430312\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:277"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "s=[[1234,56],[1212,33],[1434,80],[1312,78]]\n",
+ "for i in range(0,4,1):\n",
+ " for j in range(0,2,1):\n",
+ " print \"%d\" % (s[i][j])\n",
+ " print \"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1234\n",
+ "56\n",
+ "\n",
+ "\n",
+ "1212\n",
+ "33\n",
+ "\n",
+ "\n",
+ "1434\n",
+ "80\n",
+ "\n",
+ "\n",
+ "1312\n",
+ "78\n",
+ "\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:277-278"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "s=[[1234,56],[1212,33],[1434,80],[1312,78]]\n",
+ "p=[]\n",
+ "for i in range(2):\n",
+ " p.append(0)\n",
+ "for i in range(0,4,1):\n",
+ " p=s\n",
+ " pint=p\n",
+ " print \"\\n\"\n",
+ " for j in range(0,2,1):\n",
+ " print \"%d\" % (pint[i][j])"
+ ],
+ "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": 18
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:279-280"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "def display(q,row,col):\n",
+ " for item in q:\n",
+ " print item\n",
+ " print \"\\n\"\n",
+ "a=[[1,2,3,4],[5,6,7,8],[9,0,1,6]]\n",
+ "display(a,3,4)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "[1, 2, 3, 4]\n",
+ "[5, 6, 7, 8]\n",
+ "[9, 0, 1, 6]\n",
+ "\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:281-282"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "arr=[]\n",
+ "for i in range(4):\n",
+ " arr.append(0)\n",
+ "i=31\n",
+ "j=5\n",
+ "k=19\n",
+ "l=71\n",
+ "arr[0]=i\n",
+ "arr[1]=j\n",
+ "arr[2]=k\n",
+ "arr[3]=l\n",
+ "for m in range(0,4,1):\n",
+ " print \"%d\\n\" % (arr[m])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "31\n",
+ "\n",
+ "5\n",
+ "\n",
+ "19\n",
+ "\n",
+ "71\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:282"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "a=[0,1,2,3,4]\n",
+ "print \"%u %u %d\\n\" % (id(id(a[0])),id(a[0]),a[0])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "88518416 21147348 0\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 22
+ }
+ ],
+ "metadata": {}
+ }
+ ]
} \ No newline at end of file