summaryrefslogtreecommitdiff
path: root/C_Programming_for_the_Absolute_Beginner/Chapter8.ipynb
diff options
context:
space:
mode:
authorhardythe12015-04-07 15:58:05 +0530
committerhardythe12015-04-07 15:58:05 +0530
commit92cca121f959c6616e3da431c1e2d23c4fa5e886 (patch)
tree205e68d0ce598ac5caca7de839a2934d746cce86 /C_Programming_for_the_Absolute_Beginner/Chapter8.ipynb
parentb14c13fcc6bb6d01c468805d612acb353ec168ac (diff)
downloadPython-Textbook-Companions-92cca121f959c6616e3da431c1e2d23c4fa5e886.tar.gz
Python-Textbook-Companions-92cca121f959c6616e3da431c1e2d23c4fa5e886.tar.bz2
Python-Textbook-Companions-92cca121f959c6616e3da431c1e2d23c4fa5e886.zip
added books
Diffstat (limited to 'C_Programming_for_the_Absolute_Beginner/Chapter8.ipynb')
-rwxr-xr-xC_Programming_for_the_Absolute_Beginner/Chapter8.ipynb703
1 files changed, 703 insertions, 0 deletions
diff --git a/C_Programming_for_the_Absolute_Beginner/Chapter8.ipynb b/C_Programming_for_the_Absolute_Beginner/Chapter8.ipynb
new file mode 100755
index 00000000..bca914f3
--- /dev/null
+++ b/C_Programming_for_the_Absolute_Beginner/Chapter8.ipynb
@@ -0,0 +1,703 @@
+{
+ "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 str3<str1:\n",
+ " print \"Character ! is greater than letter A\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "A\n",
+ "A\n",
+ "!\n",
+ "Letter A is equal to letter A\n",
+ "Letter A is greater than character !\n",
+ "Character ! is greater than letter A\n"
+ ]
+ }
+ ],
+ "prompt_number": 39
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Exmaple 8.14, Page No.197"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "str1=\"Analyzing strings with with the strstr() function\"\n",
+ "str2=\"ing\"\n",
+ "str3=\"xyz\"\n",
+ "\n",
+ "print str1\n",
+ "print str2\n",
+ "print str3\n",
+ "\n",
+ "if((str1.find(str2))!=-1):\n",
+ " print \"Str2 was found in str1\"\n",
+ "else: \n",
+ " print \"Str2 was not found in str1\"\n",
+ "\n",
+ "if((str1.find(str3))!=-1):\n",
+ " print \"Str3 was found in str1\"\n",
+ "else: \n",
+ " print \"Str3 was not found in str1\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Analyzing strings with with the strstr() function\n",
+ "ing\n",
+ "xyz\n",
+ "Str2 was found in str1\n",
+ "Str3 was not found in str1\n"
+ ]
+ }
+ ],
+ "prompt_number": 44
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Exmaple 8.15, Page No.198"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import time\n",
+ "import os\n",
+ "from array import array\n",
+ "\n",
+ "\n",
+ "def checkAnswer(string1,string2):\n",
+ " for x in range(len(string2)):\n",
+ " string2=string2.upper()\n",
+ " if(set(string2).intersection(string1.split())):\n",
+ " print \"Great job\"\n",
+ " else :\n",
+ " print \"Sorry no word found...\"\n",
+ " \n",
+ "starGame=[]\n",
+ "starGame.append(\"ADELANGUAGEFERVZOPIBMOU\")\n",
+ "starGame.append(\"ZBPOINTERSKLMLOOPMNOCOT\")\n",
+ "starGame.append(\"PODSTRINGGDIWHIEEICERLS\")\n",
+ "starGame.append(\"YVCPROGRAMMERWQKNULTHMD\")\n",
+ "starGame.append(\"UKUNIXFIMWXIZEQZINPUTEX\")\n",
+ "\n",
+ "displayed=0\n",
+ "startTime=0\n",
+ "os.system('cls')\n",
+ "print \"word find\"\n",
+ "startTime=int(round(time.time()*1000))\n",
+ "for x in range(0,5):\n",
+ " while ((startTime+3)>int(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": {}
+ }
+ ]
+} \ No newline at end of file