{ "metadata": { "name": "", "signature": "sha256:ffbb5815913062b1375d894b2fe1e26db393d3f99c982f5934f82d0355e05d2e" }, "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", "\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", "\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", "\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", "\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", "\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", "\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", "\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", "\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", "\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", "\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", "\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", "\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", "\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", "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", "\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", "\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", "\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", "\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", "\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": {} } ] }