{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 10 - Arrays" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.1, page no. 206" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "defining an array\n", "note: no need to specify size of an array in python, it exapands automatically.\n", "\"\"\"\n", "\n", "testScore = [] #array defined" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.2, page no. 206" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "variable as size of array\n", "note: this won't give an error as mentioned in book as it will take numTests as\n", "an element of array instead of size of array, as there is no need of size of \n", "array in python.\n", "\"\"\"\n", "\n", "print \"Enter the number of test scores:\",\n", "numTests = int(raw_input())\n", "testScore = [numTests]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the number of test scores:" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "67\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.3, page no. 207" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "variable as size of array\n", "note: numTests will be taken as an element of array instead of size.\n", "Also, there in no constant in Python, this program will be same as previous one\n", "\"\"\"\n", "\n", "numTests = 3\n", "print \"Enter the number of test scores:\",\n", "numTests = int(raw_input())\n", "testScore = [numTests]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the number of test scores:" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "67\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.4, page no. 208" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "variable as size of array\n", "note: numTests will be taken as an element of array instead of size.\n", "Also, there in no constant in Python, this program will be same as previous one\n", "\"\"\"\n", "\n", "numTests = 3\n", "print \"Enter the number of test scores:\",\n", "num = int(raw_input())\n", "numTests = num\n", "testScore = [numTests]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the number of test scores:" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "78\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.5, page no. 213" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "intializing character array\n", "note: '\\0' will have different result in python than in c++ \n", "\"\"\"\n", "\n", "name = ['J', 'e', 'f', 'f', '\\0']\n", "print name" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['J', 'e', 'f', 'f', '\\x00']\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.5, page no. 213" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "intializing character array\n", "note: output will be different than that in book.\n", "\"\"\"\n", "\n", "name = ['J', 'e', 'f', 'f']\n", "print name" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['J', 'e', 'f', 'f']\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.7, page no. 216" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "assigning & displaying array values\n", "\"\"\"\n", "\n", "testScore = []\n", "print \"Enter test score #1: \",\n", "testScore.append(int(raw_input()))\n", "print \"Enter test score #2: \",\n", "testScore.append(int(raw_input()))\n", "print \"Enter test score #3: \",\n", "testScore.append(int(raw_input()))\n", "print \"Test score #1: \", testScore[0]\n", "print \"Test score #2: \", testScore[1]\n", "print \"Test score #3: \", testScore[2]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter test score #1: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "78\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter test score #2: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "88\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter test score #3: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "65\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Test score #1: 78\n", "Test score #2: 88\n", "Test score #3: 65\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.8, page no. 216" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "using 3 separate variables instead of array in previous example\n", "\"\"\"\n", "\n", "\n", "print \"Enter test score #1: \",\n", "testScore1 = int(raw_input())\n", "print \"Enter test score #2: \",\n", "testScore2 = int(raw_input())\n", "print \"Enter test score #3: \",\n", "testScore3 = int(raw_input())\n", "print \"Test score #1: \", testScore1\n", "print \"Test score #2: \", testScore2\n", "print \"Test score #3: \", testScore3" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter test score #1: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "65\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter test score #2: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "67\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter test score #3: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "88\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Test score #1: 65\n", "Test score #2: 67\n", "Test score #3: 88\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.9, page no. 217" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "looping for array\n", "\"\"\"\n", "\n", "testScore = []\n", "for i in range(3):\n", " print \"Enter testScore #\", i+1, \": \",\n", " testScore.append(int(raw_input()))\n", "\n", "for i in range(3):\n", " print \"Test Score #\", i+1, \": \", testScore[i]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter testScore # 1 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter testScore # 2 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter testScore # 3 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Test Score # 1 : 3\n", "Test Score # 2 : 4\n", "Test Score # 3 : 5\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.10, page no. 217" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "using constant for range of array\n", "\"\"\"\n", "\n", "MAX = 3\n", "\n", "testScore = []\n", "for i in range(MAX):\n", " print \"Enter testScore #\", i+1, \": \",\n", " testScore.append(int(raw_input()))\n", "\n", "for i in range(MAX):\n", " print \"Test Score #\", i+1, \": \", testScore[i]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter testScore # 1 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter testScore # 2 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter testScore # 3 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "7\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Test Score # 1 : 5\n", "Test Score # 2 : 6\n", "Test Score # 3 : 7\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.11, page no. 218" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "Common programming mistake for array\n", "note: the problem mentioned in the textbook won't occur in python as we are not\n", "specifying size of an array.\n", "\"\"\"\n", "\n", "MAX = 3\n", "\n", "testScore = []\n", "for i in range(MAX+1):\n", " print \"Enter testScore #\", i+1, \": \",\n", " testScore.append(int(raw_input()))\n", "\n", "for i in range(MAX+1):\n", " print \"Test Score #\", i+1, \": \", testScore[i]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter testScore # 1 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter testScore # 2 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter testScore # 3 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter testScore # 4 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Test Score # 1 : 4\n", "Test Score # 2 : 5\n", "Test Score # 3 : 6\n", "Test Score # 4 : 3\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.12, page no. 219" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "input element by index in array\n", "note: if the array is not initialized then this method won't work. append() may\n", "be used instead.\n", "\"\"\"\n", "\n", "MAX = 3\n", "grades = [0,0,0]\n", "\n", "for i in range(MAX):\n", " print \"Enter Garde #\", i+1, \": \",\n", " grades[i] = int(raw_input())" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter Garde # 1 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter Garde # 2 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter Garde # 3 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.13, page no. 219" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "character array\n", "\"\"\"\n", "\n", "name = ['J','e','f','f']\n", "print \"Enter your name: \",\n", "name = raw_input()\n", "print \"Your name is: \", name" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter your name: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Jeff\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Your name is: Jeff\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.14, page no. 220" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "cout Object with Numeric Arrays\n", "note: here the list itself will be printed instead of base address(as mentioned\n", "in textbook).\n", "\"\"\"\n", "\n", "MAX = 3\n", "testScore = []\n", "\n", "for i in range(MAX):\n", " print \"Enter test score #\", i + 1, \": \",\n", " testScore.append(int(raw_input()))\n", "print \"The test scores are: \", testScore" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter test score # 1 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter test score # 2 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter test score # 3 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The test scores are: [4, 5, 6]\n" ] } ], "prompt_number": 14 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.15, page no. 221" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "cin with numeric array\n", "note: there won't be any error in python as it is with the case of c++. It will\n", "just take testScore as a normal variable.\n", "\"\"\"\n", "\n", "testScore = []\n", "testScore = raw_input()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "45\n" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.16, page no. 222" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "cin with character array\n", "note: it will simply override the array and assign whatever value you enter to\n", "the name variable unlike c++(as mentioned in c++)\n", "\"\"\"\n", "\n", "name = ['J','e','f','f']\n", "print \"Enter your name: \",\n", "name = raw_input()\n", "print \"Your name is: \", name" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter your name: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Jeff\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Your name is: Jeff\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.17, page no. 222" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "name as string\n", "\"\"\"\n", "\n", "print \"Enter your name: \",\n", "name = raw_input()\n", "print \"Your name is: \", name" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter your name: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Jeff\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Your name is: Jeff\n" ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.18, page no. 223" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "getline()\n", "note: there is no getline() in python.\n", "\"\"\"\n", "\n", "print \"Enter your name: \",\n", "name = raw_input()\n", "print \"Your name is: \", name" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter your name: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Jeff\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Your name is: Jeff\n" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.19, page no. 225" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "passing array as function argument\n", "\"\"\"\n", "\n", "MAX = 3\n", "\n", "testScore = []\n", "for i in range(MAX):\n", " print \"Enter test score #\", i + 1, \": \",\n", " testScore.append(int(raw_input()))\n", "\n", "for i in range(MAX):\n", " print \"Test score #\", i + 1, \": \", testScore[i]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter test score # 1 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter test score # 2 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter test score # 3 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Test score # 1 : 4\n", "Test score # 2 : 5\n", "Test score # 3 : 6\n" ] } ], "prompt_number": 19 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 10.20, page no. 225" ] }, { "cell_type": "code", "collapsed": false, "input": [ "MAX = 3\n", "\n", "def assignValues(tests, num):\n", " for i in range(num):\n", " print \"Enter test score #\", i + 1, \": \",\n", " tests.append(int(raw_input()))\n", "\n", "def displayValues(scores, elems):\n", " for i in range(elems):\n", " print \"Test score #\", i + 1, \": \", scores[i]\n", "\n", "\n", "testScore = []\n", "assignValues(testScore, MAX)\n", "displayValues(testScore, MAX)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter test score # 1 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter test score # 2 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter test score # 3 : " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "7\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Test score # 1 : 5\n", "Test score # 2 : 6\n", "Test score # 3 : 7\n" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }