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

Chapter 9: More Data Types and Operations

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

Example 9.1, Page Number: 182

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def code(str):\n", " print str\n", "\n", "#Calling function\n", "code(\"this is a test\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "this is a test\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 9.2, Page Number: 183

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def f(i):\n", " i=100\n", " print i\n", " \n", "#Variable declaration\n", "k=10\n", "\n", "#function call\n", "f(k)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "100\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 9.3, Page Number: 187

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable Declaration\n", "first=10 #global definition of first and last\n", "last=20\n", "\n", "#Result\n", "print first,last\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10 20\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 9.4, Page Number: 188

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "sum=0 \n", "count=0\n", "num=5 #Loop for user entries\n", "\n", "#compute a running average\n", "def r_avg(i):\n", " global sum,count\n", " sum=sum+i\n", " count+=1\n", " return sum/count\n", "\n", "\n", "while True:\n", " print \"Enter numbers(-1 to quit): \"\n", " num-=1 #User input\n", " if not(num==-1):\n", " print \"Running average is: \",r_avg(num) #Result\n", " if num<0:\n", " break" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter numbers(-1 to quit): \n", "Running average is: 4\n", "Enter numbers(-1 to quit): \n", "Running average is: 3\n", "Enter numbers(-1 to quit): \n", "Running average is: 3\n", "Enter numbers(-1 to quit): \n", "Running average is: 2\n", "Enter numbers(-1 to quit): \n", "Running average is: 2\n", "Enter numbers(-1 to quit): \n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 9.5, Page Number: 189

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "#Variable declaration\n", "sum=0 \n", "count=0\n", "num=10 #Loop for user entries\n", "\n", "#user input given: 9,8,7,6,-2,4,3,2,1,-1\n", "\n", "#compute a running average\n", "def r_avg(i):\n", " global sum,count\n", " sum=sum+i\n", " count+=1\n", " return sum/count\n", "\n", "def reset():\n", " global sum,count\n", " sum=0\n", " count=0\n", " \n", "while True:\n", " print \"Enter numbers(-1 to quit, -2 to reset): \"\n", " num-=1 #User input\n", " if num==5:\n", " num=-2\n", " if num==-2: #for reset\n", " num=4\n", " reset()\n", " continue\n", " if not(num==-1):\n", " print \"Running average is: \",r_avg(num) #Result\n", " else:\n", " break" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter numbers(-1 to quit, -2 to reset): \n", "Running average is: 9\n", "Enter numbers(-1 to quit, -2 to reset): \n", "Running average is: 8\n", "Enter numbers(-1 to quit, -2 to reset): \n", "Running average is: 8\n", "Enter numbers(-1 to quit, -2 to reset): \n", "Running average is: 7\n", "Enter numbers(-1 to quit, -2 to reset): \n", "Enter numbers(-1 to quit, -2 to reset): \n", "Running average is: 3\n", "Enter numbers(-1 to quit, -2 to reset): \n", "Running average is: 2\n", "Enter numbers(-1 to quit, -2 to reset): \n", "Running average is: 2\n", "Enter numbers(-1 to quit, -2 to reset): \n", "Running average is: 1\n", "Enter numbers(-1 to quit, -2 to reset): \n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 9.6, Page Number: 196

" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "name=[\"Jonathan\",\"Golden Delicious\",\"Red Delicious\",\"Winesap\",\n", " \"Cortland\",\"McIntosh\"]\n", "\n", "#enumeration type\n", "(Jonathan,Golden_Delicious,Red_Delicious,Winesap,Cortland,McIntosh) = (0,1,2,3,4,5)\n", "\n", "fruit=Jonathan\n", "print name[fruit]\n", "\n", "fruit = Winesap\n", "print name[fruit]\n", "\n", "fruit = McIntosh\n", "print name[fruit]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Jonathan\n", "Winesap\n", "McIntosh\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 9.7, Page Number: 198

" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "ch='j' #User input\n", "while True:\n", " #This statement turns off the 6th but.\n", " c=chr(ord(ch)&223) #ch is now uppercase\n", " print c\n", " if c=='Q':\n", " break \n", " else:\n", " ch = chr(ord(ch)+1) #incrementing for different user inputs\n", " \n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "J\n", "K\n", "L\n", "M\n", "N\n", "O\n", "P\n", "Q\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 9.8, Page Number: 200

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "#Variable declaration\n", "ch='J' #User input\n", "while True:\n", " #This statement turns off the 6th but.\n", " c=chr(ord(ch)|32) #ch is now uppercase\n", " print c\n", " if c=='q':\n", " break \n", " else:\n", " ch = chr(ord(ch)+1) #incrementing for different user inputs\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "j\n", "k\n", "l\n", "m\n", "n\n", "o\n", "p\n", "q\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 9.9, Page Number: 201

" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def disp_binary(u):\n", " t=128\n", " while t:\n", " if u&t:\n", " print 1,\n", " else:\n", " print 0,\n", " t=t/2\n", " print \"\"\n", " \n", "#Variable declaration\n", "u=99 #User Input\n", "\n", "print \"Here's the number in binary: \",\n", "disp_binary(u)\n", "\n", "print \"Here's the complement of th number: \",\n", "disp_binary(~u)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Here's the number in binary: 0 1 1 0 0 0 1 1 \n", "Here's the complement of th number: 1 0 0 1 1 1 0 0 \n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 9.10, Page Number: 202

" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def disp_binary(u):\n", " t=128\n", " while t:\n", " if u&t:\n", " print 1,\n", " else:\n", " print 0,\n", " t=t/2\n", " print \"\"\n", " \n", "#Variable dclaration\n", "i=1\n", "\n", "#Result\n", "for t in range(8):\n", " disp_binary(i)\n", " i=i<<1\n", "\n", "print\"\\n\"\n", "\n", "for t in range(8):\n", " i=i>>1\n", " disp_binary(i)\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 0 0 0 0 0 0 1 \n", "0 0 0 0 0 0 1 0 \n", "0 0 0 0 0 1 0 0 \n", "0 0 0 0 1 0 0 0 \n", "0 0 0 1 0 0 0 0 \n", "0 0 1 0 0 0 0 0 \n", "0 1 0 0 0 0 0 0 \n", "1 0 0 0 0 0 0 0 \n", "\n", "\n", "1 0 0 0 0 0 0 0 \n", "0 1 0 0 0 0 0 0 \n", "0 0 1 0 0 0 0 0 \n", "0 0 0 1 0 0 0 0 \n", "0 0 0 0 1 0 0 0 \n", "0 0 0 0 0 1 0 0 \n", "0 0 0 0 0 0 1 0 \n", "0 0 0 0 0 0 0 1 \n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 9.11, Page Number: 204

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def div_zero():\n", " print \"Cannot divide by zero.\"\n", " return 0\n", " \n", "#Variable declaration\n", "i=10 #User Input\n", "j=0\n", "\n", "#This statement prevents a divide by zero\n", "if j:\n", " result=i/j\n", "else:\n", " result=div_zero()\n", "\n", "#Result\n", "print \"Result: \",result" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Cannot divide by zero.\n", "Result: 0\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 9.12, Page Number: 206

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "j=10\n", "i=None\n", "\n", "j+=1\n", "j+100\n", "i=999+j\n", "\n", "#Result\n", "print i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1010\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 9.13, Page Number: 207

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "from ctypes import *\n", "\n", "#Variable declaration\n", "ch=c_char\n", "i=c_int\n", "\n", "print sizeof(ch), #size of char\n", "print sizeof(i), #size of int\n", "print sizeof(c_float), #size of float\n", "print sizeof(c_double) #size of double\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 4 4 8\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 9.14, Page Number: 209

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from ctypes import *\n", "\n", "#Variabke declaration \n", "i=c_int(20) #allocate memory for int\n", "p=pointer(i) #assign a pointer to the memory\n", "\n", "#Result\n", "print p[0] #proove that it works by displaying value\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "20\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 9.15, Page Number: 210

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from ctypes import *\n", "\n", "#Variable declaration \n", "i=c_int(99) #initialize with 99\n", "p=pointer(i) #assign a pointer to the value\n", "\n", "#Result\n", "print p[0] #displays 99\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "99\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 9.16, Page Number: 211

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from ctypes import *\n", "\n", "#Variable declaration \n", "i=c_double(10) \n", "p=pointer(i)\n", "\n", "#assign the values 100 to 109\n", "for i in range(10):\n", " p[i]=100.00+i\n", "\n", "#display the contents of the array\n", "for i in range(10):\n", " print p[i],\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "100.0 101.0 102.0 103.0 104.0 105.0 106.0 107.0 108.0 109.0\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 9.17, Page Number: 211

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "i=c_int() \n", "j=c_double()\n", "pi=pointer(i) #pointer to int\n", "pj=pointer(j) #pointer to double\n", "\n", "#Assign values using pointers\n", "pi[0]=10\n", "pj[0]=100.123\n", "\n", "#Result\n", "print pi[0],pj[0]\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10 100.123\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 9.18, Page Number: 212

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from ctypes import *\n", "\n", "#Variable declaration\n", "i=pointer(c_int())\n", "j=pointer(c_double())\n", "\n", "#Checking if i and j have been allocated memory addresses\n", "if not(id(i)):\n", " print \"Allocation Failure.\"\n", " \n", "if not(id(j)):\n", " print \"Allocation Failure.\" \n", "\n", "i[0]=10\n", "j[0]=100.123\n", "\n", "\n", "#Result\n", "print i[0],j[0]\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10 100.123\n" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }