{ "metadata": { "name": "", "signature": "sha256:2192643aa60481aa37dedf877c77d457bede1bb6978dcdba9008684a80b17ad1" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 8 Strings" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8.1, Page No. 181" ] }, { "cell_type": "code", "collapsed": false, "input": [ "myString=\"MIKE\"\n", "print \"The pointer variable's value is: \", id(myString)\n", "print \"The pointer variable points to: \",myString\n", "print \"The memory locations for each character are::\"\n", "for x in range(0,4):\n", " print id(myString[x])," ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " The pointer variable's value is: 44562688\n", "The pointer variable points to: MIKE\n", "The memory locations for each character are::\n", "19548160 20083536 20086752 20285008\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8.2, Page No.183" ] }, { "cell_type": "code", "collapsed": false, "input": [ "color=raw_input(\"Enter your favorite color: \")\n", "print \"You entered: \",color" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your favorite color: Blue\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "You entered: Blue\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8.3, Page No.183" ] }, { "cell_type": "code", "collapsed": false, "input": [ "color=raw_input(\"Enter your favorite color: \")\n", "print \"You entered: \",color" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your favorite color: Black\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "You entered: Black\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8.4, Page No. 185" ] }, { "cell_type": "code", "collapsed": false, "input": [ "names = []\n", "names.append(\"Michael\")\n", "names.append(\"Sheila\")\n", "names.append(\"Spencer\")\n", "names.append(\"Hunter\")\n", "names.append(\"Kenya\")\n", "print \"\\nNames in pointer array of type char:\\n\"\n", "for i in range(5):\n", " print names[i]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Names in pointer array of type char:\n", "\n", "Michael\n", "Sheila\n", "Spencer\n", "Hunter\n", "Kenya\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8.5, Page No.186" ] }, { "cell_type": "code", "collapsed": false, "input": [ "colors = []\n", "print \"Enter 3 colors: \"\n", "for i in range(3):\n", " colors.append(raw_input())\n", "print \"Your entered: \"\n", "for i in range(3):\n", " print colors[i]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter 3 colors: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "blue\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "black\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "white\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Your entered: \n", "blue\n", "black\n", "white\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8.6, Page No.187" ] }, { "cell_type": "code", "collapsed": false, "input": [ "str1=\"123.79\"\n", "str2=\"55\"\n", "\n", "print \"String1 is:\\\"\",str1,\"\\\"\"\n", "print \"String2 is:\\\"\",str2,\"\\\"\"\n", "\n", "x=float(str1)\n", "y=int(str2)\n", "\n", "print \"String 1 converted to a float is \",round(x,2)\n", "print \"String 2 converted to an integer is \",y" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "String1 is:\" 123.79 \"\n", "String2 is:\" 55 \"\n", "String 1 converted to a float is 123.79\n", "String 2 converted to an integer is 55\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8.7, Page No.188" ] }, { "cell_type": "code", "collapsed": false, "input": [ "str1=\"37\"\n", "str2=\"20\"\n", "\n", "print \"String1 + String2 is \", str1+str2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "String1 + String2 is 3720\n" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8.8, Page No. 189" ] }, { "cell_type": "code", "collapsed": false, "input": [ "str1=\"37\"\n", "str2=\"20\"\n", "iResult=int(str1)+int(str2)\n", "print \"String1 + String2 is \", iResult" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "String1 + String2 is 57\n" ] } ], "prompt_number": 19 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8.9, Page No. 190" ] }, { "cell_type": "code", "collapsed": false, "input": [ "str1=\"Michael\"\n", "str2=\"Vine\"\n", "print \"The length of string1 is \",len(str1)\n", "print \"The length of string2 is \",len(str2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The length of string1 is 7\n", "The length of string2 is 4\n" ] } ], "prompt_number": 20 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8.10, Page No.191" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def convertL(char):\n", " char=char.lower()\n", " print \"The first name converted to lower case is: \",char\n", "def convertU(char):\n", " char=char.upper()\n", " print \"The first name converted to Upper case is: \",char\n", "name1=\"Michael\"\n", "name2=\"Vine\"\n", "\n", "convertL(name1)\n", "convertU(name2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The first name converted to lower case is: michael\n", "The first name converted to Upper case is: VINE\n" ] } ], "prompt_number": 32 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8.11, Page No.193" ] }, { "cell_type": "code", "collapsed": false, "input": [ "str2=\"C Language\"\n", "str1=str2\n", "print \"String 1 now contains \", str1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "String 1 now contains C Language\n" ] } ], "prompt_number": 33 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8.12, Page No.194" ] }, { "cell_type": "code", "collapsed": false, "input": [ "str1=\"Computer Science \"\n", "str2=\"is applied mathematics\"\n", "print str1+str2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Computer Science is applied mathematics\n" ] } ], "prompt_number": 34 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Exmaple 8.13, Page No.195" ] }, { "cell_type": "code", "collapsed": false, "input": [ "str1=\"A\"\n", "str2=\"A\"\n", "str3=\"!\"\n", "print str1\n", "print str2\n", "print str3\n", "if str1==str2:\n", " print \"Letter A is equal to letter A\"\n", "if str1>str3:\n", " print \"Letter A is greater than character !\"\n", "if str3int(round(time.time()*1000))):\n", " if displayed==0:\n", " print \"find a word in:\"\n", " print \"\",starGame[x]\n", " displayed=1\n", " os.system('cls')\n", " answer=raw_input(\"Enter word found:\")\n", " checkAnswer(starGame[x],answer)\n", " displayed=0\n", " startTime=int(round(time.time()*1000))\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "word find\n", "find a word in:\n", " ADELANGUAGEFERVZOPIBMOU\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter word found:U\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Sorry no word found...\n", "find a word in:\n", " ZBPOINTERSKLMLOOPMNOCOT\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter word found:T\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Sorry no word found...\n", "find a word in:\n", " PODSTRINGGDIWHIEEICERLS\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter word found:S\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Sorry no word found...\n", "find a word in:\n", " YVCPROGRAMMERWQKNULTHMD\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter word found:YV\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Sorry no word found...\n", "find a word in:\n", " UKUNIXFIMWXIZEQZINPUTEX\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter word found:UKU\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Sorry no word found...\n" ] } ], "prompt_number": 1 } ], "metadata": {} } ] }