diff options
Diffstat (limited to 'Programming_in_C/Chapter_16.ipynb')
-rw-r--r-- | Programming_in_C/Chapter_16.ipynb | 304 |
1 files changed, 304 insertions, 0 deletions
diff --git a/Programming_in_C/Chapter_16.ipynb b/Programming_in_C/Chapter_16.ipynb new file mode 100644 index 00000000..f6d06ea2 --- /dev/null +++ b/Programming_in_C/Chapter_16.ipynb @@ -0,0 +1,304 @@ +{ + "metadata": { + "name": "Chapter 16" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 16: Input and Output Operations in Python" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 16.1, Page number: 350" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#16.1.py\n", + "#Illustrating print function\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #Variable Declaration\n", + " c='X'\n", + " s=\"abcdefghijklmnopqrstuvwxyz\"\n", + " i=425\n", + " j=17\n", + " u=0xf179\n", + " l=75000\n", + " L=0x1234567812345678\n", + " f=12.978\n", + " d=-97.4583\n", + " cp=c\n", + " ip=i\n", + " \n", + "\n", + " #Print Integers\n", + " print(\"Integers:\\n\")\n", + " print(\"%i %o %x %u\" %(i,i,i,i))\n", + " print(\"%x %X %#x %#X\" %(i,i,i,i))\n", + " print(\"%+i % i %07i %.7i\" %(i,i,i, i))\n", + " print(\"%i %o %x %u\" %(j,j,j,j))\n", + " print(\"%i %o %x %u\" %(u,u,u,u))\n", + " print(\"%ld %lo %lx %lu\" %(l,l,l,l))\n", + " print(\"%li %lo %lx %lu\" %(L,L,L, L))\n", + "\n", + " #Print Floats & Doubles\n", + " print(\"\\nFloats and Doubles:\")\n", + " print(\"%f %e %g\"%( f, f, f))\n", + " print(\"%.2f %.2e\"%( f, f))\n", + " print(\"%.0f %.0e\"%( f, f))\n", + " print(\"%7.2f %7.2e\"%( f, f))\n", + " print(\"%f %e %g\"%( d, d, d))\n", + " print(\"%.*f\"%( 3, d))\n", + " print(\"%*.*f\" %(8, 2, d))\n", + "\n", + " #Print Characters\n", + " print(\"\\nCharacters:\")\n", + " print(\"%c\"%(c))\n", + " print(\"%3c%3c\"%( c, c))\n", + " print(\"{0:x} \".format(ord(c))) \n", + "\n", + " #Print Strings\n", + " print(\"\\nStrings:\")\n", + " print(\"%s\"%(s))\n", + " print(\"%.5s\"%(s))\n", + " print(\"%30s\"%(s))\n", + " print(\"%20.5s\"%(s))\n", + " print(\"%-20.5s\"%(s))\n", + "\n", + " #Print variables pointers\n", + " print(\"\\nPointers:\")\n", + " print(\"{0:x} {1:x}\\n\".format(int(ip),ord(cp)))\n", + "\n", + " print(\"This%n is fun.\")\n", + " print(\"c1 = %i, c2 = %i\\n\"%(4, 12))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Integers:\n", + "\n", + "425 651 1a9 425\n", + "1a9 1A9 0x1a9 0X1A9\n", + "+425 425 0000425 0000425\n", + "17 21 11 17\n", + "61817 170571 f179 61817\n", + "75000 222370 124f8 75000\n", + "1311768465173141112 110642547402215053170 1234567812345678 1311768465173141112\n", + "\n", + "Floats and Doubles:\n", + "12.978000 1.297800e+01 12.978\n", + "12.98 1.30e+01\n", + "13 1e+01\n", + " 12.98 1.30e+01\n", + "-97.458300 -9.745830e+01 -97.4583\n", + "-97.458\n", + " -97.46\n", + "\n", + "Characters:\n", + "X\n", + " X X\n", + "58 \n", + "\n", + "Strings:\n", + "abcdefghijklmnopqrstuvwxyz\n", + "abcde\n", + " abcdefghijklmnopqrstuvwxyz\n", + " abcde\n", + "abcde \n", + "\n", + "Pointers:\n", + "1a9 58\n", + "\n", + "This%n is fun.\n", + "c1 = 4, c2 = 12\n", + "\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 16.2, Page number: 362" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#16.2.py\n", + "#Copying Characters from Standard Input to Standard Output\n", + "\n", + "#Import system library\n", + "import sys\n", + "\n", + "#Main()\n", + "def main():\n", + " \n", + " while (True):\n", + " #c=raw_input() #User Input\n", + " c=\"this is a sample text\" #Dummy text\n", + " print(c); #Display Result\n", + " \n", + " #Un-comment this while executing from terminal/command line\n", + " #if(c==\"EOF\"): #Check Exit condition\n", + " sys.exit()\n", + " \n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "SystemExit", + "evalue": "", + "output_type": "pyerr", + "traceback": [ + "An exception has occurred, use %tb to see the full traceback.\n", + "\u001b[1;31mSystemExit\u001b[0m\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "this is a sample text\n" + ] + }, + { + "output_type": "stream", + "stream": "stderr", + "text": [ + "To exit: use 'exit', 'quit', or Ctrl-D." + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 16.3, Page number: 366" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#16.3.py\n", + "#Program to copy one file to another\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #Variable Declaration/ User Input\n", + " print(\"Enter name of the file to be copied: \")\n", + " inName=\"source.txt\"\n", + " #inName=raw_input()\n", + "\n", + " print(\"Enter name of the output file: \")\n", + " outName=\"target.txt\"\n", + " #outName=raw_input()\n", + "\n", + " \n", + "#Try to open a file for reading\n", + " try:\n", + " inn=open(inName,\"r\") \n", + " except:# Exception:\n", + " print(\"cant open {0} for reading\".format(inName))\n", + " sys.exit()\n", + "\n", + "#try to open a file for writing\n", + " try:\n", + " out=open(outName,\"w\") \n", + " except:# Exception:\n", + " print(\"cant open {0} for writing\".format(outName))\n", + " sys.exit()\n", + "\n", + " string=inn.read() #Read content from File-1\n", + " out.write(string) #Write content to File-2\n", + "\n", + "#Close Files\n", + " inn.close()\n", + " out.close()\n", + "\n", + " print(\"File has been copied.\\n\");\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "global name 'source' is not defined", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m/home/jovina/TBC/nalin/Stephen G Kochan--Programming in C/<ipython-input-3-388aacca0b5d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 40\u001b[0m \u001b[1;31m#Setting top level conditional script\u001b[0m\n\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 41\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0m__name__\u001b[0m\u001b[1;33m==\u001b[0m\u001b[1;34m'__main__'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 42\u001b[1;33m \u001b[0mmain\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 43\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m/home/jovina/TBC/nalin/Stephen G Kochan--Programming in C/<ipython-input-3-388aacca0b5d>\u001b[0m in \u001b[0;36mmain\u001b[1;34m()\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[0minn\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msource\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtxt\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"r\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 20\u001b[0m \u001b[1;32mexcept\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;31m# Exception:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 21\u001b[1;33m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"cant open {0} for reading\"\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msource\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtxt\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 22\u001b[0m \u001b[0msys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 23\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mNameError\u001b[0m: global name 'source' is not defined" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name of the file to be copied: \n", + "Enter name of the output file: \n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |