diff options
author | Jovina Dsouza | 2014-06-18 12:43:07 +0530 |
---|---|---|
committer | Jovina Dsouza | 2014-06-18 12:43:07 +0530 |
commit | 206d0358703aa05d5d7315900fe1d054c2817ddc (patch) | |
tree | f2403e29f3aded0caf7a2434ea50dd507f6545e2 /C++_from_the_Ground/Chapter_9(1).ipynb | |
parent | c6f0d6aeb95beaf41e4b679e78bb42c4ffe45a40 (diff) | |
download | Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.gz Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.bz2 Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.zip |
adding book
Diffstat (limited to 'C++_from_the_Ground/Chapter_9(1).ipynb')
-rw-r--r-- | C++_from_the_Ground/Chapter_9(1).ipynb | 387 |
1 files changed, 387 insertions, 0 deletions
diff --git a/C++_from_the_Ground/Chapter_9(1).ipynb b/C++_from_the_Ground/Chapter_9(1).ipynb new file mode 100644 index 00000000..b31c36a0 --- /dev/null +++ b/C++_from_the_Ground/Chapter_9(1).ipynb @@ -0,0 +1,387 @@ +{ + "metadata": { + "name": "Chapter 9" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h1>Chapter 9: More Data Types and Operations<h1>" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 9.1, Page Number: 182<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Printing a string'''\n#pyhton doesnt have constants, hence a normal object has been used\n\ndef code(str):\n print str\n\n#Calling function\ncode(\"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": "<h3>Example 9.2, Page Number: 183<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Implementing const references'''\n\ndef f(i):\n i=100\n print i\n \n#Variable declaration\nk=10\n\n#function call\nf(k)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "100\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 9.3, Page Number: 187<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Implementing extern in pyhton'''\n\n#Variable Declaration\nfirst=10 #global definition of first and last\nlast=20\n\n#Result\nprint 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": "<h3>Example 9.4, Page Number: 188<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Compute a running average of numbers entered by the user'''\n'''Implementing static variables in python'''\n\n#Variable declaration\nsum=0 \ncount=0\nnum=5 #Loop for user entries\n\n#compute a running average\ndef r_avg(i):\n global sum,count\n sum=sum+i\n count+=1\n return sum/count\n\n\nwhile 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): \nRunning average is: 4\nEnter numbers(-1 to quit): \nRunning average is: 3\nEnter numbers(-1 to quit): \nRunning average is: 3\nEnter numbers(-1 to quit): \nRunning average is: 2\nEnter numbers(-1 to quit): \nRunning average is: 2\nEnter numbers(-1 to quit): \n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 9.5, Page Number: 189<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Compute a running average of numbers entered by the user'''\n'''Implementing static variables in python'''\n\n#Variable declaration\nsum=0 \ncount=0\nnum=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\ndef r_avg(i):\n global sum,count\n sum=sum+i\n count+=1\n return sum/count\n\ndef reset():\n global sum,count\n sum=0\n count=0\n \nwhile 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): \nRunning average is: 9\nEnter numbers(-1 to quit, -2 to reset): \nRunning average is: 8\nEnter numbers(-1 to quit, -2 to reset): \nRunning average is: 8\nEnter numbers(-1 to quit, -2 to reset): \nRunning average is: 7\nEnter numbers(-1 to quit, -2 to reset): \nEnter numbers(-1 to quit, -2 to reset): \nRunning average is: 3\nEnter numbers(-1 to quit, -2 to reset): \nRunning average is: 2\nEnter numbers(-1 to quit, -2 to reset): \nRunning average is: 2\nEnter numbers(-1 to quit, -2 to reset): \nRunning average is: 1\nEnter numbers(-1 to quit, -2 to reset): \n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 9.6, Page Number: 196<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Implementation of enumeration in python'''\n\n#Array of strings that correspond to the enumeration\nname=[\"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\nfruit=Jonathan\nprint name[fruit]\n\nfruit = Winesap\nprint name[fruit]\n\nfruit = McIntosh\nprint name[fruit]", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Jonathan\nWinesap\nMcIntosh\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 9.7, Page Number: 198<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Convert to uppercase letter'''\n\n#Variable declaration\nch='j' #User input\nwhile 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\nK\nL\nM\nN\nO\nP\nQ\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 9.8, Page Number: 200<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Convert to lowercase letter'''\n\n#Variable declaration\nch='J' #User input\nwhile 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\nk\nl\nm\nn\no\np\nq\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 9.9, Page Number: 201<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Display the bits within a byte'''\n\n#function to display the bits\ndef 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\nu=99 #User Input\n\nprint \"Here's the number in binary: \",\ndisp_binary(u)\n\nprint \"Here's the complement of th number: \",\ndisp_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 \nHere's the complement of th number: 1 0 0 1 1 1 0 0 \n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 9.10, Page Number: 202<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Demonstrate bitshifting'''\n\n#unction to display the bits within a byte\ndef 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\ni=1\n\n#Result\nfor t in range(8):\n disp_binary(i)\n i=i<<1\n\nprint\"\\n\"\n\nfor 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 \n0 0 0 0 0 0 1 0 \n0 0 0 0 0 1 0 0 \n0 0 0 0 1 0 0 0 \n0 0 0 1 0 0 0 0 \n0 0 1 0 0 0 0 0 \n0 1 0 0 0 0 0 0 \n1 0 0 0 0 0 0 0 \n\n\n1 0 0 0 0 0 0 0 \n0 1 0 0 0 0 0 0 \n0 0 1 0 0 0 0 0 \n0 0 0 1 0 0 0 0 \n0 0 0 0 1 0 0 0 \n0 0 0 0 0 1 0 0 \n0 0 0 0 0 0 1 0 \n0 0 0 0 0 0 0 1 \n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 9.11, Page Number: 204<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''This program implements ? operator in python'''\n\ndef div_zero():\n print \"Cannot divide by zero.\"\n return 0\n \n#Variable declaration\ni=10 #User Input\nj=0\n\n#This statement prevents a divide by zero\nif j:\n result=i/j\nelse:\n result=div_zero()\n\n#Result\nprint \"Result: \",result", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Cannot divide by zero.\nResult: 0\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 9.12, Page Number: 206<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Implementation of the comma operator'''\n\n#Variable declaration\nj=10\ni=None\n\nj+=1\nj+100\ni=999+j\n\n#Result\nprint i", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "1010\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 9.13, Page Number: 207<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Demonstrate sizeof'''\n\nfrom ctypes import *\n\n#Variable declaration\nch=c_char\ni=c_int\n\nprint sizeof(ch), #size of char\nprint sizeof(i), #size of int\nprint sizeof(c_float), #size of float\nprint 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": "<h3>Example 9.14, Page Number: 209<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Example of pointers'''\n\nfrom ctypes import *\n\n#Variabke declaration \ni=c_int(20) #allocate memory for int\np=pointer(i) #assign a pointer to the memory\n\n#Result\nprint 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": "<h3>Example 9.15, Page Number: 210<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Imlementation of dynamic initialization in python'''\n\nfrom ctypes import *\n\n#Variable declaration \ni=c_int(99) #initialize with 99\np=pointer(i) #assign a pointer to the value\n\n#Result\nprint 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": "<h3>Example 9.16, Page Number: 211<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Imlementation of dynamic initialization in python'''\n\nfrom ctypes import *\n\n#Variable declaration \ni=c_double(10) \np=pointer(i)\n\n#assign the values 100 to 109\nfor i in range(10):\n p[i]=100.00+i\n\n#display the contents of the array\nfor 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": "<h3>Example 9.17, Page Number: 211<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Implementation of malloc int python'''\n\n#Variable declaration\ni=c_int() \nj=c_double()\npi=pointer(i) #pointer to int\npj=pointer(j) #pointer to double\n\n#Assign values using pointers\npi[0]=10\npj[0]=100.123\n\n#Result\nprint 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": "<h3>Example 9.18, Page Number: 212<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''Implementation of dynamic memory allocation'''\nfrom ctypes import *\n\n#Variable declaration\ni=pointer(c_int())\nj=pointer(c_double())\n\n#Checking if i and j have been allocated memory addresses\nif not(id(i)):\n print \"Allocation Failure.\"\n \nif not(id(j)):\n print \"Allocation Failure.\" \n\ni[0]=10\nj[0]=100.123\n\n\n#Result\nprint 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": {} + } + ] +}
\ No newline at end of file |