diff options
Diffstat (limited to 'Magnifying_C_by_Arpita_Goyal/Chapter_12_3.ipynb')
-rwxr-xr-x | Magnifying_C_by_Arpita_Goyal/Chapter_12_3.ipynb | 2137 |
1 files changed, 2137 insertions, 0 deletions
diff --git a/Magnifying_C_by_Arpita_Goyal/Chapter_12_3.ipynb b/Magnifying_C_by_Arpita_Goyal/Chapter_12_3.ipynb new file mode 100755 index 00000000..4fec3405 --- /dev/null +++ b/Magnifying_C_by_Arpita_Goyal/Chapter_12_3.ipynb @@ -0,0 +1,2137 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 12: Files" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.1, Page number: 428" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Calculation and result\n", + "fp = open('12.1.dat', 'w')\n", + "\n", + "for i in range (0, 11) :\n", + "\tfp.write('%d, %d \\n' % (i, i*i))\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.2, Page number: 429" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Calculation and result\n", + "fp = open('12.2.dat', 'w')\n", + "\n", + "for i in range (0, 11) :\n", + "\tfp.write('%d, %d \\n' % (i, i*i))\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.3, Page number: 430" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Calculation and result\n", + "fp = open('12.2.dat', 'a')\n", + "i = 11\n", + "fp.write('%d, %d \\n' % (i, i*i))\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.4, Page number: 432" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Calculation and result\n", + "with open ('12.2.dat') as fp:\n", + "\tfor line in fp :\n", + "\t\tprint line\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0, 0 \n", + "\n", + "1, 1 \n", + "\n", + "2, 4 \n", + "\n", + "3, 9 \n", + "\n", + "4, 16 \n", + "\n", + "5, 25 \n", + "\n", + "6, 36 \n", + "\n", + "7, 49 \n", + "\n", + "8, 64 \n", + "\n", + "9, 81 \n", + "\n", + "10, 100 \n", + "\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.5, Page number: 434" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Calculation and result\n", + "print ('Data input ')\n", + "f1 = open('12.5.dat', 'w')\n", + "f1.write('You are learning Files in Python language ')\n", + "f1.close()\n", + "\n", + "print ('Data output ')\n", + "with open ('12.5.dat') as f1:\n", + "\tfor line in f1 :\n", + "\t\tprint line\n", + "f1.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data input \n", + "Data output \n", + "You are learning Files in Python language \n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.6, Page number: 435" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Calculation and result\n", + "print ('Contents of the data file ')\n", + "f1 = open('12.6.dat', 'w')\n", + "for i in range (0, 10) :\n", + "\tnumber = int(raw_input())\n", + "\tf1.write('%d\\n' % number)\n", + "f1.close()\n", + "\n", + "f1 = open('12.6.dat', 'r')\n", + "f2 = open('12.6o.dat', 'w')\n", + "f3 = open('12.6e.dat', 'w')\n", + "\n", + "num = [0] * 10\n", + "with open ('12.6.dat') as f1 :\n", + "\tnum = f1.read().splitlines()\n", + "\t\n", + "for i in range (0, 10) :\n", + "\tif (int(num[i]) % 2) != 0 :\n", + "\t\tf2.write('%s \\n' % (num[i]))\n", + "\telse :\n", + "\t\tf3.write('%s \\n' % (num[i]))\n", + "\t\n", + "f1.close()\n", + "f2.close()\n", + "f3.close()\n", + "\n", + "print ('Contents of the odd file ')\n", + "with open ('12.6o.dat') as f2:\n", + "\tfor line in f2 :\n", + "\t\tprint line\n", + "\n", + "print ('Contents of the even file ')\n", + "with open ('12.6e.dat') as f3:\n", + "\tfor line in f3 :\n", + "\t\tprint line\n", + "\n", + "f2.close()\n", + "f3.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Contents of the data file \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Contents of the odd file \n", + "1 \n", + "\n", + "3 \n", + "\n", + "5 \n", + "\n", + "7 \n", + "\n", + "9 \n", + "\n", + "Contents of the even file \n", + "2 \n", + "\n", + "4 \n", + "\n", + "6 \n", + "\n", + "8 \n", + "\n", + "10 \n", + "\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.7, Page number: 438" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Calculation and result\n", + "filename = raw_input('Input filename: ')\n", + "fp = open(filename, 'w')\n", + "\n", + "print ('Input inventory data ')\n", + "print ('\\nItemName number price quantity ')\n", + "\n", + "data = [[0 for y in xrange(5)] for x in xrange(3)]\n", + "for i in range (0, 3) :\n", + "\tfor j in range (0, 4) :\n", + "\t\tdata[i][j] = raw_input('')\n", + "\t\tfp.write('%s ' % data[i][j]),\n", + "\tfp.write('\\n')\n", + "\tprint\n", + "fp.close()\n", + "\n", + "fp = open(filename, 'r')\n", + "\n", + "for i in range (0, 3) :\n", + "\tfor j in range (2, 3) :\n", + "\t\tdata[i][j+2] = float(data[i][j]) * int(data[i][j+1])\n", + "\n", + "print ('ItemName number price quantity value ')\n", + "for i in range (0, 3) :\n", + "\tfor j in range (0, 5) :\n", + "\t\tprint ('%8s' % data[i][j]),\n", + "\tprint" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input filename: 12.7.dat\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input inventory data \n", + "\n", + "ItemName number price quantity \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Monitor\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "11\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3005.50\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Mouse\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "22\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "500.50\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Keyboard\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "33\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "750.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "ItemName number price quantity value \n", + " Monitor 11 3005.50 10 30055.0\n", + " Mouse 22 500.50 5 2502.5\n", + "Keyboard 33 750.00 10 7500.0\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.8, Page number: 440" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Calculation and result\n", + "with open ('12.5.dat') as fp:\n", + "\tfor line in fp :\n", + "\t\tprint line\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You are learning Files in Python language \n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.9, Page number: 441" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from sys import argv\n", + "\n", + "# Calculation and result\n", + "script, filename = argv\n", + "file = open(filename)\n", + "print file.read()\n", + "print ('\\nFile displayed successfully.....')" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%run 12.9.py 12.1.dat" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0, 0 \n", + "1, 1 \n", + "2, 4 \n", + "3, 9 \n", + "4, 16 \n", + "5, 25 \n", + "6, 36 \n", + "7, 49 \n", + "8, 64 \n", + "9, 81 \n", + "10, 100 \n", + "\n", + "\n", + "File displayed successfully.....\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.10, Page number: 444" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Calculation and result\n", + "class person (object) :\n", + "\tdef __init__(self, name = None, age = 0):\n", + "\t\tself.name = 0\n", + "\t\tself.age = 0\n", + "\t\t\n", + "data = [person() for i in range (5)]\n", + "\n", + "fp = open('12.10.dat','w')\n", + "\n", + "for i in range (0, 5) :\n", + "\tdata[i].name = raw_input('\\nEnter name : ')\n", + "\tdata[i].age = int(raw_input('Enter age : '))\n", + "\t\n", + "for i in range (0, 5) :\n", + "\tfp.write('%s %d \\n' % (data[i].name, data[i].age))\n", + "\n", + "print ('\\n...Records are written to a file...')\n", + "print ('...Displaying records from a file...')\n", + "\n", + "for i in range (0, 5) :\n", + "\tprint ('\\nName : %s ' % data[i].name)\n", + "\tprint ('Age : %d ' % data[i].age)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter name : Dhiraj Kumar\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter age : 23\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter name : Mohasin Khan\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter age : 45\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter name : Suresh Waghmare\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter age : 55\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter name : Anand Rao\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter age : 34\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter name : Sunil Deshpande\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter age : 23\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "...Records are written to a file...\n", + "...Displaying records from a file...\n", + "\n", + "Name : Dhiraj Kumar \n", + "Age : 23 \n", + "\n", + "Name : Mohasin Khan \n", + "Age : 45 \n", + "\n", + "Name : Suresh Waghmare \n", + "Age : 55 \n", + "\n", + "Name : Anand Rao \n", + "Age : 34 \n", + "\n", + "Name : Sunil Deshpande \n", + "Age : 23 \n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.11, Page number: 445" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Calculation and result\n", + "class record (object) :\n", + "\tdef __init__(self, name = None, age = 0) :\n", + "\t\tself.name = 0\n", + "\t\tself.age = 0\n", + "\t\t\n", + "def record_write (data, filename) :\n", + "\tfilep = open(filename, 'ab')\n", + "\tdata.name = raw_input('Enter name : ')\n", + "\tdata.age = int(raw_input('Enter age : '))\n", + "\tfilep.write('%s %d\\n' % (data.name, data.age))\n", + "\t\n", + "def record_readall (filename) :\n", + "\tprint\n", + "\twith open (filename) as filep :\n", + "\t\tfor line in filep :\n", + "\t\t\tprint line\n", + "\n", + "data = record ()\n", + "filename = '12.11.dat'\n", + "\n", + "condition = True\n", + "while condition :\n", + "\tenter = raw_input('\\nAdd record (y/n) ? ')\n", + "\tif enter == 'y' or enter == 'Y' :\n", + "\t\trecord_write (data, filename)\n", + "\telse :\n", + "\t\tcondition = False\n", + "\t\tbreak\n", + "\n", + "record_readall (filename)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Add record (y/n) ? y\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name : Mohan Pande\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter age : 34\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Add record (y/n) ? y\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name : Nitin Dighe\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter age : 32\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Add record (y/n) ? n\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Mohan Pande 34\n", + "\n", + "Nitin Dighe 32\n", + "\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.12, Page number: 449" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Calculation and result\n", + "fp = open('12.12.txt','r')\n", + "fp.seek(0, 0)\n", + "line = fp.readline()\n", + "print ('Read Line: %s ' % line)\n", + "\n", + "fp.seek(17, 0)\n", + "line = fp.readline()\n", + "print ('Read Line: %s ' % line)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Read Line: This is 1st line\n", + " \n", + "Read Line: This is 2nd line\n", + " \n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.13, Page number: 449" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import os\n", + "\n", + "# Calculation and result\n", + "statinfo = os.stat('12.13.txt')\n", + "print ('Filesize of \"12.13.txt\" is %d bytes ' % statinfo.st_size)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Filesize of \"12.13.txt\" is 15 bytes \n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.14, Page number: 455" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Calculation and result\n", + "nc = nlines = 0\n", + "\n", + "filename = raw_input('Enter file name: ')\n", + "with open(filename, 'r') as fp :\n", + " for line in fp :\n", + " nlines += 1\n", + " nc += len(line)\n", + "fp.close()\n", + "\n", + "print ('There are %d characters in %s ' % (nc, filename))\n", + "print ('There are %d lines ' % nlines)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter file name: 12.14.txt\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "There are 55 characters in 12.14.txt \n", + "There are 1 lines \n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.15, Page number: 456" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Calculation and result\n", + "linecount = 0\n", + "filename = raw_input('Enter file name: ')\n", + "fp = open(filename, 'r')\n", + "for line in fp :\n", + "\tprint line\n", + "\tlinecount += 1\n", + "\tif linecount % 20 == 0 :\n", + "\t\traw_input(\"[Press Return to continue, Q to quit]\")\n", + "\t\tif raw_input() == 'Q' :\n", + "\t\t\texit()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter file name: 12.15.txt\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n", + "\n", + "2\n", + "\n", + "3\n", + "\n", + "4\n", + "\n", + "5\n", + "\n", + "6\n", + "\n", + "7\n", + "\n", + "8\n", + "\n", + "9\n", + "\n", + "10\n", + "\n", + "11\n", + "\n", + "12\n", + "\n", + "13\n", + "\n", + "14\n", + "\n", + "15\n", + "\n", + "16\n", + "\n", + "17\n", + "\n", + "18\n", + "\n", + "19\n", + "\n", + "20\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "[Press Return to continue, Q to quit]Q\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "21\n", + "\n", + "22\n", + "\n", + "23\n", + "\n", + "24\n", + "\n", + "25\n", + "\n", + "26\n", + "\n", + "27\n", + "\n", + "28\n", + "\n", + "29\n", + "\n", + "30\n", + "\n", + "31\n", + "\n", + "32\n", + "\n", + "33\n", + "\n", + "34\n", + "\n", + "35\n", + "\n", + "36\n", + "\n", + "37\n", + "\n", + "38\n", + "\n", + "39\n", + "\n", + "40\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "[Press Return to continue, Q to quit]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "41\n", + "\n", + "42\n", + "\n", + "43\n", + "\n", + "44\n", + "\n", + "45\n", + "\n", + "46\n", + "\n", + "47\n", + "\n", + "48\n", + "\n", + "49\n", + "\n", + "50\n", + "\n", + "51\n", + "\n", + "52\n", + "\n", + "53\n", + "\n", + "54\n", + "\n", + "55\n", + "\n", + "56\n", + "\n", + "57\n", + "\n", + "58\n", + "\n", + "59\n", + "\n", + "60\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "[Press Return to continue, Q to quit]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "61\n", + "\n", + "62\n", + "\n", + "63\n", + "\n", + "64\n", + "\n", + "65\n", + "\n", + "66\n", + "\n", + "67\n", + "\n", + "68\n", + "\n", + "69\n", + "\n", + "70\n", + "\n", + "71\n", + "\n", + "72\n", + "\n", + "73\n", + "\n", + "74\n", + "\n", + "75\n", + "\n", + "76\n", + "\n", + "77\n", + "\n", + "78\n", + "\n", + "79\n", + "\n", + "80\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "[Press Return to continue, Q to quit]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "81\n", + "\n", + "82\n", + "\n", + "83\n", + "\n", + "84\n", + "\n", + "85\n", + "\n", + "86\n", + "\n", + "87\n", + "\n", + "88\n", + "\n", + "89\n", + "\n", + "90\n", + "\n", + "91\n", + "\n", + "92\n", + "\n", + "93\n", + "\n", + "94\n", + "\n", + "95\n", + "\n", + "96\n", + "\n", + "97\n", + "\n", + "98\n", + "\n", + "99\n", + "\n", + "100\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "[Press Return to continue, Q to quit]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.16, Page number: 457" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Calculation and result\n", + "fname1 = raw_input('Enter source file: ')\n", + "fname2 = raw_input('Enter destination file: ')\n", + "\n", + "with open(fname1) as fp1 :\n", + "\twith open(fname2,'w') as fp2 : \n", + "\t\tfor line in fp1 :\n", + "\t\t\tfp2.write(line)\n", + "\n", + "print ('Files successfully copied ')" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter source file: 12.16in.txt\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter destination file: 12.16out.txt\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Files successfully copied \n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.17, Page number: 458" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Calculation and result\n", + "total = count = 0\n", + "filenameIn = raw_input('Please enter an input filename: ')\n", + "filenameOut = raw_input('Please enter an output filename: ')\n", + "\n", + "print ('Opening %s for reading is OK.' % filenameIn)\n", + "print ('Opening %s for writing is OK.' % filenameOut)\n", + "print ('Calculate the total...')\n", + "\n", + "for i in open(filenameIn) :\n", + "\tcount += 1\n", + "\ttotal += int(i.strip())\n", + "\t\n", + "print ('Calculate the average...')\n", + "fileptrOut = open(filenameOut,'w')\n", + "fileptrOut.write('Average of %d numbers = %f ' % (count, (total/count)))\n", + "\n", + "print ('Check also your %s file content ' % filenameOut)\n", + "fileptrOut.close()\n", + "print ('\"%s\" closed successfully ' % filenameIn)\n", + "print ('\"%s\" closed successfully ' % filenameOut)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter an input filename: 12.17in.dat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter an output filename: 12.17out.dat\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Opening 12.17in.dat for reading is OK.\n", + "Opening 12.17out.dat for writing is OK.\n", + "Calculate the total...\n", + "Calculate the average...\n", + "Check also your 12.17out.dat file content \n", + "\"12.17in.dat\" closed successfully \n", + "\"12.17out.dat\" closed successfully \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.18, Page number: 460" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Structure declaration\n", + "class STUDENT (object) :\n", + "\tdef __init__(self, fname=None, lname=None, id=None, quiz1=None, quiz2=None, quiz3=None, quiz4=None, exam=None) :\n", + "\t\tself.fname = fname\n", + "\t\tself.lname = lname\n", + "\t\tself.id = id\n", + "\t\tself.quiz1 = quiz1\n", + "\t\tself.quiz2 = quiz2\n", + "\t\tself.quiz3 = quiz3\n", + "\t\tself.quiz4 = quiz4\n", + "\t\tself.exam = exam\n", + "\t\n", + "def findLow (students, x, c) :\n", + "\tif x == 0 :\n", + "\t\tsmall = students[0].quiz1\n", + "\t\tfor i in range (1, c) :\n", + "\t\t\tif small >= students[i].quiz1 and students[i].quiz1 > 0 :\n", + "\t\t\t\tsmall = students[i].quiz1\n", + "\t\treturn small\n", + "\t\t\n", + "\telif x == 1 :\n", + "\t\tsmall = students[0].quiz2\n", + "\t\tfor i in range (1, c) :\n", + "\t\t\tif small >= students[i].quiz2 and students[i].quiz2 > 0 :\n", + "\t\t\t\tsmall = students[i].quiz2\n", + "\t\treturn small\n", + "\t\t\n", + "\telif x == 2 :\n", + "\t\tsmall = students[0].quiz3\n", + "\t\tfor i in range (1, c) :\n", + "\t\t\tif small >= students[i].quiz3 and students[i].quiz3 > 0 :\n", + "\t\t\t\tsmall = students[i].quiz3\n", + "\t\treturn small\n", + "\n", + "\telif x == 3 :\n", + "\t\tsmall = students[0].quiz4\n", + "\t\tfor i in range (1, c) :\n", + "\t\t\tif small >= students[i].quiz4 and students[i].quiz4 > 0 :\n", + "\t\t\t\tsmall = students[i].quiz4\n", + "\t\treturn small\n", + "\t\t\n", + "\telif x == 4 :\n", + "\t\tsmall = students[0].exam\n", + "\t\tfor i in range (1, c) :\n", + "\t\t\tif small >= students[i].exam and students[i].exam > 0 :\n", + "\t\t\t\tsmall = students[i].exam\n", + "\t\treturn small\n", + "\treturn 0\n", + "\n", + "def findHigh (students, x, c) :\n", + "\tif x == 0 :\n", + "\t\tbig = students[0].quiz1\n", + "\t\tfor i in range (1, c) :\n", + "\t\t\tif big <= students[i].quiz1 :\n", + "\t\t\t\tbig = students[i].quiz1\n", + "\t\treturn big\n", + "\t\t\n", + "\telif x == 1 :\n", + "\t\tbig = students[0].quiz2\n", + "\t\tfor i in range (1, c) :\n", + "\t\t\tif big <= students[i].quiz2 :\n", + "\t\t\t\tbig = students[i].quiz2\n", + "\t\treturn big\n", + "\t\t\n", + "\telif x == 2 :\n", + "\t\tbig = students[0].quiz3\n", + "\t\tfor i in range (1, c) :\n", + "\t\t\tif big <= students[i].quiz3 :\n", + "\t\t\t\tbig = students[i].quiz3\n", + "\t\treturn big\n", + "\n", + "\telif x == 3 :\n", + "\t\tbig = students[0].quiz4\n", + "\t\tfor i in range (1, c) :\n", + "\t\t\tif big <= students[i].quiz4 :\n", + "\t\t\t\tbig = students[i].quiz4\n", + "\t\treturn big\n", + "\t\t\n", + "\telif x == 4 :\n", + "\t\tbig = students[0].exam\n", + "\t\tfor i in range (1, c) :\n", + "\t\t\tif big <= students[i].exam :\n", + "\t\t\t\tbig = students[i].exam\n", + "\t\treturn big\n", + "\treturn 0\n", + "\n", + "\n", + "students = [STUDENT() for i in range (5)]\n", + "summ = avg = low = high = [0] * 5\n", + "cnt = 0\n", + "\n", + "print ('--- CLASS INFO --- \\n')\n", + "print ('Name\\t\\t Id\\tQuiz1 Quiz2 Quiz3 Quiz4 Exam \\n')\n", + "\n", + "with open('12.18.txt') as fpr :\n", + "\tfor line in fpr :\n", + "\t\ti = 0\n", + "\t\tstudents[i].fname, students[i].lname, students[i].id, students[i].quiz1, students[i].quiz2, students[i].quiz3, students[i].quiz4, students[i].exam = line.split()\n", + "\t\tstudents[i].id = int(students[i].id)\n", + "\t\tstudents[i].quiz1 = int(students[i].quiz1)\n", + "\t\tstudents[i].quiz2 = int(students[i].quiz2)\n", + "\t\tstudents[i].quiz3 = int(students[i].quiz3)\n", + "\t\tstudents[i].quiz4 = int(students[i].quiz4)\n", + "\t\tstudents[i].exam = int(students[i].exam)\n", + "\t\t\n", + "\t\tprint ('%-s %-s\\t %-4d\t%-3d\t%-3d\t%-3d\t%-3d\t%-3d' % (students[i].fname, students[i].lname, students[i].id, students[i].quiz1, students[i].quiz2, students[i].quiz3, students[i].quiz4, students[i].exam)) \n", + "\t\t\n", + "\t\tsumm[0] += int(students[i].quiz1)\n", + "\t\tsumm[1] += int(students[i].quiz2)\n", + "\t\tsumm[2] += int(students[i].quiz3)\t\t\n", + "\t\tsumm[3] += int(students[i].quiz4)\n", + "\t\tsumm[4] += int(students[i].exam)\n", + "\t\ti = i+1\n", + "\t\tcnt = cnt+1\n", + "\n", + "print ('\\nSTATISTICS')\n", + "print ('\\t\\tQuiz1\tQuiz2\tQuiz3\tQuiz4\tExam\\n')\n", + "print ('Average:\\t'),\n", + "for i in range (0, 5) :\n", + "\tavg[i] = float(summ[i])/cnt\n", + "\tprint ('%-5.1f \\t' % avg[i]),\n", + "print\n", + "\n", + "print ('Lowest:\\t\\t'),\n", + "for i in range (0, 5) :\n", + "\tlow[i] = findLow (students, i, cnt)\n", + "\thigh[i] = findHigh (students, i, cnt)\n", + "\t\n", + "for i in range (0, 5) :\n", + "\tprint ('%-5d \\t' % low[i]),\n", + "print\n", + "\t\n", + "print ('Highest:\\t'),\n", + "for i in range (0, 5) :\n", + "\tprint ('%-5d \\t' % high[i]),\n", + "print\n", + "\n", + "print ('\\n --- END OF REPORT --- \\n')" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "--- CLASS INFO --- \n", + "\n", + "Name\t\t Id\tQuiz1 Quiz2 Quiz3 Quiz4 Exam \n", + "\n", + "anand mane\t 11 \t10 \t3 \t4 \t5 \t80 \n", + "mandar patil\t 12 \t4 \t5 \t6 \t7 \t50 \n", + "kishoe dhane\t 13 \t4 \t5 \t6 \t8 \t55 \n", + "\n", + "STATISTICS\n", + "\t\tQuiz1\tQuiz2\tQuiz3\tQuiz4\tExam\n", + "\n", + "Average:\t6.0 \t4.3 \t5.3 \t6.7 \t61.7 \t\n", + "Lowest:\t\t4 \t5 \t6 \t8 \t55 \t\n", + "Highest:\t4 \t5 \t6 \t8 \t55 \t\n", + "\n", + " --- END OF REPORT --- \n", + "\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12.19, Page number: 466" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import sys\n", + "\n", + "# Structure declaration\n", + "class Employee (object) :\n", + "\tdef __init__(self, fname=None, lname=None, sub_taken=None, last_edu=None, join_date=None, id=None, age=None, bsal=None) :\n", + "\t\tself.fname = fname\n", + "\t\tself.lname = lname\n", + "\t\tself.sub_taken = sub_taken\n", + "\t\tself.last_edu = last_edu\n", + "\t\tself.join_date = join_date\n", + "\t\tself.id = id\n", + "\t\tself.age = age\n", + "\t\tself.bsal = bsal\n", + "\t\n", + "emp = Employee ()\n", + "fp = open('12.19.dat','ab+')\n", + "recsize = sys.getsizeof(emp)\n", + "\n", + "condition = True\n", + "while condition :\n", + "\tprint ('\\n1.Add Records \\\n", + "\t\t\t \\n2.Delete Records \\\n", + "\t\t\t \\n3.Modify Records \\\n", + "\t\t\t \\n4.List Records \\\n", + "\t\t\t \\n5.Exit')\n", + "\tchoice = int(raw_input('\\nEnter your choice: '))\n", + "\t\n", + "\tif choice == 1 :\n", + "\t\tanother = 'Y'\n", + "\t\twhile another == 'Y' or another == 'y' :\n", + "\t\t\tfp = open('12.19.dat','ab+')\n", + "\t\t\temp.fname = raw_input('Enter the first name: ')\n", + "\t\t\temp.lname = raw_input('Enter the last name: ')\n", + "\t\t\temp.age = int(raw_input('Enter the age: '))\n", + "\t\t\temp.bsal = int(raw_input('Enter the basic salary: '))\n", + "\t\t\temp.join_date = int(raw_input('Enter joining date: '))\n", + "\t\t\temp.id = int(raw_input('Enter the employee id: '))\n", + "\t\t\temp.last_edu = raw_input('Enter the last education: ')\n", + "\t\t\temp.sub_taken = raw_input('Enter the subject taken: ')\n", + "\t\t\tfp.write('%d %s %s %d %d %s %s %s \\n' % (emp.id, emp.fname, emp.lname, emp.age, emp.bsal, emp.join_date, emp.last_edu, emp.sub_taken))\n", + "\t\t\tanother = raw_input('Add another record (Y/N) ? ')\n", + "\n", + "\telif choice == 2 :\n", + "\t\tanother = 'Y'\n", + "\t\twhile another == 'Y' or another == 'y' :\n", + "\t\t\tfp = open('12.19.dat','r')\n", + "\t\t\tlines = fp.readlines()\n", + "\t\t\tlines = lines[:-1]\n", + "\t\t\tprint ('\\nRecord deleted ')\n", + "\t\t\tanother = raw_input('Delete another record (Y/N) ? ')\n", + "\t\t\n", + "\telif choice == 3 :\n", + "\t\tfp = open('12.19.dat','w')\n", + "\t\tanother = 'Y'\n", + "\t\twhile another == 'Y' or another == 'y' :\n", + "\t\t\temp.fname = raw_input('Enter the first name: ')\n", + "\t\t\temp.lname = raw_input('Enter the last name: ')\n", + "\t\t\temp.age = int(raw_input('Enter the age: '))\n", + "\t\t\temp.bsal = int(raw_input('Enter the basic salary: '))\n", + "\t\t\temp.join_date = int(raw_input('Enter joining date: '))\n", + "\t\t\temp.id = int(raw_input('Enter the employee id: '))\n", + "\t\t\temp.last_edu = raw_input('Enter the last education: ')\n", + "\t\t\temp.sub_taken = raw_input('Enter the subject taken: ')\n", + "\t\t\tfp.write('%d %s %s %d %d %s %s %s \\n' % (emp.id, emp.fname, emp.lname, emp.age, emp.bsal, emp.join_date, emp.last_edu, emp.sub_taken))\n", + "\t\t\tanother = raw_input('Modify another record (Y/N) ? ')\n", + "\t \t\n", + "\telif choice == 4 :\n", + "\t\twith open ('12.19.dat','r') as fp :\n", + "\t\t\tfor line in fp :\n", + "\t\t\t\tprint line\n", + "\t\t\n", + "\telif choice == 5 :\n", + "\t\tcondition = False\n", + "\t\tfp.close()\n", + "\t\texit()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1.Add Records \t\t\t \n", + "2.Delete Records \t\t\t \n", + "3.Modify Records \t\t\t \n", + "4.List Records \t\t\t \n", + "5.Exit\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter your choice: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the first name: Anand\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the last name: Rao\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the age: 29\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the basic salary: 5000\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter joining date: 2004\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the employee id: 11\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the last education: BCA\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the subject taken: OS\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Add another record (Y/N) ? y\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the first name: Deepti\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the last name: Garg\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the age: 22\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the basic salary: 25000\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter joining date: 2014\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the employee id: 12\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the last education: BTech\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the subject taken: CSE\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Add another record (Y/N) ? n\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1.Add Records \t\t\t \n", + "2.Delete Records \t\t\t \n", + "3.Modify Records \t\t\t \n", + "4.List Records \t\t\t \n", + "5.Exit\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter your choice: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "11 Anand Rao 29 5000 2004 BCA OS \n", + "\n", + "12 Deepti Garg 22 25000 2014 BTech CSE \n", + "\n", + "\n", + "1.Add Records \t\t\t \n", + "2.Delete Records \t\t\t \n", + "3.Modify Records \t\t\t \n", + "4.List Records \t\t\t \n", + "5.Exit\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter your choice: 2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Record deleted \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Delete another record (Y/N) ? y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Record deleted \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Delete another record (Y/N) ? n\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1.Add Records \t\t\t \n", + "2.Delete Records \t\t\t \n", + "3.Modify Records \t\t\t \n", + "4.List Records \t\t\t \n", + "5.Exit\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter your choice: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the first name: Nalin\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the last name: Chhibber\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the age: 25\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the basic salary: 30000\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter joining date: 2012\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the employee id: 07\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the last education: MTech\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the subject taken: DBMS\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Modify another record (Y/N) ? n\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1.Add Records \t\t\t \n", + "2.Delete Records \t\t\t \n", + "3.Modify Records \t\t\t \n", + "4.List Records \t\t\t \n", + "5.Exit\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter your choice: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "7 Nalin Chhibber 25 30000 2012 MTech DBMS \n", + "\n", + "\n", + "1.Add Records \t\t\t \n", + "2.Delete Records \t\t\t \n", + "3.Modify Records \t\t\t \n", + "4.List Records \t\t\t \n", + "5.Exit\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter your choice: 5\n" + ] + } + ], + "prompt_number": 6 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |