{ "metadata": { "name": "", "signature": "sha256:f41d93d043c68dcb6af847a40af38d47fc3a3d45c60675afb574707e38d804d9" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 5: Arrays ans Strings

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

Example 5.1, Page Number: 82

\n", " " ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "#Variable declaration\n", "sample = range(10) #Declares an array [0,1,2,3,4,5,6,7,8,9]\n", "\n", "#Displays the array\n", "for t in range(10):\n", " print sample[t],\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 1 2 3 4 5 6 7 8 9\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.2, Page Number: 83

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "import random\n", "\n", "#Variable declaration\n", "list = [] #List of integers\n", "\n", "for i in range(10):\n", " list.append(random.randint(0,1000)) #randomely assigning integers\n", " \n", "#Finding the minimum value\n", "min_value = list[0]\n", "\n", "for i in range(10):\n", " if min_value>list[i]:\n", " min_value=list[i]\n", "print \"minimum value: \",min_value #Result:Minimum value\n", "\n", "#Finding maximum value\n", "max_value = list[0]\n", "\n", "for i in range(10):\n", " if max_valueExample 5.3, Page Number: 85

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "\n", "import random\n", "\n", "#Variable declaration\n", "nums = []\n", "size = 10\n", "\n", "#Initializing list with random numbers\n", "for t in range(size):\n", " nums.append(random.randint(0,1000))\n", "\n", "#Displays original array\n", "print \"Original array is: \"\n", "print nums\n", "\n", "#Bubble Sort\n", "for a in range(size):\n", " for b in xrange(size-1,a,-1):\n", " if nums[b-1]>nums[b]:\n", " t=nums[b-1]\n", " nums[b-1]=nums[b]\n", " nums[b] = t\n", "\n", "#Display sorted array\n", "print \"Sorted array is: \"\n", "print nums\n", "\n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Original array is: \n", "[388, 661, 218, 595, 167, 46, 704, 140, 559, 428]\n", "Sorted array is: \n", "[46, 140, 167, 218, 388, 428, 559, 595, 661, 704]\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.4, Page Number: 87

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable decleration\n", "str = \"Hello\"\n", "\n", "#Result\n", "print \"Here is your string:\",\n", "print str\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Here is your string: Hello\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.5, Page Number: 88

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "\n", "print \"Enter a string: \"\n", "\n", " \n", "str = \"Hello\"\n", "\n", "#Result\n", "print \"Here is your string:\",\n", "print str\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a string: \n", "Here is your string: Hello\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.6, Page Number: 89

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "#Variable decleration\n", "s=str\n", "str = s #copying s into str\n", "\n", "#Result\n", "print str\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.7, Page Number: 89

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "#Variable decleration\n", "s1 = \"Hello\"\n", "s2 = \" There\"\n", "\n", "#Concatenation\n", "s1+=s2\n", "\n", "#Result\n", "print s1\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello There\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.8, Page Number: 90

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def password():\n", " print \"Enter password: \" #User input for password\n", " s= \"pass\"\n", " if s==\"password\":\n", " return True\n", " else:\n", " print \"Invalid password.\"\n", " return False\n", "\n", "#Result\n", "if password() :\n", " print \"Logged On.\"\n", "else:\n", " print \"Access denied\"\n", " \n", " \n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter password: \n", "Invalid password.\n", "Access denied\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.9, Page Number: 91

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import string\n", "\n", " s=raw_input(\"Enter a string: \") #User input of string;\n", " if s==\"quit\": #Sting comparison\n", " break\n", " \n", " \n", "\n", " \n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a string: ddc\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a string: hello\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a string: quit\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.10, Page Number: 91

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "#Variable declaration\n", "str = \"Hello\"\n", "\n", "#Result\n", "print \"Length is: \",len(str)\n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Length is: 5\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.11, Page Number: 92

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "str = \"Hello World\"\n", "\n", "#Reversing a String\n", "rev = str[::-1]\n", "\n", "#Result\n", "print rev\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "dlroW olleH\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.12, Page Number: 92

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "s1 = \"Hello\"\n", "s2 = \"there\"\n", "\n", "#Printing lengths\n", "print \"lengths: \",len(s1),' ',len(s2)\n", "\n", "#Comparing\n", "if(s1==s2):\n", " print \"The strings are equal\"\n", "else:\n", " print \"not equal\"\n", "\n", "#Concatenation\n", "s1+=s2\n", "print s1\n", "\n", "\n", "#Copying\n", "s1=s2\n", "\n", "#Result\n", "print s1,\"and\",s2,\" are now the same\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "lengths: 5 5\n", "not equal\n", "Hellothere\n", "there and there are now the same\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.13, Page Number: 93

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import string\n", "\n", "#Variable Initialization\n", "str= \"this is a test\"\n", "\n", "str=string.upper(str)\n", "\n", "#Result\n", "print str\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "THIS IS A TEST\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.14, Page Number: 94

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "arr=[] #The 2-D list\n", "new=[] #The nested list\n", "\n", "#Initializing the 2-D array\n", "for i in range(3):\n", " new=[]\n", " for j in range(4):\n", " new.append(i*4+j+1)\n", " arr.append(new)\n", "\n", "#Result\n", "for i in range(3):\n", " for j in range(4):\n", " print arr[i][j],\n", " print" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 2 3 4\n", "5 6 7 8\n", "9 10 11 12\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.15, Page Number: 98

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "#Variabe Initialzation\n", "sqrs= [[1,1],[2,4],[3,9],[4,16],[5,25],\n", " [6,36],[7,49],[8,64],[9,81],[10,100]] #Array storing the squares\n", "i=6 #User input of number whose square is to be looked up\n", "\n", "#Search for the number\n", "for j in range(10):\n", " if sqrs[j][0]==i:\n", " break\n", " \n", "#Result\n", "print \"The square of \",i,\" is \", sqrs[j][1]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The square of 6 is 36\n" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.16, Page Number: 99

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def f1():\n", " s=\"this is a test\" #initial s\n", " print s\n", " s=\"CHANGED\" #s is now changed \n", " print s\n", "\n", "#Calling the function twice\n", "f1()\n", "f1()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "this is a test\n", "CHANGED\n", "this is a test\n", "CHANGED\n" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.17, Page Number: 101

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "#Variable decleration\n", "text=[]\n", "str=['eat','play','work'] #user input of strings\n", "p=len(str)\n", "\n", "for t in range(p):\n", " print t,\":\"\n", " text.append(str[t]) #Here, user input taken from the list\n", " \n", "#Result; redisplay the strings\n", "for i in range(p):\n", " print text[i]\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 :\n", "1 :\n", "2 :\n", "eat\n", "play\n", "work\n" ] } ], "prompt_number": 18 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.18, Page Number: 103

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import random \n", "\n", "#Variable decleration\n", "name=[] #this list holds employee names\n", "wage=[] #their phone numbers\n", "phone=[] #hours worked per week\n", "hours=[] #wage\n", "num=0 #User choice\n", "\n", "#Menu \n", "def menu():\n", " global num #All options are chosen one by one\n", " print \"0.Quit.\"\n", " print \"1.Enter information\"\n", " print \"2.Report information\"\n", " print \"Choose one: \"\n", " num=int(input())\n", " return num #Return users selction\n", "\n", "#Enter information\n", "def enter():\n", " for i in range(10):\n", " n=raw_input(\"Enter your name: \")\n", " name.append(n)\n", " phone.append(int(input(\"Enter your phone number\")))\n", " hours.append(int(input(\"Enter number of hours worked: \")))\n", " wage.append(int(input(\"Enter wage: \")))\n", "\n", "#Display report\n", "def report():\n", " p=len(name)\n", " for i in range(p):\n", " print name[i],' ',phone[i]\n", " print \"Pay for the week: \",wage[i]*hours[i]\n", "\n", "\n", "while True:\n", " ch=menu() #get selection\n", " if ch==0:\n", " break\n", " elif ch==1:\n", " enter()\n", " elif ch==2:\n", " report()\n", " else:\n", " print \"Try again.\"\n", " if ch==0:\n", " break\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0.Quit.\n", "1.Enter information\n", "2.Report information\n", "Choose one: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your name: Anny\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your phone number987654321\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter number of hours worked: 10\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter wage: 50\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your name: Billy\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your phone number9456783652\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter number of hours worked: 10\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter wage: 50\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your name: Catherene\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your phone number9476836578\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter number of hours worked: 10\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter wage: 50\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your name: Dolly\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your phone number9831356748\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter number of hours worked: 15\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter wage: 50\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your name: Emily\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your phone number9576843721\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter number of hours worked: 15\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter wage: 40\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your name: Jack\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your phone number9485738567\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter number of hours worked: 10\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter wage: 45\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your name: Kevin\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your phone number9345678923\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter number of hours worked: 10\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter wage: 45\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your name: Lily\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your phone number9345672831\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter number of hours worked: 10\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter wage: 45\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your name: Monica\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your phone number9475867483\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter number of hours worked: 10\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter wage: 50\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your name: Irene\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your phone number5674356776\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter number of hours worked: 10\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter wage: 20\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "0.Quit.\n", "1.Enter information\n", "2.Report information\n", "Choose one: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Anny 987654321\n", "Pay for the week: 500\n", "Billy 9456783652\n", "Pay for the week: 500\n", "Catherene 9476836578\n", "Pay for the week: 500\n", "Dolly 9831356748\n", "Pay for the week: 750\n", "Emily 9576843721\n", "Pay for the week: 600\n", "Jack 9485738567\n", "Pay for the week: 450\n", "Kevin 9345678923\n", "Pay for the week: 450\n", "Lily 9345672831\n", "Pay for the week: 450\n", "Monica 9475867483\n", "Pay for the week: 500\n", "Irene 5674356776\n", "Pay for the week: 200\n", "0.Quit.\n", "1.Enter information\n", "2.Report information\n", "Choose one: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }