summaryrefslogtreecommitdiff
path: root/Magnifying_C_by_Arpita_Goyal
diff options
context:
space:
mode:
Diffstat (limited to 'Magnifying_C_by_Arpita_Goyal')
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/Chapter_10_3.ipynb336
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/Chapter_11_3.ipynb1664
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/Chapter_12_3.ipynb2137
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/Chapter_2_3.ipynb203
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/Chapter_3_3.ipynb1060
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/Chapter_4_3.ipynb1070
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/Chapter_5_3.ipynb909
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/Chapter_6_3.ipynb951
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/Chapter_7_3.ipynb862
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/Chapter_8_3.ipynb2718
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/Chapter_9_3.ipynb1093
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/README.txt10
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/screenshots/chapter_10.pngbin0 -> 22773 bytes
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/screenshots/chapter_2.pngbin0 -> 19444 bytes
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/screenshots/chapter_6.pngbin0 -> 25491 bytes
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/screenshots/ss_1.pngbin0 -> 31802 bytes
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/screenshots/ss_2.pngbin0 -> 42634 bytes
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/screenshots/ss_3.pngbin0 -> 39397 bytes
18 files changed, 13013 insertions, 0 deletions
diff --git a/Magnifying_C_by_Arpita_Goyal/Chapter_10_3.ipynb b/Magnifying_C_by_Arpita_Goyal/Chapter_10_3.ipynb
new file mode 100755
index 00000000..1a4d1459
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/Chapter_10_3.ipynb
@@ -0,0 +1,336 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 10: Miscellany"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10.1, Page number: 356"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "\n",
+ "# Calculation and result\n",
+ "i = 0\n",
+ "for arg in sys.argv :\n",
+ " print ('arg [%d] - %s' % (i, arg))\n",
+ " i = i+1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%run 10.1.py hello world"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "arg [0] - 10.1.py\n",
+ "arg [1] - hello\n",
+ "arg [2] - world\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10.2, Page number: 358"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "if len(sys.argv) != 2 :\n",
+ "\tprint ('Inappropriate number of arguments')\n",
+ "\t\n",
+ "total = 0\n",
+ "def fact (n) :\n",
+ " total = n\n",
+ " while n > 1 :\n",
+ " total = total * (n - 1)\n",
+ " n = n - 1\n",
+ " return total\n",
+ "\n",
+ "print ('Factorial of %d is %d ' % (int(sys.argv[1]), fact (int(sys.argv[1]))))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%run 10.2.py 5"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Factorial of 5 is 120 \n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10.3, Page number: 361"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "\n",
+ "# Function declaration\n",
+ "def printdata () :\n",
+ "\ti = 0\n",
+ "\tfor arg in sys.argv[1:] :\n",
+ "\t\tprint ('The data in cell [%d] is %s ' % (i, arg))\n",
+ "\t\ti = i+1\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('The names before sorting are ')\n",
+ "printdata ()\n",
+ "\n",
+ "sys.argv.sort() \n",
+ "\n",
+ "print ('The names after sorting are ')\n",
+ "printdata ()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%run 10.3.py zimbabwe nigeria argentina china hongkong"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The names before sorting are \n",
+ "The data in cell [0] is zimbabwe \n",
+ "The data in cell [1] is nigeria \n",
+ "The data in cell [2] is argentina \n",
+ "The data in cell [3] is china \n",
+ "The data in cell [4] is hongkong \n",
+ "The names after sorting are \n",
+ "The data in cell [0] is argentina \n",
+ "The data in cell [1] is china \n",
+ "The data in cell [2] is hongkong \n",
+ "The data in cell [3] is nigeria \n",
+ "The data in cell [4] is zimbabwe \n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10.4, Page number: 377"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter any positive number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def convert (a) :\n",
+ " return int(bin(a)[2:])\n",
+ "\n",
+ "bits = str(convert (a))\n",
+ "print ('Number of bits = %d' % (len(bits)))\n",
+ "print ('Binary equivalent is : %d' % (convert (a)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any positive number 16\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Number of bits = 5\n",
+ "Binary equivalent is : 10000\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10.5, Page number: 379"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number : '))\n",
+ "b = int(raw_input('Enter second number : '))\n",
+ "\n",
+ "# Calculation and result\n",
+ "b = ~b + 1\n",
+ "c = a + b\n",
+ "\n",
+ "print ('Subtraction is = %d ' % c)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number : 7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number : 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subtraction is = 4 \n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10.6, Page number: 380"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number : '))\n",
+ "original_a = a\n",
+ "\n",
+ "b = int(raw_input('Enter second number : '))\n",
+ "original_b = b\n",
+ "\n",
+ "# Calculation and result\n",
+ "c = 1\n",
+ "while (b) :\n",
+ "\tif (b & 1) :\n",
+ "\t\tc = c * a\n",
+ "\tb = b >> 1\n",
+ "\ta = a * a\n",
+ "\n",
+ "print ('%d raise to %d = %d ' % (original_a, original_b, c))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number : 7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number : 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7 raise to 3 = 343 \n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file
diff --git a/Magnifying_C_by_Arpita_Goyal/Chapter_11_3.ipynb b/Magnifying_C_by_Arpita_Goyal/Chapter_11_3.ipynb
new file mode 100755
index 00000000..16b27feb
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/Chapter_11_3.ipynb
@@ -0,0 +1,1664 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 11: Structures and Unions"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11.1, Page number: 388"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Structure declaration\n",
+ "class student :\n",
+ "\tid_no = ''\n",
+ "\tname = ''\n",
+ "\taddress = ''\n",
+ "\tage = ''\n",
+ "\t\n",
+ "newstudent = student()\n",
+ "\n",
+ "print ('Enter the student information ')\n",
+ "newstudent.id_no = int(raw_input('Enter student id_no : '))\n",
+ "newstudent.name = raw_input('Enter name of the student : ')\n",
+ "newstudent.address = raw_input('Enter the address of the student : ')\n",
+ "newstudent.age = int(raw_input('Enter the age of the student : '))\n",
+ "\n",
+ "print ('You have entered following information...')\n",
+ "print ('Student id_number = %d ' % newstudent.id_no)\n",
+ "print ('Student name = %s ' % newstudent.name)\n",
+ "print ('Student address = %s ' % newstudent.address)\n",
+ "print ('Age of student = %d ' % newstudent.age)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the student information \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter student id_no : 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name of the student : Amanjeet\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the address of the student : Delhi\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the age of the student : 20\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You have entered following information...\n",
+ "Student id_number = 1 \n",
+ "Student name = Amanjeet \n",
+ "Student address = Delhi \n",
+ "Age of student = 20 \n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11.2, Page number: 390"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Structure declaration\n",
+ "class student :\n",
+ "\tid_no = ''\n",
+ "\tname = ''\n",
+ "\taddress = ''\n",
+ "\tage = ''\n",
+ "\t\n",
+ "newstudent = student()\n",
+ "\n",
+ "def putdata (newstudent) : \n",
+ " print ('You have entered following information...')\n",
+ " print ('Student id_number = %d ' % newstudent.id_no)\n",
+ " print ('Student name = %s ' % newstudent.name)\n",
+ " print ('Student address = %s ' % newstudent.address)\n",
+ " print ('Age of student = %d ' % newstudent.age)\n",
+ " \n",
+ "print ('Enter the student information ') \n",
+ "newstudent.id_no = int(raw_input('Enter student id_no : '))\n",
+ "newstudent.name = raw_input('Enter name of the student : ')\n",
+ "newstudent.address = raw_input('Enter the address of the student : ')\n",
+ "newstudent.age = int(raw_input('Enter the age of the student : '))\n",
+ "\n",
+ "putdata (newstudent)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the student information \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter student id_no : 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name of the student : Deepti\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the address of the student : Noida\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the age of the student : 23\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You have entered following information...\n",
+ "Student id_number = 2 \n",
+ "Student name = Deepti \n",
+ "Student address = Noida \n",
+ "Age of student = 23 \n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11.3, Page number: 392"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Structure declaration\n",
+ "class student :\n",
+ "\tid_no = ''\n",
+ "\tname = ''\n",
+ "\taddress = ''\n",
+ "\tage = ''\n",
+ "\t\n",
+ "newstudent = student()\n",
+ "\n",
+ "def putdata (idno, name, add, age) :\n",
+ " print ('You have entered following information...')\n",
+ " print ('Student id_number = %d ' % idno)\n",
+ " print ('Student name = %s ' % name)\n",
+ " print ('Student address = %s ' % add)\n",
+ " print ('Age of student = %d ' % age)\n",
+ " \n",
+ "print ('Enter the student information ')\n",
+ "newstudent.id_no = int(raw_input('Enter student id_no : '))\n",
+ "newstudent.name = raw_input('Enter name of the student : ')\n",
+ "newstudent.address = raw_input('Enter the address of the student : ')\n",
+ "newstudent.age = int(raw_input('Enter the age of the student : '))\n",
+ "\n",
+ "putdata (newstudent.id_no, newstudent.name, newstudent.address, newstudent.age)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the student information \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter student id_no : 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name of the student : Kushagra\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the address of the student : Bangalore\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the age of the student : 21\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You have entered following information...\n",
+ "Student id_number = 3 \n",
+ "Student name = Kushagra \n",
+ "Student address = Bangalore \n",
+ "Age of student = 21 \n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11.4, Page number: 395"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Structure declaration\n",
+ "class student :\n",
+ "\tid_no = 0\n",
+ "\tname = 'Unknown'\n",
+ "\taddress = 'Unknown'\n",
+ "\tage = 0\n",
+ "\t\n",
+ "newstudent = student()\n",
+ "\n",
+ "def readdata (newstudent) :\n",
+ " print ('Enter the student information ')\n",
+ " newstudent.id_no = int(raw_input('Enter student id_no : '))\n",
+ " newstudent.name = raw_input('Enter name of the student : ')\n",
+ " newstudent.address = raw_input('Enter the address of the student : ')\n",
+ " newstudent.age = int(raw_input('Enter the age of the student : '))\n",
+ "\n",
+ "readdata (newstudent)\n",
+ "print ('You have entered following information...')\n",
+ "print ('Student id_number = %d ' % newstudent.id_no)\n",
+ "print ('Student name = %s ' % newstudent.name)\n",
+ "print ('Student address = %s ' % newstudent.address)\n",
+ "print ('Age of student = %d ' % newstudent.age)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the student information \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter student id_no : 4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name of the student : Vipul\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the address of the student : Chandigarh\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the age of the student : 25\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You have entered following information...\n",
+ "Student id_number = 4 \n",
+ "Student name = Vipul \n",
+ "Student address = Chandigarh \n",
+ "Age of student = 25 \n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11.5, Page number: 397"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Structure declaration\n",
+ "class student :\n",
+ "\tid_no = 0\n",
+ "\tname = 'Unknown'\n",
+ "\taddress = 'Unknown'\n",
+ "\tage = 0\n",
+ "\t\n",
+ "def readdata () :\n",
+ " x = student()\n",
+ " print ('Enter the student information ')\n",
+ " x.id_no = int(raw_input('Enter student id_no : '))\n",
+ " x.name = raw_input('Enter name of the student : ')\n",
+ " x.address = raw_input('Enter the address of the student : ')\n",
+ " x.age = int(raw_input('Enter the age of the student : '))\n",
+ " return x\n",
+ "\n",
+ "newstudent = readdata()\n",
+ "\n",
+ "print ('You have entered following information...')\n",
+ "print ('Student id_number = %d ' % newstudent.id_no)\n",
+ "print ('Student name = %s ' % newstudent.name)\n",
+ "print ('Student address = %s ' % newstudent.address)\n",
+ "print ('Age of student = %d ' % newstudent.age)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the student information \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter student id_no : 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name of the student : Ujjwal\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the address of the student : Jaipur\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the age of the student : 20\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You have entered following information...\n",
+ "Student id_number = 5 \n",
+ "Student name = Ujjwal \n",
+ "Student address = Jaipur \n",
+ "Age of student = 20 \n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11.6, Page number: 400"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Structure declaration\n",
+ "class student :\n",
+ "\tid_no = 0\n",
+ "\tname = 'Unknown'\n",
+ "\taddress = 'Unknown'\n",
+ "\tage = 0\n",
+ "\t\n",
+ "x = student()\n",
+ "\n",
+ "def readdata (x) :\n",
+ " print ('Enter the student information ')\n",
+ " x.id_no = int(raw_input('Enter student id_no : '))\n",
+ " x.name = raw_input('Enter name of the student : ')\n",
+ " x.address = raw_input('Enter the address of the student : ')\n",
+ " x.age = int(raw_input('Enter the age of the student : '))\n",
+ "\n",
+ "readdata(x) \n",
+ "print ('You have entered following information...')\n",
+ "print ('Student id_number = %d ' % x.id_no)\n",
+ "print ('Student name = %s ' % x.name)\n",
+ "print ('Student address = %s ' % x.address)\n",
+ "print ('Age of student = %d ' % x.age)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the student information \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter student id_no : 6\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name of the student : Karan\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the address of the student : Chennai\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the age of the student : 24\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You have entered following information...\n",
+ "Student id_number = 6 \n",
+ "Student name = Karan \n",
+ "Student address = Chennai \n",
+ "Age of student = 24 \n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11.7, Page number: 403"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Structure declaration\n",
+ "class student (object) :\n",
+ "\tdef __init__(self, roll=None, name=None, gender=None, marks=[0]*3) :\n",
+ "\t\tself.roll = roll\n",
+ "\t\tself.name = name\n",
+ "\t\tself.gender = gender\n",
+ "\t\tself.marks = marks\n",
+ "\n",
+ "def putdata (stud, count) :\n",
+ "\tprint ('RollNo\\t Name\\t\\t Gender\\t Sub1\\t Sub2\\t Sub3 ')\n",
+ "\tprint ('---------------------------------------------------------------------')\n",
+ "\t\n",
+ "\tfor i in range (0, count) :\n",
+ "\t\tprint ('%-10d' % stud[i].roll),\n",
+ "\t\tprint ('%-15s' % stud[i].name),\n",
+ "\t\t\n",
+ "\t\tif stud[i].gender == 0 :\n",
+ "\t\t\tprint ('%-15s' % 'Female'),\n",
+ "\t\telse :\n",
+ "\t\t\tprint ('%-15s' % 'Male'),\n",
+ "\t\t\n",
+ "\t\tfor j in range (0, 3) :\n",
+ "\t\t\tprint ('%-10d' % stud[i].marks[j]),\n",
+ "\t\tprint\n",
+ "\n",
+ "def calculateper (stud, count) :\n",
+ "\taverage = 0\n",
+ "\tfor i in range (0, count) :\n",
+ "\t\tsum = 0\n",
+ "\t\tfor j in range (0, 3) :\n",
+ "\t\t\tsum = sum + stud[i].marks[j]\n",
+ "\t\taverage = average + sum/3\n",
+ "\treturn average/count\n",
+ "\n",
+ "stud = []\t\n",
+ "stud.append (student (1, 'Kapildev', 1, (45, 56, 67)))\n",
+ "stud.append (student (2, 'Vidya', 0, (87, 45, 23)))\n",
+ "stud.append (student (3, 'Sharada', 0, (67, 94, 56)))\n",
+ "stud.append (student (4, 'Saismaran', 1, (56, 38, 84)))\n",
+ "stud.append (student (5, 'Reshma', 0, (67, 98, 76)))\n",
+ "\n",
+ "average_percentage = calculateper (stud, 5)\n",
+ "putdata (stud, 5)\n",
+ "print ('\\nAverage percentage of the class is : %.2f ' % average_percentage)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "RollNo\t Name\t\t Gender\t Sub1\t Sub2\t Sub3 \n",
+ "---------------------------------------------------------------------\n",
+ "1 Kapildev Male 45 56 67 \n",
+ "2 Vidya Female 87 45 23 \n",
+ "3 Sharada Female 67 94 56 \n",
+ "4 Saismaran Male 56 38 84 \n",
+ "5 Reshma Female 67 98 76 \n",
+ "\n",
+ "Average percentage of the class is : 63.00 \n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11.8, Page number: 406"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Structure declaration\n",
+ "class student (object) :\n",
+ "\tdef __init__(self, roll=None, name=None, gender=None, marks=[0]*3) :\n",
+ "\t\tself.roll = roll\n",
+ "\t\tself.name = name\n",
+ "\t\tself.gender = gender\n",
+ "\t\tself.marks = marks\n",
+ "\t\n",
+ "def putdata (stud, count) :\n",
+ "\tprint ('\\nRollNo\\t Name\\t\\t Gender\\t Sub1\\t Sub2\\t Sub3 ')\n",
+ "\tprint ('---------------------------------------------------------------------')\n",
+ "\t\n",
+ "\tfor i in range (0, count) :\n",
+ "\t\tprint ('%-10d' % stud[i].roll),\n",
+ "\t\tprint ('%-15s' % stud[i].name),\n",
+ "\t\t\n",
+ "\t\tif stud[i].gender == 0 :\n",
+ "\t\t\tprint ('%-15s' % 'Female'),\n",
+ "\t\telse :\n",
+ "\t\t\tprint ('%-15s' % 'Male'),\n",
+ "\t\t\n",
+ "\t\tfor j in range (0, 3) :\n",
+ "\t\t\tprint ('%-10d' % stud[i].marks[j]),\n",
+ "\t\tprint\n",
+ "\n",
+ "def calculateper (stud, count) :\n",
+ "\taverage = 0\n",
+ "\tfor i in range (0, count) :\n",
+ "\t\tsum = 0\n",
+ "\t\tfor j in range (0, 3) :\n",
+ "\t\t\tsum = sum + stud[i].marks[j]\n",
+ "\t\taverage = average + sum/3\n",
+ "\treturn average/count\n",
+ "\n",
+ "count = int(raw_input('Enter the number of students in a class : '))\n",
+ "stud = [student() for i in range (0, count)]\n",
+ "\n",
+ "for i in range (0, count) :\n",
+ "\tstud[i].roll = int(raw_input('\\nEnter roll number of student %d: ' % (i+1)))\n",
+ "\tstud[i].name = raw_input('Enter name of student %d: ' % (i+1))\n",
+ "\tstud[i].gender = int(raw_input('Enter gender[0/1] of student %d: ' % (i+1)))\n",
+ "\tprint ('Enter marks of three subjects of student %d: ' % (i+1))\n",
+ "\tfor j in range (0, 3) :\n",
+ "\t\tstud[i].marks[j] = int(raw_input('Subject %d: ' % (j+1)))\n",
+ "\n",
+ "putdata (stud, count)\n",
+ "average_percentage = calculateper (stud, count)\n",
+ "print ('\\nAverage percentage of the class is : %.2f ' % average_percentage)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the number of students in a class : 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter roll number of student 1: 24\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name of student 1: Vidya\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter gender[0/1] of student 1: 0\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks of three subjects of student 1: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 1: 78\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 2: 96\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 3: 75\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "RollNo\t Name\t\t Gender\t Sub1\t Sub2\t Sub3 \n",
+ "---------------------------------------------------------------------\n",
+ "24 Vidya Female 78 96 75 \n",
+ "\n",
+ "Average percentage of the class is : 83.00 \n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11.9, Page number: 408"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Structure declaration\n",
+ "class student (object) :\n",
+ "\tdef __init__(self, roll=None, name=None, gender=None, marks=[0]*3) :\n",
+ "\t\tself.roll = roll\n",
+ "\t\tself.name = name\n",
+ "\t\tself.gender = gender\n",
+ "\t\tself.marks = marks\n",
+ "\n",
+ "def readdata (stud, count) :\n",
+ "\tfor i in range (0, count) :\n",
+ "\t\tstud[i].roll = int(raw_input('\\nEnter roll number of student %d: ' % (i+1)))\n",
+ "\t\tstud[i].name = raw_input('Enter name of student %d: ' % (i+1))\n",
+ "\t\tstud[i].gender = int(raw_input('Enter gender[0/1] of student %d: ' % (i+1)))\n",
+ "\t\tprint ('Enter marks of three subjects of student %d: ' % (i+1))\n",
+ "\t\tfor j in range (0, 3) :\n",
+ "\t\t\tstud[i].marks[j] = int(raw_input('Subject %d: ' % (j+1)))\n",
+ "\t\n",
+ "def putdata (stud, count) :\n",
+ "\tprint ('\\nRollNo\\t Name\\t\\t Gender\\t Sub1\\t Sub2\\t Sub3 ')\n",
+ "\tprint ('---------------------------------------------------------------------')\n",
+ "\t\n",
+ "\tfor i in range (0, count) :\n",
+ "\t\tprint ('%-10d' % stud[i].roll),\n",
+ "\t\tprint ('%-15s' % stud[i].name),\n",
+ "\t\t\n",
+ "\t\tif stud[i].gender == 0 :\n",
+ "\t\t\tprint ('%-15s' % 'Female'),\n",
+ "\t\telse :\n",
+ "\t\t\tprint ('%-15s' % 'Male'),\n",
+ "\t\t\n",
+ "\t\tfor j in range (0, 3) :\n",
+ "\t\t\tprint ('%-10d' % stud[i].marks[j]),\n",
+ "\t\tprint\n",
+ "\n",
+ "def calculateper (stud, count) :\n",
+ "\taverage = 0\n",
+ "\tfor i in range (0, count) :\n",
+ "\t\tsum = 0\n",
+ "\t\tfor j in range (0, 3) :\n",
+ "\t\t\tsum = sum + stud[i].marks[j]\n",
+ "\t\taverage = average + sum/3\n",
+ "\treturn average/count\n",
+ "\n",
+ "count = int(raw_input('Enter the number of students in a class : '))\n",
+ "stud = [student() for i in range (0, count)]\n",
+ "readdata (stud, count)\n",
+ "\n",
+ "average_percentage = calculateper (stud, count)\n",
+ "putdata (stud, count)\n",
+ "print ('\\nAverage percentage of the class is : %.2f ' % average_percentage)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the number of students in a class : 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter roll number of student 1: 25\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name of student 1: Kapildev\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter gender[0/1] of student 1: 1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks of three subjects of student 1: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 1: 45\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 2: 70\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 3: 93\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "RollNo\t Name\t\t Gender\t Sub1\t Sub2\t Sub3 \n",
+ "---------------------------------------------------------------------\n",
+ "25 Kapildev Male 45 70 93 \n",
+ "\n",
+ "Average percentage of the class is : 69.00 \n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11.10, Page number: 412"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Structure declaration\n",
+ "class time (object) :\n",
+ "\tdef __init__(self, hours=None, minutes=None, seconds=None) :\n",
+ "\t\tself.hours = hours\n",
+ "\t\tself.minutes = minutes\n",
+ "\t\tself.seconds = seconds\n",
+ "\n",
+ "def AcceptTime (t) :\n",
+ "\tprint ('Enter the time in hh mm ss format: ')\n",
+ "\tt.hours = int(raw_input('Hours = '))\n",
+ "\tt.minutes = int(raw_input('Minutes = '))\n",
+ "\tt.seconds = int(raw_input('Seconds = '))\n",
+ "\n",
+ "def ValidateTime (t) :\n",
+ "\tif t.hours < 0 or t.hours > 23 :\n",
+ "\t\treturn 0\n",
+ "\tif t.minutes < 0 or t.minutes > 60 :\n",
+ "\t\treturn 0\n",
+ "\tif t.seconds < 0 or t.seconds > 60 :\n",
+ "\t\treturn 0\n",
+ "\treturn 1\n",
+ "\n",
+ "def DisplayTime (t) :\n",
+ "\tprint ('Time you entered is %d:%d:%d ' % (t.hours, t.minutes, t.seconds))\n",
+ "\t\n",
+ "condition = True\n",
+ "while condition :\n",
+ "\tt = time ()\n",
+ "\tAcceptTime (t)\n",
+ "\t\n",
+ "\tif ValidateTime (t) == 1 :\n",
+ "\t\tcondition = False\n",
+ "\telse :\n",
+ "\t\tprint ('\\nTime is incorrect. Enter again ')\n",
+ "\t\t\n",
+ "DisplayTime (t)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the time in hh mm ss format: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hours = 13\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Minutes = 45\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Seconds = 53\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Time you entered is 13:45:53 \n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11.11, Page number: 414"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Structure declaration\n",
+ "class date (object) :\n",
+ "\tdef __init__(self, dd=None, mm=None, yy=None) :\n",
+ "\t\tself.dd = dd\n",
+ "\t\tself.mm = mm\n",
+ "\t\tself.yy = yy\n",
+ "\n",
+ "def AcceptDate (d) :\n",
+ "\tprint ('Enter date in dd mm yy format: ')\n",
+ "\td.dd = int(raw_input('Date = '))\n",
+ "\td.mm = int(raw_input('Month = '))\n",
+ "\td.yy = int(raw_input('Year = '))\n",
+ "\n",
+ "def isleap (val) :\n",
+ "\tif val % 4 == 0 and val % 100 or val % 400 == 0 :\n",
+ "\t\treturn 1\n",
+ "\treturn 0\n",
+ "\t\n",
+ "def ValidateDate (d) :\n",
+ "\tif d.dd < 1 or d.dd > 31 :\n",
+ "\t\treturn 0\n",
+ "\tif d.mm < 1 or d.mm > 12 :\n",
+ "\t\treturn 0\n",
+ "\tif d.yy < 1 :\n",
+ "\t\treturn 0\n",
+ "\tif d.mm == 2 :\n",
+ "\t\tif isleap (d.yy) :\n",
+ "\t\t\tif d.dd > 29 :\n",
+ "\t\t\t\treturn 0\n",
+ "\t\telif d.dd > 28 :\n",
+ "\t\t\treturn 0\n",
+ "\tif d.mm == 4 or d.mm == 6 or d.mm == 9 or d.mm == 11 and d.dd > 30 :\n",
+ "\t\treturn 0\n",
+ "\treturn 1\n",
+ "\n",
+ "def DisplayMsg (d1, d2) :\n",
+ "\tif d1.dd == d2.dd and d1.mm == d2.mm and d1.yy == d2.yy :\n",
+ "\t\tprint ('Both dates are Equal ')\n",
+ "\telse :\n",
+ "\t\tprint ('Both dates are Not Equal ')\n",
+ "\t\n",
+ "condition = True\n",
+ "while condition :\n",
+ "\td1 = date ()\n",
+ "\tAcceptDate (d1)\n",
+ "\t\n",
+ "\tif ValidateDate (d1) == 1 :\n",
+ "\t\tcondition = False\n",
+ "\telse :\n",
+ "\t\tprint ('\\nDate is incorrect. Enter again ')\n",
+ "\n",
+ "condition = True\n",
+ "while condition :\n",
+ "\td2 = date ()\n",
+ "\tAcceptDate (d2)\n",
+ "\t\n",
+ "\tif ValidateDate (d2) == 1 :\n",
+ "\t\tcondition = False\n",
+ "\telse :\n",
+ "\t\tprint ('\\nDate is incorrect. Enter again ')\n",
+ "\t\t\n",
+ "DisplayMsg (d1, d2)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter date in dd mm yy format: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Date = 12\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Month = 11\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Year = 92\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter date in dd mm yy format: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Date = 12\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Month = 11\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Year = 92\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Both dates are Equal \n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11.12, Page number: 415"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Structure declaration\n",
+ "class date (object) :\n",
+ "\tdef __init__(self, dd=None, mm=None, yy=None) :\n",
+ "\t\tself.dd = dd\n",
+ "\t\tself.mm = mm\n",
+ "\t\tself.yy = yy\n",
+ "\n",
+ "def AcceptDate (d) :\n",
+ "\tprint ('Enter date in dd mm yy format: ')\n",
+ "\td.dd = int(raw_input('Date = '))\n",
+ "\td.mm = int(raw_input('Month = '))\n",
+ "\td.yy = int(raw_input('Year = '))\n",
+ "\n",
+ "def isleap (val) :\n",
+ "\tif val % 4 == 0 and val % 100 or val % 400 == 0 :\n",
+ "\t\treturn 1\n",
+ "\treturn 0\n",
+ "\t\n",
+ "def ValidateDate (d) :\n",
+ "\tif d.dd < 1 or d.dd > 31 :\n",
+ "\t\treturn 0\n",
+ "\tif d.mm < 1 or d.mm > 12 :\n",
+ "\t\treturn 0\n",
+ "\tif d.yy < 1 :\n",
+ "\t\treturn 0\n",
+ "\tif d.mm == 2 :\n",
+ "\t\tif isleap (d.yy) :\n",
+ "\t\t\tif d.dd > 29 :\n",
+ "\t\t\t\treturn 0\n",
+ "\t\telif d.dd > 28 :\n",
+ "\t\t\treturn 0\n",
+ "\tif d.mm == 4 or d.mm == 6 or d.mm == 9 or d.mm == 11 and d.dd > 30 :\n",
+ "\t\treturn 0\n",
+ "\treturn 1\n",
+ "\n",
+ "def CalcDate (d1, d2) :\n",
+ "\tdate1 = date2 = 0\n",
+ "\tfor i in range (-d1.yy, d1.yy) :\n",
+ "\t\tdate1 = date1 + 366 if isleap (d1.yy) else 365\n",
+ "\t\td1.yy = d1.yy - 1\n",
+ "\t\t\n",
+ "\tfor i in range (-d1.mm, d1.mm) :\n",
+ "\t\tdate1 = date1 + (30 if (d1.mm==4 or d1.mm==6 or d1.mm==9 or d1.mm==11) else ((29 if isleap(d1.yy) else 28) if (d1.mm==2) else 31))\n",
+ "\t\n",
+ "\tdate1 = date1 + d1.dd\n",
+ "\t\n",
+ "\tfor i in range (-d2.yy, d2.yy) :\n",
+ "\t\tdate2 = date2 + 366 if isleap(d2.yy) else 365\n",
+ "\t\n",
+ "\tfor i in range (-d2.mm, d2.mm) :\n",
+ "\t\tdate2 = date2 + (30 if (d2.mm==4 or d2.mm==6 or d2.mm==9 or d2.mm==11) else ((29 if isleap(d2.yy) else 28) if d2.mm==2 else 31))\n",
+ "\t\n",
+ "\tdate2 = date2 + d2.dd\n",
+ "\t\n",
+ "\treturn (date2 - date1)/2\n",
+ "\t\t\n",
+ "def DisplayMsg (d1, d2, diff) :\n",
+ "\tprint ('The difference between %d %d %d and %d %d %d is %d days ' % (d1.dd, d1.mm, -d1.yy, d2.dd, d2.mm, d2.yy, diff))\n",
+ "\t\n",
+ "condition = True\n",
+ "while condition :\n",
+ "\tprint ('\\nEnter first date: ')\n",
+ "\td1 = date ()\n",
+ "\tAcceptDate (d1)\n",
+ "\t\n",
+ "\tif ValidateDate (d1) == 1 :\n",
+ "\t\tcondition = False\n",
+ "\telse :\n",
+ "\t\tprint ('\\nDate is incorrect. Enter again ')\n",
+ "\n",
+ "condition = True\n",
+ "while condition :\n",
+ "\tprint ('\\nEnter second date: ')\n",
+ "\td2 = date ()\n",
+ "\tAcceptDate (d2)\n",
+ "\t\n",
+ "\tif ValidateDate (d2) == 1 :\n",
+ "\t\tcondition = False\n",
+ "\telse :\n",
+ "\t\tprint ('\\nDate is incorrect. Enter again ')\n",
+ "\n",
+ "diff = CalcDate (d1, d2)\n",
+ "DisplayMsg (d1, d2, diff)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter first date: \n",
+ "Enter date in dd mm yy format: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Date = 01\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Month = 01\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Year = 01\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter second date: \n",
+ "Enter date in dd mm yy format: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Date = 25\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Month = 12\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Year = 08\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The difference between 1 1 1 and 25 12 8 is 2915 days \n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11.13, Page number: 419"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from ctypes import *\n",
+ "\n",
+ "# Structure declaration\n",
+ "class stream (Union) :\n",
+ "\t_fields_ = [('grade', c_char), \n",
+ "\t\t\t\t\t('class_std', c_int), \n",
+ "\t\t\t\t\t('percentage', c_float)]\n",
+ "\t\n",
+ "class student (Structure) :\n",
+ "\t_fields_ = [('roll', c_int), \n",
+ "\t\t\t\t\t('name', c_char_p), \n",
+ "\t\t\t\t\t('gender', c_int), \n",
+ "\t\t\t\t\t('marks', c_int * 3), \n",
+ "\t\t\t\t\t('st_stream', c_char), \n",
+ "\t\t\t\t\t('st', stream)]\n",
+ "\t\n",
+ "def putdata (stud, count) :\n",
+ "\tprint ('\\nStream\\t Roll\\t Name\\t Gender\\t Result\\t ')\n",
+ "\tprint ('---------------------------------------------------------------------')\n",
+ "\t\n",
+ "\tfor i in range (0, count) :\n",
+ "\t\tif stud[i].st_stream == 'A' :\n",
+ "\t\t\tprint ('Arts\\t '),\n",
+ "\t\telif stud[i].st_stream == 'S' :\n",
+ "\t\t\tprint ('Science\\t '),\n",
+ "\t\telse :\n",
+ "\t\t\tprint ('Commerce '),\n",
+ "\t\t\t\n",
+ "\t\tprint ('%-5d ' % stud[i].roll),\n",
+ "\t\tprint ('%-3s ' % stud[i].name),\n",
+ "\t\t\n",
+ "\t\tif stud[i].gender == 0 :\n",
+ "\t\t\tprint ('\\tFemale'),\n",
+ "\t\telse :\n",
+ "\t\t\tprint ('\\tMale '),\n",
+ "\t\t\n",
+ "\t\tif stud[i].st_stream == 'A' :\n",
+ "\t\t\tprint (' %d Class ' % stud[i].st.class_std),\n",
+ "\t\telif stud[i].st_stream == 'S' :\n",
+ "\t\t\tprint (' %c Grade ' % stud[i].st.grade),\n",
+ "\t\telse :\n",
+ "\t\t\tprint (' %.2f Percent ' % stud[i].st.percentage),\n",
+ "\t\tprint\n",
+ "\n",
+ "def calculateper (stud, count) :\n",
+ "\taverage = 0\n",
+ "\tfor i in range (0, count) :\n",
+ "\t\tsum = 0\n",
+ "\t\tfor j in range (0, 3) :\n",
+ "\t\t\tsum = sum + stud[i].marks[j]\n",
+ "\t\taverage = (float)(sum/3)\n",
+ "\t\t\n",
+ "\t\tif stud[i].st_stream == 'A' :\n",
+ "\t\t\tif average >= 60 :\n",
+ "\t\t\t\tstud[i].st.class_std = 1\n",
+ "\t\t\telse :\n",
+ "\t\t\t\tstud[i].st.class_std = 2\n",
+ "\t\t\t\t\n",
+ "\t\telif stud[i].st_stream == 'S' :\n",
+ "\t\t\tif average >= 60 :\n",
+ "\t\t\t\tstud[i].st.grade = 'A'\n",
+ "\t\t\telse :\n",
+ "\t\t\t\tstud[i].st.class_std = 'B'\n",
+ "\t\telse : \n",
+ "\t\t\tstud[i].st.percentage = average\n",
+ "\n",
+ "count = int(raw_input('Enter the number of students in a class : '))\n",
+ "stud = [student() for i in range (0, count)]\n",
+ "\n",
+ "for i in range (0, count) :\n",
+ "\tstud[i].st_stream = raw_input('\\nEnter stream of student %d (A/C/S) : ' % (i+1))\n",
+ "\tstud[i].roll = int(raw_input('Enter roll number of student %d : ' % (i+1)))\n",
+ "\tstud[i].name = raw_input('Enter name of student %d : ' % (i+1))\n",
+ "\tstud[i].gender = int(raw_input('Enter gender of student %d [0/1] : ' % (i+1)))\n",
+ "\tprint ('Enter marks of three subjects of student %d : ' % (i+1))\n",
+ "\tfor j in range (0, 3) :\n",
+ "\t\tstud[i].marks[j] = int(raw_input('Subject %d : ' % (j+1)))\n",
+ "\t\n",
+ "calculateper (stud, count)\n",
+ "putdata (stud, count)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the number of students in a class : 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter stream of student 1 (A/C/S) : A\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter roll number of student 1 : 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name of student 1 : Saismaran\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter gender of student 1 [0/1] : 1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks of three subjects of student 1 : \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 1 : 65\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 2 : 83\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 3 : 59\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter stream of student 2 (A/C/S) : C\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter roll number of student 2 : 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name of student 2 : Vidya\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter gender of student 2 [0/1] : 0\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks of three subjects of student 2 : \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 1 : 84\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 2 : 75\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 3 : 93\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter stream of student 3 (A/C/S) : S\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter roll number of student 3 : 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name of student 3 : Pranav\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter gender of student 3 [0/1] : 1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks of three subjects of student 3 : \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 1 : 84\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 2 : 95\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 3 : 83\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Stream\t Roll\t Name\t Gender\t Result\t \n",
+ "---------------------------------------------------------------------\n",
+ "Arts\t 1 Saismaran \tMale 1 Class \n",
+ "Commerce 2 Vidya \tFemale 84.00 Percent \n",
+ "Science\t 3 Pranav \tMale A Grade \n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file
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
diff --git a/Magnifying_C_by_Arpita_Goyal/Chapter_2_3.ipynb b/Magnifying_C_by_Arpita_Goyal/Chapter_2_3.ipynb
new file mode 100755
index 00000000..7144ab92
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/Chapter_2_3.ipynb
@@ -0,0 +1,203 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 2: Syntactic Aspects"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 1, Page number: 39"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Calculation and result\n",
+ "class color() :\n",
+ " RED = 5\n",
+ " YELLOW = 6\n",
+ " GREEN = 4\n",
+ " BLUE = 5\n",
+ "\n",
+ "print ('RED = %d ' % color.RED)\n",
+ "print ('YELLOW = %d ' % color.YELLOW)\n",
+ "print ('GREEN = %d ' % color.GREEN)\n",
+ "print ('BLUE = %d ' % color.BLUE)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "RED = 5 \n",
+ "YELLOW = 6 \n",
+ "GREEN = 4 \n",
+ "BLUE = 5 \n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2, Page number: 41"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Calculation and result\n",
+ "i = 2004\n",
+ "c = 'Year'\n",
+ "\n",
+ "print ('%s %d' % (c, i))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Year 2004\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.1, Page number: 44"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Calculation and result\n",
+ "num = 5\n",
+ "c = '$'\n",
+ "pi = 3.141\n",
+ "print ('Hello World')\n",
+ "print ('%d %c %f' % (num, c, pi))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hello World\n",
+ "5 $ 3.141000\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.2, Page number: 45"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Calculation and result\n",
+ "num = int(raw_input('Enter your favorite number '))\n",
+ "print ('You have entered your favorite number as %d ' % num)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter your favorite number 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You have entered your favorite number as 3 \n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.3, Page number: 60"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Calculation and result\n",
+ "fahrenheit = float(raw_input('Please enter the Fahrenheit temperature '))\n",
+ "\n",
+ "centigrade = float(5)/9 * (fahrenheit - 32)\n",
+ "\n",
+ "print ('The temperature in Centigrade is %.2f ' % centigrade)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Please enter the Fahrenheit temperature 100\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The temperature in Centigrade is 37.78 \n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file
diff --git a/Magnifying_C_by_Arpita_Goyal/Chapter_3_3.ipynb b/Magnifying_C_by_Arpita_Goyal/Chapter_3_3.ipynb
new file mode 100755
index 00000000..9e566fc1
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/Chapter_3_3.ipynb
@@ -0,0 +1,1060 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 3: Simple Programs"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 1, Page number: 67"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Calculation and result\n",
+ "pi = 3.141\n",
+ "num = int(raw_input('Enter your favorite number: '))\n",
+ "print ('You have entered your favorite number as %d' % num)\n",
+ "c = raw_input('Enter your favorite character: ')\n",
+ "print ('You have entered your favorite character as %c' % c)\n",
+ "print ('The value of pi is %.3f ' % pi)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter your favorite number: 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You have entered your favorite number as 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter your favorite character: m\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You have entered your favorite character as m\n",
+ "The value of pi is 3.141 \n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.1, Page number: 70"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number '))\n",
+ "b = int(raw_input('Enter second number '))\n",
+ "\n",
+ "# Calculation\n",
+ "som = a+b\n",
+ "\n",
+ "# Result\n",
+ "print ('The sum of %d + %d = %d ' % (a, b, som))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number 67\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number 990\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum of 67 + 990 = 1057 \n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.2, Page number: 71"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number '))\n",
+ "b = int(raw_input('Enter second number '))\n",
+ "\n",
+ "# Calculation and result\n",
+ "if a>b :\n",
+ " print ('The greater number is %d ' % a)\n",
+ "else :\n",
+ " print ('The greater number is %d ' % b)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number 78\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number 190\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The greater number is 190 \n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.3, Page number: 72"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number '))\n",
+ "b = int(raw_input('Enter second number '))\n",
+ "\n",
+ "# Calculation and result\n",
+ "if a==b :\n",
+ " print ('Both numbers are equal ')\n",
+ "else :\n",
+ " if a>b :\n",
+ " print ('The greater number is %d ' % a)\n",
+ " else :\n",
+ " print ('The greater number is %d ' % b)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number 78\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number 78\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Both numbers are equal \n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.4, Page number: 73"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number '))\n",
+ "b = int(raw_input('Enter second number '))\n",
+ "c = int(raw_input('Enter third number '))\n",
+ "\n",
+ "# Calculation and result\n",
+ "if a>b :\n",
+ " if a>c :\n",
+ " print ('The greatest number is %d ' % a)\n",
+ " else :\n",
+ " print ('The greatest number is %d ' % c)\n",
+ "\n",
+ "else :\n",
+ " if b>c :\n",
+ " print ('The greatest number is %d ' % b)\n",
+ " else : \n",
+ " print ('The greatest number is %d ' % c)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number 45\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number 128\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter third number 90\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The greatest number is 128 \n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.5, Page number: 74"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('Counting upto %d ' % n)\n",
+ "\n",
+ "for i in range (1, n+1) :\n",
+ " print ('%d ' % i),"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Counting upto 7 \n",
+ "1 2 3 4 5 6 7 \n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.6, Page number: 75"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('Even numbers upto %d ' % n)\n",
+ "\n",
+ "for i in range (2, n+1, 2) :\n",
+ " print ('%d ' % i),"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Even numbers upto 10 \n",
+ "2 4 6 8 10 \n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.7, Page number: 75"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('Odd numbers upto %d ' % n)\n",
+ "\n",
+ "for i in range (1, n+1, 2) :\n",
+ " print ('%d ' % i),"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Odd numbers upto 10 \n",
+ "1 3 5 7 9 \n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.8, Page number: 76"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('Number line upto %d ' % n)\n",
+ "\n",
+ "for i in range (-n, n+1) :\n",
+ " print ('%d ' % i),"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Number line upto 7 \n",
+ "-7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 \n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.9, Page number: 77"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "sum = 0\n",
+ "\n",
+ "# Calculation\n",
+ "for i in range (1, n+1) :\n",
+ " sum = sum + i\n",
+ "\n",
+ "# Result\n",
+ "print ('Sum of %d natural numbers is %d ' % (n, sum))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum of 6 natural numbers is 21 \n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.10, Page number: 78"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('The factors of %d are ' % n)\n",
+ "\n",
+ "for i in range (1, n+1) :\n",
+ " if n%i == 0 :\n",
+ " print ('%d ' % i),"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 28\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The factors of 28 are \n",
+ "1 2 4 7 14 28 \n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.11, Page number: 79"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('The first 10 multiples of %d are ' % n)\n",
+ "\n",
+ "for i in range (1, 11) :\n",
+ " print ('%d X %d = %d ' % (n, i, n*i))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The first 10 multiples of 6 are \n",
+ "6 X 1 = 6 \n",
+ "6 X 2 = 12 \n",
+ "6 X 3 = 18 \n",
+ "6 X 4 = 24 \n",
+ "6 X 5 = 30 \n",
+ "6 X 6 = 36 \n",
+ "6 X 7 = 42 \n",
+ "6 X 8 = 48 \n",
+ "6 X 9 = 54 \n",
+ "6 X 10 = 60 \n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.12, Page number: 80"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "i = 1\n",
+ "sum = 0\n",
+ "\n",
+ "# Calculation\n",
+ "print ('The sum of digits of %d is' % n),\n",
+ "\n",
+ "while n > 0 :\n",
+ " sum = sum + n%10\n",
+ " n = n/10\n",
+ "\n",
+ "# Result\n",
+ "print ('%d' % sum)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 5214\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum of digits of 5214 is 12\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.13, Page number: 81"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "flag = 1\n",
+ "\n",
+ "# Calculation\n",
+ "for i in range (2, n) :\n",
+ " if flag == 1 :\n",
+ " if n%i == 0 :\n",
+ " flag = 0\n",
+ "\n",
+ "# Result\n",
+ "if flag :\n",
+ " print ('%d is Prime ' % n)\n",
+ "else :\n",
+ " print ('%d is Composite ' % n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 41\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "41 is Prime \n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.14, Page number: 82"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "first = 0\n",
+ "second = 1\n",
+ "count = 3\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('%d Fibonacci terms are ' % n)\n",
+ "print ('%d %d ' % (first, second)),\n",
+ "\n",
+ "while count<=n :\n",
+ " third = first + second\n",
+ " first = second\n",
+ " second = third\n",
+ " print ('%d ' % third),\n",
+ " count = count + 1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10 Fibonacci terms are \n",
+ "0 1 1 2 3 5 8 13 21 34 \n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.15, Page number: 83"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "sum = n\n",
+ "\n",
+ "# Calculation\n",
+ "print ('Sum of digits of %d is' % n),\n",
+ "\n",
+ "while sum>=10 :\n",
+ " sum = 0\n",
+ " while n>0 :\n",
+ " sum = sum + n%10\n",
+ " n = n/10\n",
+ " n = sum\n",
+ "\n",
+ "# Result\n",
+ "print ('%d' % sum)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 8626\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum of digits of 8626 is 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.16, Page number: 84"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('Prime numbers upto %d are ' % n)\n",
+ "\n",
+ "for num in range (2, n+1) :\n",
+ " flag = 1\n",
+ " for i in range (2, num) :\n",
+ " if num % i == 0 : \n",
+ " flag = 0\n",
+ " if flag :\n",
+ " print ('%d ' % num),"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 12\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Prime numbers upto 12 are \n",
+ "2 3 5 7 11 \n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.17, Page number: 85"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number '))\n",
+ "\n",
+ "# Calculation and result\n",
+ "for lin in range (1, n+1) :\n",
+ " for count in range (1, lin+1) :\n",
+ " print ('%d ' % count),\n",
+ " print"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number 6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 \n",
+ "1 2 \n",
+ "1 2 3 \n",
+ "1 2 3 4 \n",
+ "1 2 3 4 5 \n",
+ "1 2 3 4 5 6 \n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.18, Page number: 86"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number '))\n",
+ "\n",
+ "# Calculation and result\n",
+ "for lin in range (1, n+1) :\n",
+ " for count in range (lin, 2*lin) :\n",
+ " print ('%d ' % count),\n",
+ " print"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number 6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 \n",
+ "2 3 \n",
+ "3 4 5 \n",
+ "4 5 6 7 \n",
+ "5 6 7 8 9 \n",
+ "6 7 8 9 10 11 \n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.19, Page number: 87"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number '))\n",
+ "\n",
+ "# Calculation and result\n",
+ "for lin in range (1, n+1) :\n",
+ " for blk in range (1, n-lin+1) :\n",
+ " print ' ',\n",
+ " for up in range (1, lin+1) :\n",
+ " print ('%d' % up),\n",
+ " down = lin-1\n",
+ " while down>0 :\n",
+ " print ('%d' % down),\n",
+ " down = down-1\n",
+ " print"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number 6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 1\n",
+ " 1 2 1\n",
+ " 1 2 3 2 1\n",
+ " 1 2 3 4 3 2 1\n",
+ " 1 2 3 4 5 4 3 2 1\n",
+ "1 2 3 4 5 6 5 4 3 2 1\n"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3.20, Page number: 88"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number '))\n",
+ "\n",
+ "# Calculation and result\n",
+ "for lin in range (1, n+1) :\n",
+ " for blk in range (1, n-lin+1) :\n",
+ " print ' ',\n",
+ " for up in range (lin, 2*lin) :\n",
+ " print ('%d' % (up%10)),\n",
+ " down = 2*lin-2\n",
+ " while down>=lin :\n",
+ " print ('%d' % (down%10)),\n",
+ " down = down-1\n",
+ " print"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number 6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 1\n",
+ " 2 3 2\n",
+ " 3 4 5 4 3\n",
+ " 4 5 6 7 6 5 4\n",
+ " 5 6 7 8 9 8 7 6 5\n",
+ "6 7 8 9 0 1 0 9 8 7 6\n"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file
diff --git a/Magnifying_C_by_Arpita_Goyal/Chapter_4_3.ipynb b/Magnifying_C_by_Arpita_Goyal/Chapter_4_3.ipynb
new file mode 100755
index 00000000..a071712d
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/Chapter_4_3.ipynb
@@ -0,0 +1,1070 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 4: Functions"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.1, Page number: 91"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Function declaration and result\n",
+ "a = 5\n",
+ "c = 'h'\n",
+ "b = 3.14\n",
+ "def myfunction (x, ch, fl) :\n",
+ " print ('%d %c %.2f ' % (x, ch, fl))\n",
+ " \n",
+ "myfunction (a, c, b)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5 h 3.14 \n"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.2, Page number: 92"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def add (a, b) :\n",
+ " som = a + b\n",
+ " return som\n",
+ "\n",
+ "print ('The sum of %d and %d is %d' % (a, b, (add (a, b))))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 8\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum of 7 and 8 is 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.3, Page number: 99"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def greater (a, b) :\n",
+ " if a > b :\n",
+ " return a\n",
+ " else :\n",
+ " return b\n",
+ "\n",
+ "print ('The greater number is %d' % (greater (a, b)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 78\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 190\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The greater number is 190\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.4, Page number: 100"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def greater (a, b) :\n",
+ " if a == b :\n",
+ " print ('Both numbers are equal')\n",
+ " elif a > b :\n",
+ " print ('The greater number is %d' % a)\n",
+ " else :\n",
+ " print ('The greater number is %d' % b)\n",
+ " return\n",
+ "\n",
+ "greater (a, b)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 20\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 20\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Both numbers are equal\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.5, Page number: 102"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "c = int(raw_input('Enter third number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def greatest (a, b, c) :\n",
+ " if a > b :\n",
+ " if a > c :\n",
+ " return a\n",
+ " else :\n",
+ " return c\n",
+ " else :\n",
+ " if b > c :\n",
+ " return b\n",
+ " else :\n",
+ " return c\n",
+ "\n",
+ "print ('The greatest number is %d' % (greatest (a, b, c)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 45\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 128\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter third number: 190\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The greatest number is 190\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.6, Page number: 104"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def printnum (n) :\n",
+ " print ('Counting upto %d' % n) \n",
+ " for i in range (1, n+1) :\n",
+ " print i,\n",
+ "\n",
+ "printnum (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Counting upto 7\n",
+ "1 2 3 4 5 6 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.7, Page number: 105"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def printeven (n) :\n",
+ " print ('Even numbers upto %d' % n) \n",
+ " for i in range (2, n+1, 2) :\n",
+ " print i,\n",
+ "\n",
+ "printeven (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Even numbers upto 7\n",
+ "2 4 6\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.8, Page number: 107"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def printodd (n) :\n",
+ " print ('Odd numbers upto %d' % n) \n",
+ " for i in range (1, n+1, 2) :\n",
+ " print i,\n",
+ "\n",
+ "printodd (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Odd numbers upto 7\n",
+ "1 3 5 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.9, Page number: 108"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def printnum (n) :\n",
+ " print ('Number line for %d' % n) \n",
+ " for i in range (-n, n+1) :\n",
+ " print i,\n",
+ "\n",
+ "printnum (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Number line for 4\n",
+ "-4 -3 -2 -1 0 1 2 3 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.10, Page number: 110"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def summate (n) :\n",
+ " total = 0\n",
+ " for i in range (1, n+1) :\n",
+ " total = total + i\n",
+ " return total\n",
+ "\n",
+ "print('Sum of first %d natural numbers is %d' % (n, summate (n)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum of first 6 natural numbers is 21\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.11, Page number: 111"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def factors (n) :\n",
+ " print ('The factors of %d are' % n)\n",
+ " for i in range (1, n+1) :\n",
+ " if n%i == 0 :\n",
+ " print i,\n",
+ "\n",
+ "factors (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 28\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The factors of 28 are\n",
+ "1 2 4 7 14 28\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.12, Page number: 113"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def multiple (n) :\n",
+ " print ('The first 10 multiples of %d are' % n)\n",
+ " for i in range (1, 11) :\n",
+ " print('%d X %d = %d' % (n, i, n*i))\n",
+ "\n",
+ "multiple (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The first 10 multiples of 6 are\n",
+ "6 X 1 = 6\n",
+ "6 X 2 = 12\n",
+ "6 X 3 = 18\n",
+ "6 X 4 = 24\n",
+ "6 X 5 = 30\n",
+ "6 X 6 = 36\n",
+ "6 X 7 = 42\n",
+ "6 X 8 = 48\n",
+ "6 X 9 = 54\n",
+ "6 X 10 = 60\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.13, Page number: 114"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def sumofdigit (n) :\n",
+ " sum = 0\n",
+ " while n>0 :\n",
+ " sum = sum + n%10\n",
+ " n = n/10\n",
+ " return sum\n",
+ "\n",
+ "print ('The sum of digits of %d is %d' % (n, sumofdigit (n)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 5214\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum of digits of 5214 is 12\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.14, Page number: 116"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def prime (n) :\n",
+ " count = 0\n",
+ " for i in range (2, n) :\n",
+ " if n%i == 0 :\n",
+ " count = count + 1\n",
+ " break\n",
+ "\n",
+ " if count == 0 :\n",
+ " print ('The number %d is prime' % n)\n",
+ " else :\n",
+ " print ('The number %d is composite' % n)\n",
+ "\n",
+ "prime (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 14\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The number 14 is composite\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.15, Page number: 118"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "print ('%d terms of Fibonacci series are ' % n)\n",
+ "\n",
+ "def printfibo (n) :\n",
+ " first = 0\n",
+ " second = 1\n",
+ " count = 3\n",
+ "\n",
+ " print ('%d %d ' % (first, second)),\n",
+ "\n",
+ " while count<=n :\n",
+ " third = first + second\n",
+ " first = second\n",
+ " second = third\n",
+ " print (' %d ' % third), \n",
+ " count = count + 1\n",
+ "\n",
+ "printfibo (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10 terms of Fibonacci series are \n",
+ "0 1 1 2 3 5 8 13 21 34 \n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.16, Page number: 120"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def sumofdigit (n) :\n",
+ " sum = n\n",
+ " while sum>=10 :\n",
+ " sum = 0\n",
+ " while n>0 :\n",
+ " sum = sum + n%10\n",
+ " n = n/10\n",
+ " n = sum\n",
+ " return sum\n",
+ "\n",
+ "print ('Sum of digits of %d is %d' % (n, sumofdigit (n)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 8626\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum of digits of 8626 is 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.17, Page number: 122"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "print ('Prime numbers upto %d are ' % n)\n",
+ "\n",
+ "def printprime (n) :\n",
+ " for num in range (2, n+1) :\n",
+ " flag = 1\n",
+ " for i in range (2, num) :\n",
+ " if num % i == 0 : \n",
+ " flag = 0\n",
+ " if flag :\n",
+ " print ('%d ' % num),\n",
+ "\n",
+ "printprime (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 40\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Prime numbers upto 40 are \n",
+ "2 3 5 7 11 13 17 19 23 29 31 37 \n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.18, Page number: 124"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def pattern (n) :\n",
+ " for lin in range (1, n+1) :\n",
+ " for count in range (1, lin+1) :\n",
+ " print ('%d ' % count),\n",
+ " print\n",
+ "\n",
+ "pattern (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 \n",
+ "1 2 \n",
+ "1 2 3 \n",
+ "1 2 3 4 \n"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.19, Page number: 126"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def pattern (n) :\n",
+ " for lin in range (1, n+1) :\n",
+ " for count in range (lin, 2*lin) :\n",
+ " print ('%d ' % count),\n",
+ " print\n",
+ "\n",
+ "pattern (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 \n",
+ "2 3 \n",
+ "3 4 5 \n",
+ "4 5 6 7 \n"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.20, Page number: 128"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def pattern (n) :\n",
+ " for lin in range (1, n+1) :\n",
+ " for blk in range (1, n-lin+1) :\n",
+ " print ' ',\n",
+ " for up in range (1, lin+1) :\n",
+ " print ('%d' % up),\n",
+ " down = lin-1\n",
+ " while down>0 :\n",
+ " print ('%d' % down),\n",
+ " down = down-1\n",
+ " print\n",
+ "\n",
+ "pattern (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 1\n",
+ " 1 2 1\n",
+ " 1 2 3 2 1\n",
+ "1 2 3 4 3 2 1\n"
+ ]
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4.21, Page number: 131"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter a number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def pattern (n) :\n",
+ " for lin in range (1, n+1) :\n",
+ " for blk in range (1, n-lin+1) :\n",
+ " print ' ',\n",
+ " for up in range (lin, 2*lin) :\n",
+ " print ('%d' % (up%10)),\n",
+ " down = 2*lin-2\n",
+ " while down>=lin :\n",
+ " print ('%d' % (down%10)),\n",
+ " down = down-1\n",
+ " print\n",
+ "\n",
+ "pattern (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 1\n",
+ " 2 3 2\n",
+ " 3 4 5 4 3\n",
+ "4 5 6 7 6 5 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 24
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file
diff --git a/Magnifying_C_by_Arpita_Goyal/Chapter_5_3.ipynb b/Magnifying_C_by_Arpita_Goyal/Chapter_5_3.ipynb
new file mode 100755
index 00000000..b4cb2db2
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/Chapter_5_3.ipynb
@@ -0,0 +1,909 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 5: Pointers"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.1, Page number: 135"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('The original values are a = %d b = %d' % (a, b))\n",
+ "temp = a\n",
+ "a = b\n",
+ "b = temp\n",
+ "\n",
+ "print ('The values after swapping are a = %d b = %d' % (a, b))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 15\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 27\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The original values are a = 15 b = 27\n",
+ "The values after swapping are a = 27 b = 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.2, Page number: 136"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "print ('The original values are a = %d b = %d' % (a, b))\n",
+ "\n",
+ "def swap (a, b) :\n",
+ " temp = a\n",
+ " a = b\n",
+ " b = temp\n",
+ " print ('The values after swapping are a = %d b = %d' % (a, b))\n",
+ "\n",
+ "swap (a, b)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 15\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 27\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The original values are a = 15 b = 27\n",
+ "The values after swapping are a = 27 b = 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.3, Page number: 141"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "print ('The original values are a = %d b = %d' % (a, b))\n",
+ "\n",
+ "def swap (a, b) :\n",
+ " temp = a\n",
+ " a = b\n",
+ " b = temp\n",
+ " print ('The values after swapping are a = %d b = %d' % (a, b))\n",
+ "\n",
+ "swap (a, b)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 15\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 27\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The original values are a = 15 b = 27\n",
+ "The values after swapping are a = 27 b = 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.4, Page number: 145"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "print ('In main the original values are a = %d b = %d' % (a, b))\n",
+ "print ('In main the address of a is %x and address of b is %x' % (a, b))\n",
+ "\n",
+ "def swap (a, b) :\n",
+ " print ('In swap the address in a is %x and b is %x' % (a, b))\n",
+ " temp = a\n",
+ " a = b\n",
+ " b = temp\n",
+ " print ('The values after swapping are a = %d b = %d' % (a, b))\n",
+ "\n",
+ "swap (a, b)\n",
+ "\n",
+ "print ('In main the values after swapping are a = %d b = %d' % (a, b))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 15\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 27\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "In main the original values are a = 15 b = 27\n",
+ "In main the address of a is f and address of b is 1b\n",
+ "In swap the address in a is f and b is 1b\n",
+ "The values after swapping are a = 27 b = 15\n",
+ "In main the values after swapping are a = 15 b = 27\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.5, Page number: 148"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "print ('In main the value of a = %d b = %d' % (a, b))\n",
+ "\n",
+ "def doooo (a, b) :\n",
+ " a = a+10\n",
+ " b = b+10\n",
+ "\n",
+ "doooo (a, b)\n",
+ "\n",
+ "print ('In main the value of a = %d b = %d' % (a, b))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "In main the value of a = 5 b = 7\n",
+ "In main the value of a = 5 b = 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.6, Page number: 150"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "print ('In main the value of a = %d b = %d' % (a, b))\n",
+ "print ('In main the addresses are a = %s b = %s' % (hex(id(a)), hex(id(b))))\n",
+ "\n",
+ "def doooo (a, b) :\n",
+ " print ('In doooo the addresses are a = %s b = %s' % (hex(id(a)), hex(id(b))))\n",
+ " a = a+10\n",
+ " b = b+10\n",
+ "\n",
+ "doooo (a, b)\n",
+ "\n",
+ "print ('In main the value of a = %d b = %d' % (a, b))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "In main the value of a = 5 b = 7\n",
+ "In main the addresses are a = 0x1b49138 b = 0x1b49108\n",
+ "In doooo the addresses are a = 0x1b49138 b = 0x1b49108\n",
+ "In main the value of a = 5 b = 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.7, Page number: 152"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number '))\n",
+ "b = int(raw_input('Enter second number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def add (a, b) :\n",
+ " x = a+b\n",
+ " return x\n",
+ "\n",
+ "som = add (a, b)\n",
+ "\n",
+ "print ('The sum of %d + %d = %d' % (a, b, som))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum of 2 + 3 = 5\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.8, Page number: 154"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number '))\n",
+ "b = int(raw_input('Enter second number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def greater (a, b) :\n",
+ " if a>b :\n",
+ " big = a\n",
+ " else :\n",
+ " big = b\n",
+ " return big\n",
+ "\n",
+ "great = greater (a, b)\n",
+ "\n",
+ "print ('The greater number is %d' % great)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number 45\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number 23\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The greater number is 45\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.9, Page number: 156"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number '))\n",
+ "b = int(raw_input('Enter second number '))\n",
+ "c = int(raw_input('Enter third number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def greater (a, b, c) :\n",
+ " if a>b :\n",
+ " if a>c :\n",
+ " big = a\n",
+ " else :\n",
+ " big = c\n",
+ " else :\n",
+ " if b>c :\n",
+ " big = b\n",
+ " else :\n",
+ " big = c\n",
+ " return big\n",
+ "\n",
+ "great = greater (a, b, c)\n",
+ "\n",
+ "print ('The greatest number is %d' % great)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number 45\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number 190\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter third number 128\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The greatest number is 190\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.10, Page number: 158"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def summate (n) :\n",
+ " i = 1\n",
+ " total = 0\n",
+ " while i <= n :\n",
+ " total = total + i\n",
+ " i = i+1\n",
+ " return total\n",
+ "\n",
+ "sumn = summate (n)\n",
+ "\n",
+ "print ('Sum of first %d natural numbers is %d' % (n, sumn))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum of first 5 natural numbers is 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.11, Page number: 160"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def sumofdigit (n) :\n",
+ " sum = 0\n",
+ " while n > 0 :\n",
+ " sum = sum + n%10\n",
+ " n = n/10\n",
+ " return sum\n",
+ "\n",
+ "result = sumofdigit (n)\n",
+ "\n",
+ "print ('Sum of digits of %d is %d' % (n, result))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 7162\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum of digits of 7162 is 16\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.12, Page number: 161"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "sum = n\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def sumofdigit (n, sum) :\n",
+ " while sum>=10 :\n",
+ " sum = 0\n",
+ " while n>0 :\n",
+ " sum = sum + n%10\n",
+ " n = n/10\n",
+ " n = sum\n",
+ " return sum\n",
+ "\n",
+ "result = sumofdigit (n, sum)\n",
+ "\n",
+ "print ('Sum of digits of %d is %d' % (n, result))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 7162\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum of digits of 7162 is 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.13, Page number: 163"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def checkprime (n) :\n",
+ " flag = 1\n",
+ " i = 2\n",
+ " while i<n and flag :\n",
+ " if n%i == 0 :\n",
+ " flag = 0\n",
+ " i = i+1\n",
+ " return flag\n",
+ "\n",
+ "result = checkprime (n)\n",
+ "\n",
+ "if result :\n",
+ " print ('%d is Prime ' % n)\n",
+ "else :\n",
+ " print ('%d is Composite ' % n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 41\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "41 is Prime \n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.14, Page number: 165"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def checkprime (n) :\n",
+ " for num in range (2, n+1) :\n",
+ " flag = 1\n",
+ " for i in range (2, num) :\n",
+ " if num % i == 0 : \n",
+ " flag = 0\n",
+ " if flag :\n",
+ " print ('%d ' % num),\n",
+ "\n",
+ "checkprime (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 11\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2 3 5 7 11 \n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.15, Page number: 167"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def makethird (n) :\n",
+ " first = 0\n",
+ " second = 1\n",
+ " count = 3\n",
+ " print ('%d terms of Fibonacci series are ' % n)\n",
+ " print ('%d %d ' % (first, second)),\n",
+ " while count<=n :\n",
+ " third = first + second\n",
+ " first = second\n",
+ " second = third\n",
+ " print (' %d ' % third),\n",
+ " count = count + 1\n",
+ "\n",
+ "makethird (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10 terms of Fibonacci series are \n",
+ "0 1 1 2 3 5 8 13 21 34 \n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.16, Page number: 169"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def makethird (n) :\n",
+ " first = 0\n",
+ " second = 1\n",
+ " third = 0\n",
+ " print ('Fibonacci series up to %d is ' % n)\n",
+ " print first,\n",
+ " while third<=n :\n",
+ " print (' %d ' % second),\n",
+ " third = first + second\n",
+ " first = second\n",
+ " second = third\n",
+ "\n",
+ "makethird (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 100\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Fibonacci series up to 100 is \n",
+ "0 1 1 2 3 5 8 13 21 34 55 89 \n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file
diff --git a/Magnifying_C_by_Arpita_Goyal/Chapter_6_3.ipynb b/Magnifying_C_by_Arpita_Goyal/Chapter_6_3.ipynb
new file mode 100755
index 00000000..690f5ef0
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/Chapter_6_3.ipynb
@@ -0,0 +1,951 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 6: Storage classes"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.1, Page number: 180"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def fact (n) :\n",
+ " prod = 1\n",
+ " while n :\n",
+ " prod = prod * n\n",
+ " n = n - 1\n",
+ " return prod\n",
+ "\n",
+ "print ('Factorial of %d is %d' % (n, fact(n)))\n",
+ "\n",
+ "m = int(raw_input('Enter another number '))\n",
+ "\n",
+ "print ('Factorial of %d is %d' % (m, fact(m)))\n",
+ "\n",
+ "print ('Factorial of 6 is %d' % fact(6))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Factorial of 5 is 120\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter another number 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Factorial of 3 is 6\n",
+ "Factorial of 6 is 720\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.2, Page number: 183"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number '))\n",
+ "b = int(raw_input('Enter second number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def add (a, b) :\n",
+ " global som\n",
+ " som = a + b\n",
+ "\n",
+ "add (a, b)\n",
+ "\n",
+ "print ('The sum of %d + %d = %d' % (a, b, som))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum of 2 + 3 = 5\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.3, Page number: 187"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "global y\n",
+ "y = 90\n",
+ "m = 10\n",
+ "z = 2.18\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def fun1 (m, y, z) :\n",
+ " z = 3.141\n",
+ " print ('%f' % z)\n",
+ "\n",
+ "fun1 (m, y, z)\n",
+ "\n",
+ "print ('%d %d %f' % (m, y, z))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3.141000\n",
+ "10 90 2.180000\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.4, Page number: 190"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "global x, y, z, ch, tmp\n",
+ "z = 2.18\n",
+ "y = 90\n",
+ "ch = '#'\n",
+ "m = 10\n",
+ "x = '*'\n",
+ "tmp = '^'\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def fun1 (m, y, z) :\n",
+ " z = 3.141\n",
+ " num = 100\n",
+ " tmp = '$'\n",
+ " x = 50\n",
+ " ch = '@'\n",
+ " print ('In FUNCTION FORMAL PARAMETERS')\n",
+ " print ('In the function value of m = %d address of m = %x' % (m, id(m)))\n",
+ " print ('In the function value of y = %d address of y = %x' % (y, id(y)))\n",
+ " print ('In the function value of z = %f address of z = %x' % (z, id(z)))\n",
+ "\n",
+ " print ('In FUNCTION LOCAL VARIABLES')\n",
+ " print ('In the function value of z = %f address of z = %x' % (z, id(z)))\n",
+ " print ('In the function value of num = %d address of num = %x' % (num, id(num)))\n",
+ " print ('In the function value of tmp = %s address of tmp = %x' % (tmp, id(tmp)))\n",
+ "\n",
+ " print ('In FUNCTION GLOBAL VARIABLES')\n",
+ " print ('In the function value of x = %s address of x = %x' % (x, id(x)))\n",
+ " print ('In the function value of y = %d address of y = %x' % (y, id(y)))\n",
+ " print ('In the function value of ch = %s address of ch = %x' % (ch, id(ch)))\n",
+ "\n",
+ "\n",
+ "print ('In MAIN LOCAL VARIABLES')\n",
+ "print ('In Main the value of m = %d address of m = %x' % (m, id(m)))\n",
+ "print ('In Main the value of x = %s address of x = %s' % (x, id(x)))\n",
+ "print ('In MAIN GLOBAL VARIABLES')\n",
+ "print ('In Main the value of y = %d address of y = %x' % (y, id(y)))\n",
+ "print ('In Main the value of z = %f address of z = %x' % (z, id(z)))\n",
+ "print ('In Main the value of ch = %s address of ch = %s' % (ch, id(ch)))\n",
+ "print ('In Main the value of tmp = %s address of tmp = %x' % (tmp, id(tmp)))\n",
+ "\n",
+ "fun1 (m, y, z)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "In MAIN LOCAL VARIABLES\n",
+ "In Main the value of m = 10 address of m = 117d0c0\n",
+ "In Main the value of x = * address of x = 140148786619376\n",
+ "In MAIN GLOBAL VARIABLES\n",
+ "In Main the value of y = 90 address of y = 117d8d0\n",
+ "In Main the value of z = 2.180000 address of z = 16ca340\n",
+ "In Main the value of ch = # address of ch = 140148787513184\n",
+ "In Main the value of tmp = ^ address of tmp = 7f76eea46eb8\n",
+ "In FUNCTION FORMAL PARAMETERS\n",
+ "In the function value of m = 10 address of m = 117d0c0\n",
+ "In the function value of y = 90 address of y = 117d8d0\n",
+ "In the function value of z = 3.141000 address of z = 16ca2f8\n",
+ "In FUNCTION LOCAL VARIABLES\n",
+ "In the function value of z = 3.141000 address of z = 16ca2f8\n",
+ "In the function value of num = 100 address of num = 117d7e0\n",
+ "In the function value of tmp = $ address of tmp = 7f76eeb088a0\n",
+ "In FUNCTION GLOBAL VARIABLES\n",
+ "In the function value of x = 50 address of x = 117d4c8\n",
+ "In the function value of y = 90 address of y = 117d8d0\n",
+ "In the function value of ch = @ address of ch = 7f76ed6203f0\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.5, Page number: 193"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number '))\n",
+ "b = int(raw_input('Enter second number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def add (a, b) :\n",
+ " global som\n",
+ " som = a + b\n",
+ "\n",
+ "add (a, b)\n",
+ "\n",
+ "print ('The sum of %d + %d = %d' % (a, b, som))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number 20\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number 30\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum of 20 + 30 = 50\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.6, Page number: 194"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number '))\n",
+ "b = int(raw_input('Enter second number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def greater (a, b) :\n",
+ " global big\n",
+ " if a == b :\n",
+ " big = 'equal'\n",
+ " else :\n",
+ " if a > b :\n",
+ " big = 'first'\n",
+ " else :\n",
+ " big = 'second'\n",
+ " return big\n",
+ "\n",
+ "greater (a, b)\n",
+ "\n",
+ "if big == 'equal' :\n",
+ " print ('EQUAL')\n",
+ "else :\n",
+ " if big == 'first' :\n",
+ " print ('First number is greater %d' % a)\n",
+ " else :\n",
+ " print ('Second number is greater %d' % b)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number 20\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number 20\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "EQUAL\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.7, Page number: 196"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number '))\n",
+ "b = int(raw_input('Enter second number '))\n",
+ "c = int(raw_input('Enter third number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def greater (a, b, c) :\n",
+ " if a>b :\n",
+ " if a>c :\n",
+ " print ('The greatest number is %d ' % a)\n",
+ " else :\n",
+ " print ('The greatest number is %d ' % c)\n",
+ "\n",
+ " else :\n",
+ " if b>c :\n",
+ " print ('The greatest number is %d ' % b)\n",
+ " else : \n",
+ " print ('The greatest number is %d ' % c)\n",
+ "\n",
+ "greater (a, b, c)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number 190\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number 128\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter third number 45\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The greatest number is 190 \n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.8, Page number: 198"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def summate (n) :\n",
+ " i = 1\n",
+ " global total\n",
+ " total = 0\n",
+ " while i <= n :\n",
+ " total = total + i\n",
+ " i = i+1\n",
+ " \n",
+ "summate (n)\n",
+ "\n",
+ "print ('Sum of first %d natural numbers is %d' % (n, total))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum of first 6 natural numbers is 21\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.9, Page number: 199"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def sumofdigit (n) :\n",
+ " global sum\n",
+ " sum = 0\n",
+ " while n > 0 :\n",
+ " sum = sum + n%10\n",
+ " n = n/10\n",
+ " return sum\n",
+ "\n",
+ "sumofdigit (n)\n",
+ "\n",
+ "print ('Sum of digits of %d is %d' % (n, sum))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 7612\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum of digits of 7612 is 16\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.10, Page number: 201"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def checkprime (n) :\n",
+ " flag = 1\n",
+ " i = 2\n",
+ " while i<n and flag :\n",
+ " if n%i == 0 :\n",
+ " flag = 0\n",
+ " i = i+1\n",
+ " return flag\n",
+ "\n",
+ "result = checkprime (n)\n",
+ "\n",
+ "if result :\n",
+ " print ('The number %d is PRIME ' % n)\n",
+ "else :\n",
+ " print ('The number %d is COMPOSITE ' % n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 14\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The number 14 is COMPOSITE \n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.11, Page number: 203"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def makethird (n) :\n",
+ " first = 0\n",
+ " second = 1\n",
+ " count = 3\n",
+ " print ('%d terms of the Fibonacci series are ' % n)\n",
+ " print ('%d %d ' % (first, second)),\n",
+ " while count<=n :\n",
+ " third = first + second\n",
+ " first = second\n",
+ " second = third\n",
+ " print (' %d ' % third),\n",
+ " count = count + 1\n",
+ "\n",
+ "makethird (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10 terms of the Fibonacci series are \n",
+ "0 1 1 2 3 5 8 13 21 34 \n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.12, Page number: 205"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "sum = n\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def sumofdigit (n, sum) :\n",
+ " while sum>=10 :\n",
+ " sum = 0\n",
+ " while n>0 :\n",
+ " sum = sum + n%10\n",
+ " n = n/10\n",
+ " n = sum\n",
+ " return sum\n",
+ "\n",
+ "result = sumofdigit (n, sum)\n",
+ "\n",
+ "print ('Sum of digits of %d is %d' % (n, result))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 7612\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum of digits of 7612 is 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.13, Page number: 207"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def prime (n) :\n",
+ " for num in range (2, n) :\n",
+ " flag = 1\n",
+ " for i in range (2, num) :\n",
+ " if num % i == 0 : \n",
+ " flag = 0\n",
+ " if flag :\n",
+ " print ('%d ' % num),\n",
+ "\n",
+ "prime (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 20\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2 3 5 7 11 13 17 19 \n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.14, Page number: 210"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "m = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def fun1 (m) :\n",
+ " if not hasattr(fun1, \"temp\") : \n",
+ " fun1.temp = 0\n",
+ " print ('In function value of temp = %d' % fun1.temp)\n",
+ " if fun1.temp < m :\n",
+ " fun1.temp = m\n",
+ " else :\n",
+ " fun1.temp = fun1.temp + 1\n",
+ "\n",
+ "for i in range (1, 6) :\n",
+ " fun1 (m)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "In function value of temp = 0\n",
+ "In function value of temp = 10\n",
+ "In function value of temp = 11\n",
+ "In function value of temp = 12\n",
+ "In function value of temp = 13\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.15, Page number: 212"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "m = 10\n",
+ "i = 1\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def fun1 (m) :\n",
+ " x = m\n",
+ " if not hasattr(fun1, \"temp\") : \n",
+ " fun1.temp = 0\n",
+ "\n",
+ " if fun1.temp == 0 :\n",
+ " print ('\\nIn FUNCTION Global variable')\n",
+ " print ('Value of m = %d address of m = %x' % (m, id(m)))\n",
+ " print ('\\nIn FUNCTION Formal parameter variable')\n",
+ " print ('Value of x = %d address of x = %x' % (x, id(x)))\n",
+ " print ('\\nIn FUNCTION Static variable')\n",
+ " print ('Value of temp = %d address of temp = %x' % (fun1.temp, id(fun1.temp)))\n",
+ "\n",
+ " if fun1.temp < m :\n",
+ " fun1.temp = m\n",
+ "\n",
+ " else :\n",
+ " print ('Value of temp = %d' % fun1.temp)\n",
+ " fun1.temp = fun1.temp + 1\n",
+ "\n",
+ "print ('\\nIn MAIN Global variable')\n",
+ "print ('Value of m = %d address of m = %x' % (m, id(m)))\n",
+ "print ('\\nIn MAIN Local variable')\n",
+ "print ('Value of i = %d address of i = %x' % (i, id(i)))\n",
+ " \n",
+ "for i in range (1, 6) :\n",
+ " fun1 (m)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "In MAIN Global variable\n",
+ "Value of m = 10 address of m = 117d0c0\n",
+ "\n",
+ "In MAIN Local variable\n",
+ "Value of i = 1 address of i = 117d198\n",
+ "\n",
+ "In FUNCTION Global variable\n",
+ "Value of m = 10 address of m = 117d0c0\n",
+ "\n",
+ "In FUNCTION Formal parameter variable\n",
+ "Value of x = 10 address of x = 117d0c0\n",
+ "\n",
+ "In FUNCTION Static variable\n",
+ "Value of temp = 0 address of temp = 117d1b0\n",
+ "Value of temp = 10\n",
+ "Value of temp = 11\n",
+ "Value of temp = 12\n",
+ "Value of temp = 13\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6.16, Page number: 215"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter number of Fibonacci terms required '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def nextfibo () :\n",
+ " if not hasattr(nextfibo, \"first\") : \n",
+ " nextfibo.first = 0\n",
+ " if not hasattr(nextfibo, \"second\") : \n",
+ " nextfibo.second = 1\n",
+ " if not hasattr(nextfibo, \"third\") : \n",
+ " nextfibo.third = 0\n",
+ " nextfibo.third = nextfibo.first + nextfibo.second\n",
+ " nextfibo.first = nextfibo.second\n",
+ " nextfibo.second = nextfibo.third\n",
+ " return nextfibo.first\n",
+ " \n",
+ "print ('%d terms of Fibonacci series are' % n) \n",
+ "print 0,\n",
+ "for count in range (2, n+1) :\n",
+ " result = nextfibo ()\n",
+ " print ('%d' % result),"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter number of Fibonacci terms required 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10 terms of Fibonacci series are\n",
+ "0 1 1 2 3 5 8 13 21 34\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file
diff --git a/Magnifying_C_by_Arpita_Goyal/Chapter_7_3.ipynb b/Magnifying_C_by_Arpita_Goyal/Chapter_7_3.ipynb
new file mode 100755
index 00000000..73eec220
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/Chapter_7_3.ipynb
@@ -0,0 +1,862 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 7: Recursion"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.1, Page number: 219"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration, calculation and result\n",
+ "str = raw_input('Enter a string: ')\n",
+ "\n",
+ "print str"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a string: HELLO\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "HELLO\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.2, Page number: 221"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration, calculation and result\n",
+ "str = raw_input('Enter a string: ')\n",
+ "\n",
+ "print str[::-1]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a string: HELLO\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "OLLEH\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.3, Page number: 224"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def fact (n) :\n",
+ " if n>0 :\n",
+ " return n * fact (n-1)\n",
+ " else :\n",
+ " return 1\n",
+ "\n",
+ "print ('Factorial of %d is %d' % (n, fact (n)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Factorial of 5 is 120\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.4, Page number: 227"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def printnum (n) :\n",
+ " if n>0 :\n",
+ " printnum (n-1)\n",
+ " print n,\n",
+ "\n",
+ "print ('Counting up to %d' % n)\n",
+ "printnum (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Counting up to 10\n",
+ "1 2 3 4 5 6 7 8 9 10\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.5, Page number: 229"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def printeven (n) :\n",
+ "\tif not hasattr(printeven, \"x\") : \n",
+ "\t\tprinteven.x = 2\n",
+ " \n",
+ "\tif printeven.x <= n :\n",
+ "\t\tprint printeven.x,\n",
+ "\t\tprinteven.x = printeven.x + 2\n",
+ "\t\tprinteven (n)\n",
+ "\n",
+ "print ('Even numbers up to %d are' % n)\n",
+ "printeven (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Even numbers up to 10 are\n",
+ "2 4 6 8 10\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.6, Page number: 231"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def printodd (x, n) :\n",
+ "\tif x <= n : \n",
+ "\t\tprint x,\n",
+ "\t\tprintodd (x+2, n)\n",
+ "\n",
+ "print ('Odd numbers up to %d are' % n)\n",
+ "printodd (1, n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Odd numbers up to 10 are\n",
+ "1 3 5 7 9\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.7, Page number: 232"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def printnum (fro, to) :\n",
+ "\tif fro <= to : \n",
+ "\t\tprint fro,\n",
+ "\t\tprintnum (fro+1, to)\n",
+ "\n",
+ "print ('Number line for %d is' % n)\n",
+ "printnum (-n, n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Number line for 5 is\n",
+ "-5 -4 -3 -2 -1 0 1 2 3 4 5\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.8, Page number: 234"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def summate (n) :\n",
+ "\tif n > 0 : \n",
+ "\t\treturn n + summate (n-1)\n",
+ "\telse :\n",
+ "\t\treturn 0\n",
+ "\n",
+ "print ('Sum of first %d natural numbers is %d' % (n, summate (n)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum of first 10 natural numbers is 55\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.9, Page number: 237"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def factors (n) :\n",
+ "\tif not hasattr(factors, \"i\") : \n",
+ "\t\tfactors.i = 1\n",
+ "\t\t\n",
+ "\tif factors.i <= n : \n",
+ "\t\tif n % factors.i == 0 :\n",
+ "\t\t\tprint factors.i,\n",
+ "\t\tfactors.i = factors.i + 1\n",
+ "\t\tfactors (n)\n",
+ "\n",
+ "print ('The factors of %d are' % n)\n",
+ "factors (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 28\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The factors of 28 are\n",
+ "1 2 4 7 14 28\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.10, Page number: 239"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def multiple (n) :\n",
+ "\tif not hasattr(multiple, \"i\") : \n",
+ "\t\tmultiple.i = 1\n",
+ "\t\t\n",
+ "\tif multiple.i <= 10 : \n",
+ "\t\tprint ('%d X %d = %d' % (n, multiple.i, n*multiple.i))\n",
+ "\t\tmultiple.i = multiple.i + 1\n",
+ "\t\tmultiple (n)\n",
+ "\n",
+ "print ('The first 10 multiples of %d are' % n)\n",
+ "multiple (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The first 10 multiples of 6 are\n",
+ "6 X 1 = 6\n",
+ "6 X 2 = 12\n",
+ "6 X 3 = 18\n",
+ "6 X 4 = 24\n",
+ "6 X 5 = 30\n",
+ "6 X 6 = 36\n",
+ "6 X 7 = 42\n",
+ "6 X 8 = 48\n",
+ "6 X 9 = 54\n",
+ "6 X 10 = 60\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.11, Page number: 241"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def checkprime (n) :\n",
+ " flag = 1\n",
+ " i = 2\n",
+ " while i<n and flag :\n",
+ " if n%i == 0 :\n",
+ " flag = 0\n",
+ " i = i+1\n",
+ " return flag\n",
+ "\n",
+ "result = checkprime (n)\n",
+ "\n",
+ "if result :\n",
+ " print ('The number %d is PRIME ' % n)\n",
+ "else :\n",
+ " print ('The number %d is COMPOSITE ' % n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 19\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The number 19 is PRIME \n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.12, Page number: 243"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def Fibonacci (n) :\n",
+ "\tif n < 2 :\n",
+ "\t\treturn n\n",
+ "\telse :\n",
+ "\t\treturn Fibonacci (n-1) + Fibonacci (n-2)\n",
+ "\t\n",
+ "\n",
+ "print ('The %d terms of Fibonacci series are' % n)\n",
+ "for i in range (0, n) :\n",
+ "\tprint ('%d' % Fibonacci (i)),"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The 10 terms of Fibonacci series are\n",
+ "0 1 1 2 3 5 8 13 21 34\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.13, Page number: 244"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "sum = n\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def sumofdigit (n, sum) :\n",
+ " while sum>=10 :\n",
+ " sum = 0\n",
+ " while n>0 :\n",
+ " sum = sum + n%10\n",
+ " n = n/10\n",
+ " n = sum\n",
+ " return sum\n",
+ "\n",
+ "result = sumofdigit (n, sum)\n",
+ "\n",
+ "print ('Sum of digits of %d is %d' % (n, result))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 8626\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum of digits of 8626 is 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.14, Page number: 245"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def prime (n) :\n",
+ " for num in range (3, n+1) :\n",
+ " flag = 1\n",
+ " for i in range (2, num) :\n",
+ " if num % i == 0 : \n",
+ " flag = 0\n",
+ " if flag :\n",
+ " print ('%d ' % num),\n",
+ "\n",
+ "print ('Prime numbers upto %d are' % n)\n",
+ "prime (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 30\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Prime numbers upto 30 are\n",
+ "3 5 7 11 13 17 19 23 29 \n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.15, Page number: 246"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int (raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def pattern (n) :\n",
+ "\tif n > 0 :\n",
+ "\t\tpattern (n-1)\n",
+ "\t\tfor count in range (1, n+1) :\n",
+ "\t\t\tprint ('%d' % count),\n",
+ " \tprint\n",
+ " \t\n",
+ "pattern (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1\n",
+ "1 2\n",
+ "1 2 3\n",
+ "1 2 3 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.16, Page number: 249"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def printline (x, total) :\n",
+ "\tif x > 0 :\n",
+ "\t\tprintline (x-1, total)\n",
+ "\t\tfor blk in range (1, total-x+1) :\n",
+ "\t\t\tprint ' ',\n",
+ "\t\tfor up in range (1, x+1) :\n",
+ "\t\t\tprint ('%d' % up),\n",
+ "\t\tdown = x-1\n",
+ "\t\twhile down>0 :\n",
+ "\t\t\tprint ('%d' % down),\n",
+ "\t\t\tdown = down-1\n",
+ "\t\tprint\n",
+ "\n",
+ "printline (n, n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 1\n",
+ " 1 2 1\n",
+ " 1 2 3 2 1\n",
+ "1 2 3 4 3 2 1\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.17, Page number: 253"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Function declaration, calculation and result\n",
+ "def listsum () :\n",
+ "\tn = int(raw_input('Enter any number '))\n",
+ "\tif n > 0 :\n",
+ "\t\treturn n + listsum ()\n",
+ "\telse :\n",
+ "\t\treturn 0\n",
+ "\t\t\n",
+ "sum = listsum ()\n",
+ "print ('Sum of list of numbers is %d' % sum)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number -4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum of list of numbers is 6\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file
diff --git a/Magnifying_C_by_Arpita_Goyal/Chapter_8_3.ipynb b/Magnifying_C_by_Arpita_Goyal/Chapter_8_3.ipynb
new file mode 100755
index 00000000..ccafa905
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/Chapter_8_3.ipynb
@@ -0,0 +1,2718 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 8: Arrays"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.1, Page number: 256"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from array import *\n",
+ "\n",
+ "# Variable declaration\n",
+ "word = array('c', ['H', 'e', 'l', 'l', 'o'])\n",
+ "\n",
+ "# Result\n",
+ "print ('The contents of word [] is %s ' % ''.join(word))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The contents of word [] is Hello \n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.2, Page number: 259"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from array import *\n",
+ "\n",
+ "# Variable declaration\n",
+ "marks = [0]*10\n",
+ "avg = 0\n",
+ "\n",
+ "# Calculation and result\n",
+ "for i in range (0, 10) :\n",
+ "\tmarks[i] = int(raw_input('Enter value in cell [%d] ' % i))\n",
+ "\tavg = avg + marks[i]\n",
+ "\n",
+ "avg = avg/10\n",
+ "print ('The average marks are %f' % avg)\n",
+ "\n",
+ "for i in range (0, 10) :\n",
+ "\tif marks[i] < avg :\n",
+ "\t\tprint ('Marks in cell [%d] are %d less than average' % (i, marks[i]))\n",
+ "\telse :\n",
+ "\t\tprint ('Marks in cell [%d] are %d above/equal to average' % (i, marks[i]))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [0] 45\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [1] 78\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [2] 65\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [3] 43\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [4] 56\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [5] 98\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [6] 53\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [7] 82\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [8] 74\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [9] 39\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The average marks are 63.000000\n",
+ "Marks in cell [0] are 45 less than average\n",
+ "Marks in cell [1] are 78 above/equal to average\n",
+ "Marks in cell [2] are 65 above/equal to average\n",
+ "Marks in cell [3] are 43 less than average\n",
+ "Marks in cell [4] are 56 less than average\n",
+ "Marks in cell [5] are 98 above/equal to average\n",
+ "Marks in cell [6] are 53 less than average\n",
+ "Marks in cell [7] are 82 above/equal to average\n",
+ "Marks in cell [8] are 74 above/equal to average\n",
+ "Marks in cell [9] are 39 less than average\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.3, Page number: 261"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from array import *\n",
+ "\n",
+ "# Variable declaration\n",
+ "data = raw_input('Enter the string ')\n",
+ "\n",
+ "# Calculation and result\n",
+ "rev_data = reversed (data)\n",
+ "\n",
+ "if list (data) == list (rev_data) :\n",
+ "\tprint ('The string %s is a PALINDROME' % data)\n",
+ "else :\n",
+ "\tprint ('The string %s is not a PALINDROME' % data)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the string MADAM\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The string MADAM is a PALINDROME\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.4, Page number: 266"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from array import *\n",
+ "\n",
+ "# Variable declaration\n",
+ "marks = [0]*10\n",
+ "avg = 0\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def readdata (marks) :\n",
+ "\tfor i in range (0, 10) :\n",
+ "\t\tmarks[i] = int(raw_input('Enter value in cell [%d] ' % i))\n",
+ "\t\t\n",
+ "def average (marks) :\n",
+ "\tmean = 0\n",
+ "\tfor i in range (0, 10) :\n",
+ "\t\tmean = mean + marks[i]\n",
+ "\treturn mean/10\n",
+ "\n",
+ "def printdata (marks, avg) :\n",
+ "\tfor i in range (0, 10) :\n",
+ "\t\tif marks[i] < avg :\n",
+ "\t\t\tprint ('Marks in cell [%d] are %d less than average' % (i, marks[i]))\n",
+ "\t\telse :\n",
+ "\t\t\tprint ('Marks in cell [%d] are %d above/equal to average' % (i, marks[i]))\t\n",
+ "\n",
+ "\n",
+ "readdata (marks)\n",
+ "avg = average (marks)\t\n",
+ "print ('The average marks are %f' % avg)\n",
+ "printdata (marks, avg)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [0] 45\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [1] 78\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [2] 65\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [3] 43\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [4] 56\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [5] 98\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [6] 53\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [7] 82\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [8] 74\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell [9] 39\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The average marks are 63.000000\n",
+ "Marks in cell [0] are 45 less than average\n",
+ "Marks in cell [1] are 78 above/equal to average\n",
+ "Marks in cell [2] are 65 above/equal to average\n",
+ "Marks in cell [3] are 43 less than average\n",
+ "Marks in cell [4] are 56 less than average\n",
+ "Marks in cell [5] are 98 above/equal to average\n",
+ "Marks in cell [6] are 53 less than average\n",
+ "Marks in cell [7] are 82 above/equal to average\n",
+ "Marks in cell [8] are 74 above/equal to average\n",
+ "Marks in cell [9] are 39 less than average\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.5, Page number: 273"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from array import *\n",
+ "\n",
+ "# Variable declaration\n",
+ "a = [10, 20, 30, 40, 50]\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def fun (x, y, z) :\n",
+ "\tx = x + 5\n",
+ "\ty = y + 10\n",
+ "\tz[4] = z[4] + 100\n",
+ "\n",
+ "\n",
+ "for i in range (0, 5) :\n",
+ "\tprint ('The cell [%d] contains %d' % (i, a[i]))\n",
+ "\tprint ('The address of cell [%d] is %x' % (i, id(a[i])))\n",
+ "\t\t\n",
+ "fun (a[0], a[3], a)\t\n",
+ "print\n",
+ "\n",
+ "for i in range (0, 5) :\n",
+ "\tprint ('The cell [%d] contains %d' % (i, a[i]))\n",
+ "\tprint ('The address of cell [%d] is %x' % (i, id(a[i])))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The cell [0] contains 10\n",
+ "The address of cell [0] is 18bc0c0\n",
+ "The cell [1] contains 20\n",
+ "The address of cell [1] is 18bbfd0\n",
+ "The cell [2] contains 30\n",
+ "The address of cell [2] is 18bbee0\n",
+ "The cell [3] contains 40\n",
+ "The address of cell [3] is 18bc5b8\n",
+ "The cell [4] contains 50\n",
+ "The address of cell [4] is 18bc4c8\n",
+ "\n",
+ "The cell [0] contains 10\n",
+ "The address of cell [0] is 18bc0c0\n",
+ "The cell [1] contains 20\n",
+ "The address of cell [1] is 18bbfd0\n",
+ "The cell [2] contains 30\n",
+ "The address of cell [2] is 18bbee0\n",
+ "The cell [3] contains 40\n",
+ "The address of cell [3] is 18bc5b8\n",
+ "The cell [4] contains 150\n",
+ "The address of cell [4] is 18bcaf8\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.6, Page number: 275"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from array import *\n",
+ "\n",
+ "# Variable declaration\n",
+ "data = raw_input('Enter the string ')\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def revstring (data) :\n",
+ "\trev_data = reversed (data)\n",
+ "\n",
+ "\tif list (data) == list (rev_data) :\n",
+ "\t\tprint ('The string %s is a PALINDROME' % data)\n",
+ "\telse :\n",
+ "\t\tprint ('The string %s is not a PALINDROME' % data)\n",
+ "\n",
+ "revstring (data)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the string python\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The string python is not a PALINDROME\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.7, Page number: 277"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from array import *\n",
+ "\n",
+ "# Variable declaration\n",
+ "a = [0]*10\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def readnsort (a) :\n",
+ "\tpos = 0\n",
+ "\tfor len in range (0, 10) :\n",
+ "\t\tnum = int(raw_input('Enter data element no. %d ' % len))\n",
+ "\t\t\n",
+ "\t\tfor pos in range (0, len+1) :\n",
+ "\t\t\tif num < a[pos] :\n",
+ "\t\t\t\tbreak\n",
+ "\t\t\t\t\n",
+ "\t\ti = len\t\t\n",
+ "\t\twhile i>=pos :\n",
+ "\t\t\ta[i] = a[i-1]\n",
+ "\t\t\ti = i-1\n",
+ "\t\ta[pos] = num\n",
+ "\t\t\t\n",
+ "def printdata (a) :\n",
+ "\tfor i in range (0, 10) :\n",
+ "\t\tprint ('The data in cell [%d] is %d ' % (i, a[i]))\t\t\t\n",
+ "\t\t\t\n",
+ "readnsort (a)\n",
+ "print\n",
+ "printdata (a)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data element no. 0 45\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data element no. 1 78\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data element no. 2 65\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data element no. 3 43\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data element no. 4 56\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data element no. 5 98\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data element no. 6 53\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data element no. 7 82\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data element no. 8 74\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data element no. 9 39\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The data in cell [0] is 39 \n",
+ "The data in cell [1] is 43 \n",
+ "The data in cell [2] is 45 \n",
+ "The data in cell [3] is 53 \n",
+ "The data in cell [4] is 56 \n",
+ "The data in cell [5] is 65 \n",
+ "The data in cell [6] is 74 \n",
+ "The data in cell [7] is 78 \n",
+ "The data in cell [8] is 82 \n",
+ "The data in cell [9] is 98 \n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.8, Page number: 279"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from array import *\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def readdata () :\n",
+ "\ta = []\n",
+ "\tfor i in range (0, 10) :\n",
+ "\t\ta.append(int(raw_input('Enter data in cell [%d] ' % i)))\n",
+ "\treturn a\n",
+ "\n",
+ "def sortdata (a) :\n",
+ "\tfor p in range (0, 9) :\n",
+ "\t\tmin = p\n",
+ "\t\tfor i in range (p, 10) :\n",
+ "\t\t\tif a[min] > a[i] :\n",
+ "\t\t\t\tmin = i\n",
+ "\t\ttemp = a[p]\n",
+ "\t\ta[p] = a[min]\n",
+ "\t\ta[min] = temp\n",
+ "\treturn a\n",
+ "\n",
+ "def printdata (a) :\n",
+ "\tfor i in range (0, 10) :\n",
+ "\t\tprint ('The data in cell [%d] is %d ' % (i, a[i]))\n",
+ "\n",
+ "a = readdata ()\n",
+ "a = sortdata (a)\n",
+ "print ('\\nThe sorted data is ')\n",
+ "printdata (a)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [0] 45\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [1] 78\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [2] 65\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [3] 43\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [4] 56\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [5] 98\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [6] 53\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [7] 82\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [8] 74\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [9] 39\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The sorted data is \n",
+ "The data in cell [0] is 39 \n",
+ "The data in cell [1] is 43 \n",
+ "The data in cell [2] is 45 \n",
+ "The data in cell [3] is 53 \n",
+ "The data in cell [4] is 56 \n",
+ "The data in cell [5] is 65 \n",
+ "The data in cell [6] is 74 \n",
+ "The data in cell [7] is 78 \n",
+ "The data in cell [8] is 82 \n",
+ "The data in cell [9] is 98 \n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.9, Page number: 282"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from array import *\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def readdata () :\n",
+ "\ta = []\n",
+ "\tfor i in range (0, 10) :\n",
+ "\t\ta.append(int(raw_input('Enter data in cell [%d] ' % i)))\n",
+ "\treturn a\n",
+ "\n",
+ "def bubble (a) :\n",
+ "\tfor p in range (0, 10) :\n",
+ "\t\tfor i in range (0, 9-p) :\n",
+ "\t\t\tif a[i] > a[i+1] :\n",
+ "\t\t\t\ttemp = a[i]\n",
+ "\t\t\t\ta[i] = a[i+1]\n",
+ "\t\t\t\ta[i+1] = temp\n",
+ "\treturn a\n",
+ "\n",
+ "def printdata (a) :\n",
+ "\tfor i in range (0, 10) :\n",
+ "\t\tprint ('The data in cell [%d] is %d ' % (i, a[i]))\n",
+ "\n",
+ "a = readdata ()\n",
+ "a = bubble (a)\n",
+ "print ('\\nThe sorted data is ')\n",
+ "printdata (a)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [0] 45\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [1] 78\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [2] 65\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [3] 43\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [4] 56\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [5] 98\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [6] 53\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [7] 82\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [8] 74\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [9] 39\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The sorted data is \n",
+ "The data in cell [0] is 39 \n",
+ "The data in cell [1] is 43 \n",
+ "The data in cell [2] is 45 \n",
+ "The data in cell [3] is 53 \n",
+ "The data in cell [4] is 56 \n",
+ "The data in cell [5] is 65 \n",
+ "The data in cell [6] is 74 \n",
+ "The data in cell [7] is 78 \n",
+ "The data in cell [8] is 82 \n",
+ "The data in cell [9] is 98 \n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.10, Page number: 285"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from array import *\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def readdata (size) :\n",
+ "\ta = []\n",
+ "\tfor i in range (0, size) :\n",
+ "\t\ta.append(int(raw_input('Enter data in cell [%d] ' % i)))\n",
+ "\treturn a\n",
+ "\n",
+ "def bubble (a, size) :\n",
+ "\tfor p in range (0, size) :\n",
+ "\t\tfor i in range (0, size-p-1) :\n",
+ "\t\t\tif a[i] > a[i+1] :\n",
+ "\t\t\t\ttemp = a[i]\n",
+ "\t\t\t\ta[i] = a[i+1]\n",
+ "\t\t\t\ta[i+1] = temp\n",
+ "\treturn a\n",
+ "\n",
+ "def printdata (a, size) :\n",
+ "\tfor i in range (0, size) :\n",
+ "\t\tprint ('The data in cell [%d] is %d ' % (i, a[i]))\n",
+ "\n",
+ "def lsearch (a, key, size) :\n",
+ "\tfor i in range (0, size) :\n",
+ "\t\tif a[i] == key :\n",
+ "\t\t\treturn i\n",
+ "\t\tif i == size :\n",
+ "\t\t\treturn -1\n",
+ "\t\t\t\n",
+ "size = int(raw_input('Enter the total number of elements '))\n",
+ "a = readdata (size)\n",
+ "a = bubble (a, size)\n",
+ "print ('\\nThe sorted array is ')\n",
+ "printdata (a, size)\n",
+ "\n",
+ "find = int(raw_input('\\nEnter the element to be searched '))\n",
+ "pos = lsearch (a, find, size)\n",
+ "\n",
+ "if pos<0 :\n",
+ "\tprint ('\\nThe element %d NOT FOUND ' % find)\n",
+ "else :\n",
+ "\tprint ('\\nThe element %d found at position %d ' % (find, pos))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the total number of elements 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [0] 45\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [1] 78\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [2] 65\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [3] 34\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [4] 49\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The sorted array is \n",
+ "The data in cell [0] is 34 \n",
+ "The data in cell [1] is 45 \n",
+ "The data in cell [2] is 49 \n",
+ "The data in cell [3] is 65 \n",
+ "The data in cell [4] is 78 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter the element to be searched 65\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The element 65 found at position 3 \n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.11, Page number: 288"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from array import *\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def readdata (size) :\n",
+ "\ta = []\n",
+ "\tfor i in range (0, size) :\n",
+ "\t\ta.append(int(raw_input('Enter data in cell [%d] ' % i)))\n",
+ "\treturn a\n",
+ "\n",
+ "def bubble (a, size) :\n",
+ "\tfor p in range (0, size) :\n",
+ "\t\tfor i in range (0, size-p-1) :\n",
+ "\t\t\tif a[i] > a[i+1] :\n",
+ "\t\t\t\ttemp = a[i]\n",
+ "\t\t\t\ta[i] = a[i+1]\n",
+ "\t\t\t\ta[i+1] = temp\n",
+ "\treturn a\n",
+ "\n",
+ "def printdata (a, size) :\n",
+ "\tfor i in range (0, size) :\n",
+ "\t\tprint ('The data in cell [%d] is %d ' % (i, a[i]))\n",
+ "\n",
+ "def bsearch (a, key, size) :\n",
+ "\tfirst = 0\n",
+ "\tlast = size - 1\n",
+ "\tmid = (first + last)/2\n",
+ "\t\n",
+ "\twhile (first <= last and a[mid] != key) :\n",
+ "\t\tif a[mid] < key :\n",
+ "\t\t\tfirst = mid + 1\n",
+ "\t\telse :\n",
+ "\t\t\tif a[mid] > key :\n",
+ "\t\t\t\tlast = mid - 1\n",
+ "\t\tmid = (first + last)/2\n",
+ "\t\n",
+ "\tif a[mid] == key :\n",
+ "\t\treturn mid\n",
+ "\telse :\n",
+ "\t\treturn -1\n",
+ "\t\t\t\n",
+ "size = int(raw_input('Enter the number of elements in the array '))\n",
+ "a = readdata (size)\n",
+ "a = bubble (a, size)\n",
+ "print ('\\nThe sorted array is ')\n",
+ "printdata (a, size)\n",
+ "\n",
+ "find = int(raw_input('\\nPlease enter the element to be searched for '))\n",
+ "pos = bsearch (a, find, size)\n",
+ "\n",
+ "if pos<0 :\n",
+ "\tprint ('\\nThe element %d NOT FOUND ' % find)\n",
+ "else :\n",
+ "\tprint ('\\nThe element %d found at position %d ' % (find, pos))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the number of elements in the array 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [0] 45\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [1] 78\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [2] 65\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [3] 34\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [4] 49\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The sorted array is \n",
+ "The data in cell [0] is 34 \n",
+ "The data in cell [1] is 45 \n",
+ "The data in cell [2] is 49 \n",
+ "The data in cell [3] is 65 \n",
+ "The data in cell [4] is 78 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Please enter the element to be searched for 45\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The element 45 found at position 1 \n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.12, Page number: 293"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from array import *\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def readncompute (no_of_students) :\n",
+ "\ta = [[0 for y in xrange(5)] for x in xrange(no_of_students)]\n",
+ "\tper = [0 for x in range(no_of_students)]\n",
+ "\tavg = 0\n",
+ "\t\n",
+ "\tfor i in range (0, no_of_students) :\n",
+ "\t\tprint ('\\nEnter marks of 5 subjects for Roll no. %d ' % (i+1))\n",
+ "\t\t\n",
+ "\t\tfor j in range (0, 5) :\n",
+ "\t\t\ta[i][j] = int(raw_input('Subject %d = ' % (j+1)))\n",
+ "\t\t\tper[i] = per[i] + a[i][j]\n",
+ "\t\t\t\n",
+ "\t\tper[i] = per[i]/5\n",
+ "\t\tavg = avg + per[i]\n",
+ "\t\t\n",
+ "\treturn a, avg/no_of_students, per\n",
+ "\n",
+ "def printdata (a, no_of_students, avg, per) :\n",
+ "\tfor i in range (0, no_of_students) :\n",
+ "\t\tprint ('\\nMarks for Roll no. %d are ' % (i+1))\n",
+ "\t\tfor j in range (0, 5) :\n",
+ "\t\t\tprint a[i][j]\n",
+ "\t\tprint ('Percentage for Roll no. %d is %f ' % ((i+1), per[i]))\n",
+ "\t\t\n",
+ "\t\tif per[i] < avg :\n",
+ "\t\t\tprint ('Below average')\n",
+ "\t\telse :\n",
+ "\t\t\tprint ('Above average')\n",
+ "\t\t\n",
+ "\n",
+ "no_of_students = int(raw_input('How many students marks do you want to enter '))\n",
+ "a, average, per = readncompute (no_of_students)\n",
+ "print ('\\nThe average marks are %f ' % average)\n",
+ "printdata (a, no_of_students, average, per)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many students marks do you want to enter 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter marks of 5 subjects for Roll no. 1 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 1 = 75\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 2 = 43\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 3 = 51\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 4 = 68\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 5 = 73\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter marks of 5 subjects for Roll no. 2 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 1 = 49\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 2 = 71\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 3 = 62\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 4 = 53\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 5 = 50\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter marks of 5 subjects for Roll no. 3 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 1 = 90\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 2 = 82\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 3 = 74\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 4 = 93\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 5 = 89\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The average marks are 68.000000 \n",
+ "\n",
+ "Marks for Roll no. 1 are \n",
+ "75\n",
+ "43\n",
+ "51\n",
+ "68\n",
+ "73\n",
+ "Percentage for Roll no. 1 is 62.000000 \n",
+ "Below average\n",
+ "\n",
+ "Marks for Roll no. 2 are \n",
+ "49\n",
+ "71\n",
+ "62\n",
+ "53\n",
+ "50\n",
+ "Percentage for Roll no. 2 is 57.000000 \n",
+ "Below average\n",
+ "\n",
+ "Marks for Roll no. 3 are \n",
+ "90\n",
+ "82\n",
+ "74\n",
+ "93\n",
+ "89\n",
+ "Percentage for Roll no. 3 is 85.000000 \n",
+ "Above average\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.13, Page number: 296"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Function declaration, calculation and result\n",
+ "def getchoice () :\n",
+ "\tprint\n",
+ "\tprint ('1 - Input two matrices ')\n",
+ "\tprint ('2 - Add and display sum ')\n",
+ "\tprint ('3 - Exit ')\n",
+ "\t\n",
+ "\tchoice = int(raw_input('Enter choice : '))\n",
+ "\treturn choice\n",
+ "\n",
+ "def read (matrix, rows, cols) :\t\n",
+ "\tfor i in range (0, rows) :\n",
+ "\t\tprint ('\\nEnter elements for row %d ' % (i+1))\t\t\n",
+ "\t\tfor j in range (0, cols) :\n",
+ "\t\t\tmatrix[i][j] = int(raw_input('matrix[%d][%d] = ' % ((i+1),(j+1))))\n",
+ "\n",
+ "def add (sum, augend, addend, rows, cols) :\n",
+ "\tfor i in range (0, rows) :\n",
+ "\t\tfor j in range (0, cols) :\n",
+ "\t\t\tsum[i][j] = augend[i][j] + addend[i][j]\n",
+ "\n",
+ "def display2matrices (augend, addend, rows, cols) :\n",
+ "\tprint ('\\nMatrix A ')\n",
+ "\tfor i in range (0, rows) :\n",
+ "\t\tfor j in range (0, cols) :\n",
+ "\t\t\tprint ('%3d ' % augend[i][j]),\n",
+ "\t\tprint\n",
+ "\tprint ('\\nMatrix B ')\n",
+ "\tfor i in range (0, rows) :\n",
+ "\t\tfor j in range (0, cols) :\n",
+ "\t\t\tprint ('%3d ' % addend[i][j]),\n",
+ "\t\tprint\n",
+ "\n",
+ "def display3matrices (sum, augend, addend, rows, cols) :\n",
+ "\tprint\n",
+ "\tfor i in range (0, rows) :\n",
+ "\t\tfor j in range (0, cols) :\n",
+ "\t\t\tprint ('%3d ' % augend[i][j]),\n",
+ "\t\tprint ('%s ' % ('+' if i==rows/2 else ' ')),\n",
+ "\t\t\n",
+ "\t\tfor j in range (0, cols) :\n",
+ "\t\t\tprint ('%2d ' % addend[i][j]),\n",
+ "\t\tprint ('%s ' % ('=' if i==rows/2 else ' ')),\n",
+ "\n",
+ "\t\tfor j in range (0, cols) :\n",
+ "\t\t\tprint ('%2d ' % sum[i][j]),\n",
+ "\t\tprint\n",
+ "\n",
+ "def display (matrix, rows, cols) :\n",
+ "\tfor i in range (0, rows) :\n",
+ "\t\tfor j in range (0, cols) :\n",
+ "\t\t\tprint ('%3d ' % matrix[i][j]),\n",
+ "\t\tprint\n",
+ "\n",
+ "augend = [[0 for y in xrange(10)] for x in xrange(10)]\n",
+ "addend = [[0 for y in xrange(10)] for x in xrange(10)]\n",
+ "sum = [[0 for y in xrange(10)] for x in xrange(10)]\n",
+ "\n",
+ "condition = True\n",
+ "while condition :\n",
+ "\tchoice = getchoice ()\n",
+ "\t\n",
+ "\tif choice == 1 :\n",
+ "\t\trows = int(raw_input('\\nEnter the number of rows '))\t\t\n",
+ "\t\tcols = int(raw_input('Enter the number of columns '))\n",
+ "\t\t\n",
+ "\t\tprint ('\\n\\nEnter the first matrix ')\n",
+ "\t\tread (augend, rows, cols)\n",
+ "\t\tprint ('\\n\\nEnter the second matrix ')\n",
+ "\t\tread (addend, rows, cols)\n",
+ "\t\tprint ('\\n\\nYou entered ')\n",
+ "\t\tdisplay2matrices (augend, addend, rows, cols)\n",
+ "\t\n",
+ "\telif choice == 2 :\n",
+ "\t\tadd (sum, augend, addend, rows, cols)\n",
+ "\t\tdisplay3matrices (sum, augend, addend, rows, cols)\n",
+ "\t\n",
+ "\telif choice == 3 :\n",
+ "\t\tcondition = False\n",
+ "\t\texit ()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1 - Input two matrices \n",
+ "2 - Add and display sum \n",
+ "3 - Exit \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter choice : 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter the number of rows 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the number of columns 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ "Enter the first matrix \n",
+ "\n",
+ "Enter elements for row 1 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[1][1] = 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[1][2] = 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[1][3] = 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter elements for row 2 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[2][1] = 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[2][2] = 4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[2][3] = 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter elements for row 3 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[3][1] = 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[3][2] = 6\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[3][3] = 7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ "Enter the second matrix \n",
+ "\n",
+ "Enter elements for row 1 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[1][1] = 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[1][2] = 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[1][3] = 1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter elements for row 2 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[2][1] = 6\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[2][2] = 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[2][3] = 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter elements for row 3 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[3][1] = 8\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[3][2] = 7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[3][3] = 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ "You entered \n",
+ "\n",
+ "Matrix A \n",
+ " 1 2 3 \n",
+ " 3 4 5 \n",
+ " 5 6 7 \n",
+ "\n",
+ "Matrix B \n",
+ " 3 5 1 \n",
+ " 6 5 3 \n",
+ " 8 7 4 \n",
+ "\n",
+ "1 - Input two matrices \n",
+ "2 - Add and display sum \n",
+ "3 - Exit \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter choice : 2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ " 1 2 3 3 5 1 4 7 4 \n",
+ " 3 4 5 + 6 5 3 = 9 9 8 \n",
+ " 5 6 7 8 7 4 13 13 11 \n",
+ "\n",
+ "1 - Input two matrices \n",
+ "2 - Add and display sum \n",
+ "3 - Exit \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter choice : 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.14, Page number: 300"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Function declaration, calculation and result\n",
+ "def readmatrix (matrix, rows, cols) :\t\n",
+ "\tfor i in range (0, rows) :\n",
+ "\t\tprint ('\\nPlease enter elements for row %d ' % (i+1))\t\t\n",
+ "\t\tfor j in range (0, cols) :\n",
+ "\t\t\tmatrix[i][j] = int(raw_input('matrix[%d][%d] = ' % ((i+1),(j+1))))\n",
+ "\n",
+ "def multiply (prod, a, b, Ar, Ac, Bc) :\n",
+ "\tfor i in range (0, Ar) :\n",
+ "\t\tfor j in range (0, Bc) :\n",
+ "\t\t\tprod[i][j] = 0\n",
+ "\t\t\tfor k in range (0, Ac) :\n",
+ "\t\t\t\tprod[i][j] += a[i][k] * b[k][j]\n",
+ "\n",
+ "def display (matrix, rows, cols) :\n",
+ "\tfor i in range (0, rows) :\n",
+ "\t\tfor j in range (0, cols) :\n",
+ "\t\t\tprint ('%3d ' % matrix[i][j]),\n",
+ "\t\tprint\n",
+ "\n",
+ "a = [[0 for y in xrange(10)] for x in xrange(10)]\n",
+ "b = [[0 for y in xrange(10)] for x in xrange(10)]\n",
+ "product = [[0 for y in xrange(10)] for x in xrange(10)]\n",
+ "\n",
+ "condition = True\n",
+ "while condition :\n",
+ "\tArows, Acols = map(int, raw_input('Enter order of matrix A: ').split())\n",
+ "\tBrows, Bcols = map(int, raw_input('Enter order of matrix B: ').split())\n",
+ "\n",
+ "\tif Acols == Brows :\n",
+ "\t\tcondition = False\n",
+ "\t\tbreak\n",
+ "\telse : \n",
+ "\t\tprint ('\\nMatrices are not compatible for multiplication. Enter the order again. ')\n",
+ "\t\tcontinue\n",
+ "\t\n",
+ "print ('\\nEnter matrix A: ')\n",
+ "readmatrix (a, Arows, Acols)\n",
+ "\t\n",
+ "print ('\\nEnter matrix B: ')\n",
+ "readmatrix (b, Brows, Bcols)\n",
+ "\n",
+ "multiply (product, a, b, Arows, Acols, Bcols)\n",
+ "print ('\\nThe resultant product (A.B) is: ')\n",
+ "display (product, Arows, Bcols)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter order of matrix A: 2 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter order of matrix B: 3 2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter matrix A: \n",
+ "\n",
+ "Please enter elements for row 1 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[1][1] = 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[1][2] = 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[1][3] = 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Please enter elements for row 2 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[2][1] = 4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[2][2] = 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[2][3] = 6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter matrix B: \n",
+ "\n",
+ "Please enter elements for row 1 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[1][1] = 6\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[1][2] = 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Please enter elements for row 2 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[2][1] = 4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[2][2] = 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Please enter elements for row 3 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[3][1] = 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[3][2] = 1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The resultant product (A.B) is: \n",
+ " 20 14 \n",
+ " 56 41 \n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.15, Page number: 303"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Function declaration, calculation and result\n",
+ "def getchoice () :\n",
+ "\tprint\n",
+ "\tprint ('1 - Input square matrix ')\n",
+ "\tprint ('2 - Display upper triangular matrix ')\n",
+ "\tprint ('3 - Display lower triangular matrix ')\n",
+ "\tprint ('4 - Exit ')\n",
+ "\t\n",
+ "\tcondition = True\n",
+ "\twhile condition :\n",
+ "\t\tchoice = int(raw_input('Enter choice : '))\n",
+ "\t\t\n",
+ "\t\tif choice in range (1, 5) :\n",
+ "\t\t\tcondition = False\n",
+ "\t\t\t\n",
+ "\treturn choice\n",
+ "\n",
+ "def displayupper (matrix, order) :\n",
+ "\tprint\n",
+ "\tfor i in range (0, order) :\n",
+ "\t\tfor j in range (0, order) :\n",
+ "\t\t\tif j >= i :\n",
+ "\t\t\t\tprint ('%3d ' % matrix[i][j]),\n",
+ "\t\t\telse :\n",
+ "\t\t\t\tprint ('%3d ' % 0),\n",
+ "\t\tprint\n",
+ "\t\t\n",
+ "def displaylower (matrix, order) :\n",
+ "\tprint\n",
+ "\tfor i in range (0, order) :\n",
+ "\t\tfor j in range (0, order) :\n",
+ "\t\t\tif j <= i :\n",
+ "\t\t\t\tprint ('%3d ' % matrix[i][j]),\n",
+ "\t\t\telse :\n",
+ "\t\t\t\tprint ('%3d ' % 0),\n",
+ "\t\tprint\n",
+ "\n",
+ "def read (matrix, rows, cols) :\t\n",
+ "\tfor i in range (0, rows) :\n",
+ "\t\tprint ('\\nEnter %d elements for row %d ' % (rows, (i+1)))\t\t\n",
+ "\t\tfor j in range (0, cols) :\n",
+ "\t\t\tmatrix[i][j] = int(raw_input('matrix[%d][%d] = ' % ((i+1),(j+1))))\n",
+ "\n",
+ "matrix = [[0 for y in xrange(10)] for x in xrange(10)]\n",
+ "\n",
+ "condition = True\n",
+ "while condition :\n",
+ "\tchoice = getchoice ()\n",
+ "\t\n",
+ "\tif choice == 1 :\n",
+ "\t\torder = int(raw_input('\\nEnter order of square matrix '))\t\t\n",
+ "\t\tread (matrix, order, order)\n",
+ "\t\n",
+ "\telif choice == 2 :\n",
+ "\t\tdisplayupper (matrix, order)\n",
+ "\t\n",
+ "\telif choice == 3 :\n",
+ "\t\tdisplaylower (matrix, order)\n",
+ "\t\t\n",
+ "\telif choice == 4 :\n",
+ "\t\tcondition = False\n",
+ "\t\texit ()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1 - Input square matrix \n",
+ "2 - Display upper triangular matrix \n",
+ "3 - Display lower triangular matrix \n",
+ "4 - Exit \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter choice : 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter order of square matrix 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter 3 elements for row 1 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[1][1] = 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[1][2] = 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[1][3] = 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter 3 elements for row 2 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[2][1] = 4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[2][2] = 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[2][3] = 6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter 3 elements for row 3 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[3][1] = 7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[3][2] = 8\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[3][3] = 9\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1 - Input square matrix \n",
+ "2 - Display upper triangular matrix \n",
+ "3 - Display lower triangular matrix \n",
+ "4 - Exit \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter choice : 2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ " 1 2 3 \n",
+ " 0 5 6 \n",
+ " 0 0 9 \n",
+ "\n",
+ "1 - Input square matrix \n",
+ "2 - Display upper triangular matrix \n",
+ "3 - Display lower triangular matrix \n",
+ "4 - Exit \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter choice : 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ " 1 0 0 \n",
+ " 4 5 0 \n",
+ " 7 8 9 \n",
+ "\n",
+ "1 - Input square matrix \n",
+ "2 - Display upper triangular matrix \n",
+ "3 - Display lower triangular matrix \n",
+ "4 - Exit \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter choice : 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.16, Page number: 307"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Function declaration, calculation and result\n",
+ "def readmatrix (matrix, n) :\n",
+ "\tfor i in range (0, n) :\n",
+ "\t\tprint ('\\nEnter elements for row no %d ' % (i+1))\n",
+ "\t\tfor j in range (0, n) :\n",
+ "\t\t\tmatrix[i][j] = int(raw_input('matrix[%d][%d] = ' % ((i+1),(j+1))))\n",
+ "\n",
+ "def display (matrix, n) :\n",
+ "\tprint\n",
+ "\tfor i in range (0, n) :\n",
+ "\t\tfor j in range (0, n) :\n",
+ "\t\t\tprint ('%5d ' % matrix[i][j]),\n",
+ "\t\tprint\n",
+ "\t\t\n",
+ "def isunitmatrix(matrix, n) :\n",
+ "\tfor i in range (0, n) :\n",
+ "\t\tif matrix[i][i] != 1 :\n",
+ "\t\t\treturn 0\n",
+ "\t\n",
+ "\tfor i in range (0, n) :\n",
+ "\t\tfor j in range (0, n) :\n",
+ "\t\t\tif i != j :\n",
+ "\t\t\t\tif matrix[i][j] != 0 :\n",
+ "\t\t\t\t\treturn 0\n",
+ "\treturn 1\n",
+ "\n",
+ "matrix = [[0 for y in xrange(10)] for x in xrange(10)]\n",
+ "n = int(raw_input('Enter order of matrix '))\n",
+ "readmatrix (matrix, n)\n",
+ "display (matrix, n)\n",
+ "\n",
+ "if (isunitmatrix (matrix, n)) :\n",
+ "\tprint \"UNIT MATRIX\"\n",
+ "else :\n",
+ "\tprint \"NOT A UNIT MATRIX\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter order of matrix 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter elements for row no 1 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[1][1] = 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[1][2] = 0\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[1][3] = 0\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter elements for row no 2 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[2][1] = 0\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[2][2] = 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[2][3] = 0\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter elements for row no 3 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[3][1] = 0\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[3][2] = 0\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "matrix[3][3] = 1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ " 1 0 0 \n",
+ " 0 1 0 \n",
+ " 0 0 1 \n",
+ "UNIT MATRIX\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.17, Page number: 311"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "string = raw_input('Enter a string: ')\n",
+ "\n",
+ "# Calculation and result\n",
+ "swap_string = string.swapcase()\n",
+ "\n",
+ "print swap_string"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a string: hello world\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "HELLO WORLD\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.18, Page number: 312"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "string = raw_input('Enter a string ')\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def printstr (string) :\n",
+ "\tfor i in range (0, len(string)) :\n",
+ "\t\tfor j in range (0, i+1) :\n",
+ "\t\t\tprint string[j],\n",
+ "\t\tprint\n",
+ "\t\n",
+ "\ti = i - 2\n",
+ "\twhile i >= 0 :\n",
+ "\t\tfor j in range (0, i+1) :\n",
+ "\t\t\tprint string[j],\n",
+ "\t\tprint\n",
+ "\t\ti = i - 1\n",
+ "\t\t\n",
+ "printstr (string)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a string hello\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "h\n",
+ "h e\n",
+ "h e l\n",
+ "h e l l\n",
+ "h e l l o\n",
+ "h e l\n",
+ "h e\n",
+ "h\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.19, Page number: 314"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = raw_input('Enter any string ')\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def copy (a) :\n",
+ "\tb = a\n",
+ "\treturn b\n",
+ "\t\n",
+ "b = copy (a)\n",
+ "print\n",
+ "print ('The first string is %s ' % a)\n",
+ "print ('The second string is %s ' % b)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any string hello\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The first string is hello \n",
+ "The second string is hello \n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.20, Page number: 315"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = raw_input('Enter the major string: ')\n",
+ "b = raw_input('Enter the minor string: ')\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def ispart (a, b) :\n",
+ "\tif b in a :\n",
+ "\t\tprint ('The string \"%s\" is A SUBSTRING of \"%s\" ' % (b, a))\n",
+ "\telse :\n",
+ "\t\tprint ('The string \"%s\" is NOT A SUBSTRING of \"%s\" ' % (b, a))\n",
+ "\n",
+ "ispart (a, b)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the major string: python is a programming language\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the minor string: python\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The string \"python\" is A SUBSTRING of \"python is a programming language\" \n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file
diff --git a/Magnifying_C_by_Arpita_Goyal/Chapter_9_3.ipynb b/Magnifying_C_by_Arpita_Goyal/Chapter_9_3.ipynb
new file mode 100755
index 00000000..acb61edf
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/Chapter_9_3.ipynb
@@ -0,0 +1,1093 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 9: More on Pointers"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.1, Page number: 318"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from array import *\n",
+ "\n",
+ "# Variable declaration\n",
+ "a = [[1,2,3,4,5],\n",
+ " [6,7,8,9,10],\n",
+ " [11,12,13,14,15],\n",
+ " [16,17,18,19,20],\n",
+ " [21,22,23,24,25]]\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('The value of a is %x ' % id(a))\n",
+ "\n",
+ "for i in range (0, 5) :\n",
+ "\tprint ('The value of a[%d] is %x ' % (i, id(i)))\n",
+ "\n",
+ "for i in range (0, 5) :\n",
+ "\tfor j in range (0, 5) : \n",
+ "\t\tprint ('The cell [%d][%d] i.e. %x has %d ' % (i, j, id(a[i][j]), a[i][j]))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The value of a is 7f55dc3e08c0 \n",
+ "The value of a[0] is 18861b0 \n",
+ "The value of a[1] is 1886198 \n",
+ "The value of a[2] is 1886180 \n",
+ "The value of a[3] is 1886168 \n",
+ "The value of a[4] is 1886150 \n",
+ "The cell [0][0] i.e. 1886198 has 1 \n",
+ "The cell [0][1] i.e. 1886180 has 2 \n",
+ "The cell [0][2] i.e. 1886168 has 3 \n",
+ "The cell [0][3] i.e. 1886150 has 4 \n",
+ "The cell [0][4] i.e. 1886138 has 5 \n",
+ "The cell [1][0] i.e. 1886120 has 6 \n",
+ "The cell [1][1] i.e. 1886108 has 7 \n",
+ "The cell [1][2] i.e. 18860f0 has 8 \n",
+ "The cell [1][3] i.e. 18860d8 has 9 \n",
+ "The cell [1][4] i.e. 18860c0 has 10 \n",
+ "The cell [2][0] i.e. 18860a8 has 11 \n",
+ "The cell [2][1] i.e. 1886090 has 12 \n",
+ "The cell [2][2] i.e. 1886078 has 13 \n",
+ "The cell [2][3] i.e. 1886060 has 14 \n",
+ "The cell [2][4] i.e. 1886048 has 15 \n",
+ "The cell [3][0] i.e. 1886030 has 16 \n",
+ "The cell [3][1] i.e. 1886018 has 17 \n",
+ "The cell [3][2] i.e. 1886000 has 18 \n",
+ "The cell [3][3] i.e. 1885fe8 has 19 \n",
+ "The cell [3][4] i.e. 1885fd0 has 20 \n",
+ "The cell [4][0] i.e. 1885fb8 has 21 \n",
+ "The cell [4][1] i.e. 1885fa0 has 22 \n",
+ "The cell [4][2] i.e. 1885f88 has 23 \n",
+ "The cell [4][3] i.e. 1885f70 has 24 \n",
+ "The cell [4][4] i.e. 1885f58 has 25 \n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.2, Page number: 320"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from array import *\n",
+ "\n",
+ "# Variable declaration\n",
+ "a = [[[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], [16,17,18,19,20]],\n",
+ " [[21,22,23,24,25], [26,27,28,29,30], [31,32,33,34,35], [36,37,38,39,40]]]\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('The value of a is %x ' % id(a))\n",
+ "\n",
+ "for i in range (0, 2) :\n",
+ "\tprint ('The value of a[%d] is %x ' % (i, id(a[i])))\n",
+ "\tfor j in range (0, 4) :\n",
+ "\t\tprint ('The value of a[%d][%d] is %x ' % (i, j, id(a[i][j])))\n",
+ "\tprint\n",
+ "\n",
+ "for i in range (0, 2) :\n",
+ "\tfor j in range (0, 4) : \n",
+ "\t\tfor k in range (0, 5) :\n",
+ "\t\t\tprint ('The cell [%d][%d][%d] i.e. %x has %d ' % (i, j, k, id(a[i][j][k]), a[i][j][k]))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The value of a is 7f55de4772d8 \n",
+ "The value of a[0] is 7f55de486b90 \n",
+ "The value of a[0][0] is 7f55de4db368 \n",
+ "The value of a[0][1] is 7f55de48f050 \n",
+ "The value of a[0][2] is 7f55de4db3f8 \n",
+ "The value of a[0][3] is 7f55de4c7908 \n",
+ "\n",
+ "The value of a[1] is 7f55de477320 \n",
+ "The value of a[1][0] is 7f55de4ff3f8 \n",
+ "The value of a[1][1] is 7f55de477290 \n",
+ "The value of a[1][2] is 7f55de4773b0 \n",
+ "The value of a[1][3] is 7f55de477368 \n",
+ "\n",
+ "The cell [0][0][0] i.e. 1886198 has 1 \n",
+ "The cell [0][0][1] i.e. 1886180 has 2 \n",
+ "The cell [0][0][2] i.e. 1886168 has 3 \n",
+ "The cell [0][0][3] i.e. 1886150 has 4 \n",
+ "The cell [0][0][4] i.e. 1886138 has 5 \n",
+ "The cell [0][1][0] i.e. 1886120 has 6 \n",
+ "The cell [0][1][1] i.e. 1886108 has 7 \n",
+ "The cell [0][1][2] i.e. 18860f0 has 8 \n",
+ "The cell [0][1][3] i.e. 18860d8 has 9 \n",
+ "The cell [0][1][4] i.e. 18860c0 has 10 \n",
+ "The cell [0][2][0] i.e. 18860a8 has 11 \n",
+ "The cell [0][2][1] i.e. 1886090 has 12 \n",
+ "The cell [0][2][2] i.e. 1886078 has 13 \n",
+ "The cell [0][2][3] i.e. 1886060 has 14 \n",
+ "The cell [0][2][4] i.e. 1886048 has 15 \n",
+ "The cell [0][3][0] i.e. 1886030 has 16 \n",
+ "The cell [0][3][1] i.e. 1886018 has 17 \n",
+ "The cell [0][3][2] i.e. 1886000 has 18 \n",
+ "The cell [0][3][3] i.e. 1885fe8 has 19 \n",
+ "The cell [0][3][4] i.e. 1885fd0 has 20 \n",
+ "The cell [1][0][0] i.e. 1885fb8 has 21 \n",
+ "The cell [1][0][1] i.e. 1885fa0 has 22 \n",
+ "The cell [1][0][2] i.e. 1885f88 has 23 \n",
+ "The cell [1][0][3] i.e. 1885f70 has 24 \n",
+ "The cell [1][0][4] i.e. 1885f58 has 25 \n",
+ "The cell [1][1][0] i.e. 1885f40 has 26 \n",
+ "The cell [1][1][1] i.e. 1885f28 has 27 \n",
+ "The cell [1][1][2] i.e. 1885f10 has 28 \n",
+ "The cell [1][1][3] i.e. 1885ef8 has 29 \n",
+ "The cell [1][1][4] i.e. 1885ee0 has 30 \n",
+ "The cell [1][2][0] i.e. 1885ec8 has 31 \n",
+ "The cell [1][2][1] i.e. 1885eb0 has 32 \n",
+ "The cell [1][2][2] i.e. 1885e98 has 33 \n",
+ "The cell [1][2][3] i.e. 1885e80 has 34 \n",
+ "The cell [1][2][4] i.e. 1885e68 has 35 \n",
+ "The cell [1][3][0] i.e. 1886618 has 36 \n",
+ "The cell [1][3][1] i.e. 1886600 has 37 \n",
+ "The cell [1][3][2] i.e. 18865e8 has 38 \n",
+ "The cell [1][3][3] i.e. 18865d0 has 39 \n",
+ "The cell [1][3][4] i.e. 18865b8 has 40 \n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.3, Page number: 326"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Function declaration, calculation and result\n",
+ "def readdata (a, num) :\n",
+ "\twhile num > 0 :\n",
+ "\t\ta[num-1] = int(raw_input('Enter value in cell[%d] ' % num))\n",
+ "\t\tnum = num - 1\n",
+ "\t\t\n",
+ "def printdata (a, num) :\n",
+ "\twhile num > 0 :\n",
+ "\t\tprint ('Value in cell[%d] is %d ' % (num, a[num-1]))\n",
+ "\t\tnum = num - 1\n",
+ "\n",
+ "n = int(raw_input('Enter the number of elements '))\n",
+ "a = [0 for i in xrange(n)]\n",
+ "readdata (a, n)\n",
+ "print\n",
+ "printdata (a, n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the number of elements 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[3] 30\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[2] 20\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[1] 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Value in cell[3] is 30 \n",
+ "Value in cell[2] is 20 \n",
+ "Value in cell[1] is 10 \n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.4, Page number: 328"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Function declaration, calculation and result\n",
+ "def readdata (a, n) :\n",
+ "\twhile n > 0 :\n",
+ "\t\ta[n-1] = int(raw_input('Enter value in cell[%d] ' % n))\n",
+ "\t\tn = n - 1\n",
+ "\t\t\n",
+ "def printdata (a, n) :\n",
+ "\twhile n > 0 :\n",
+ "\t\tprint ('Value in cell[%d] is %d ' % (n, a[n-1]))\n",
+ "\t\tn = n - 1\n",
+ "\n",
+ "n = int(raw_input('Enter the number of elements '))\n",
+ "a = [0 for i in xrange(n)]\n",
+ "readdata (a, n)\n",
+ "print\n",
+ "printdata (a, n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the number of elements 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[5] 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[4] 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[3] 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[2] 4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[1] 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Value in cell[5] is 1 \n",
+ "Value in cell[4] is 2 \n",
+ "Value in cell[3] is 3 \n",
+ "Value in cell[2] is 4 \n",
+ "Value in cell[1] is 5 \n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.5, Page number: 330"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Function declaration, calculation and result\n",
+ "global n\n",
+ "n = 0\n",
+ "\n",
+ "def makearray (n) :\n",
+ "\tn = int(raw_input('Enter the number of elements '))\t\n",
+ "\treturn n\n",
+ "\t\n",
+ "def readdata (a, n) :\n",
+ "\twhile n > 0 :\n",
+ "\t\ta[n-1] = int(raw_input('Enter value in cell[%d] ' % n))\n",
+ "\t\tn = n - 1\n",
+ "\t\t\n",
+ "def printdata (a, n) :\n",
+ "\twhile n > 0 :\n",
+ "\t\tprint ('Value in cell[%d] is %d ' % (n, a[n-1]))\n",
+ "\t\tn = n - 1\n",
+ "\t\t\n",
+ "n = makearray (n)\n",
+ "a = [0 for i in xrange(n)]\n",
+ "readdata (a, n)\n",
+ "print\n",
+ "printdata (a, n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the number of elements 4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[4] 400\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[3] 300\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[2] 200\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[1] 100\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Value in cell[4] is 400 \n",
+ "Value in cell[3] is 300 \n",
+ "Value in cell[2] is 200 \n",
+ "Value in cell[1] is 100 \n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.6, Page number: 335"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration, calculation and result\n",
+ "string = raw_input('Enter the string: ')\n",
+ "\n",
+ "rev_string = reversed(string)\n",
+ "\n",
+ "if list(string) == list(rev_string) :\n",
+ "\tprint ('%s is a PALINDROME ' % string)\n",
+ "else :\n",
+ "\tprint ('%s is not a PALINDROME ' % string)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the string: malayalam\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "malayalam is a PALINDROME \n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.7, Page number: 337"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Function declaration, calculation and result\n",
+ "def readdata (a, n) :\n",
+ "\tfor i in range (0, n) :\n",
+ "\t\ta[i] = int(raw_input('Enter data in cell [%d] ' % i))\n",
+ "\treturn a\n",
+ "\n",
+ "def bubble (a, n) :\n",
+ "\tfor p in range (0, n) :\n",
+ "\t\tfor i in range (0, n-1-p) :\n",
+ "\t\t\tif a[i] > a[i+1] :\n",
+ "\t\t\t\ttemp = a[i]\n",
+ "\t\t\t\ta[i] = a[i+1]\n",
+ "\t\t\t\ta[i+1] = temp\n",
+ "\treturn a\n",
+ "\n",
+ "def printdata (a, n) :\n",
+ "\tfor i in range (0, n) :\n",
+ "\t\tprint ('The data in cell [%d] is %d ' % (i, a[i]))\n",
+ "\n",
+ "n = int(raw_input('Enter the number of elements to be entered: '))\n",
+ "a = [0] * n\n",
+ "a = readdata (a, n)\n",
+ "a = bubble (a, n)\n",
+ "print ('\\nThe sorted data is ')\n",
+ "printdata (a, n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the number of elements to be entered: 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [0] 46\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [1] 75\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [2] 68\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [3] 34\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [4] 49\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The sorted data is \n",
+ "The data in cell [0] is 34 \n",
+ "The data in cell [1] is 46 \n",
+ "The data in cell [2] is 49 \n",
+ "The data in cell [3] is 68 \n",
+ "The data in cell [4] is 75 \n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.8, Page number: 341"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration, calculation and result\n",
+ "i = 6\n",
+ "c = 'a'\n",
+ "\n",
+ "the_data = i\n",
+ "print ('the_data points to the integer value %d ' % the_data)\n",
+ "\n",
+ "the_data = c\n",
+ "print ('the_data points to the character value %c ' % the_data)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "the_data points to the integer value 6 \n",
+ "the_data points to the character value a \n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.9, Page number: 343"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "marks = [[0 for x in xrange(5)] for x in xrange(5)]\n",
+ "avg = 0\n",
+ "percent = [0 for x in xrange(5)]\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def readncompute (marks, percent, avg) :\n",
+ "\tno_of_students = int(raw_input('How many students marks do you want to enter? '))\n",
+ "\tfor i in range (0, no_of_students) :\n",
+ "\t\tprint ('\\nEnter marks of 5 subjects for roll no. %d ' % (i+1))\n",
+ "\t\tfor j in range (0, 5) :\n",
+ "\t\t\tmarks[i][j] = int(raw_input('Subject %d: ' % (j+1)))\n",
+ "\t\t\tpercent[i] = percent[i] + marks[i][j]\n",
+ "\t\t\n",
+ "\t\tpercent[i] = percent[i]/5\n",
+ "\t\tavg = avg + percent[i]\n",
+ "\treturn avg/no_of_students, no_of_students\n",
+ "\n",
+ "def printdata (score, per, avg, no_of_students) :\n",
+ "\tfor i in range (0, no_of_students) :\n",
+ "\t\tprint ('\\nMarks for roll no. %d are ' % (i+1))\n",
+ "\t\tfor j in range (0, 5) :\n",
+ "\t\t\tprint ('%d ' % score[i][j])\n",
+ "\t\tprint ('Percentage for roll no. is %.2f ' % per[i])\n",
+ "\t\tif per[i] < avg :\n",
+ "\t\t\tprint ('Below average')\n",
+ "\t\telse :\n",
+ "\t\t\tprint ('Above average')\n",
+ "\n",
+ "average, nos = readncompute(marks, percent, avg)\n",
+ "print ('\\nThe average marks are %.2f' % average)\n",
+ "printdata (marks, percent, average, nos)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many students marks do you want to enter? 2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter marks of 5 subjects for roll no. 1 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 1: 65\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 2: 76\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 3: 54\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 4: 83\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 5: 94\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter marks of 5 subjects for roll no. 2 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 1: 84\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 2: 62\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 3: 94\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 4: 73\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 5: 62\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The average marks are 74.00\n",
+ "\n",
+ "Marks for roll no. 1 are \n",
+ "65 \n",
+ "76 \n",
+ "54 \n",
+ "83 \n",
+ "94 \n",
+ "Percentage for roll no. is 74.00 \n",
+ "Above average\n",
+ "\n",
+ "Marks for roll no. 2 are \n",
+ "84 \n",
+ "62 \n",
+ "94 \n",
+ "73 \n",
+ "62 \n",
+ "Percentage for roll no. is 75.00 \n",
+ "Above average\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.10, Page number: 349"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "marks = [[0 for x in xrange(5)] for x in xrange(5)]\n",
+ "avg = 0\n",
+ "percent = [0 for x in xrange(5)]\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def readncompute (marks, percent, avg) :\n",
+ "\tno_of_students = int(raw_input('How many students marks do you want to enter? '))\n",
+ "\tfor i in range (0, no_of_students) :\n",
+ "\t\tprint ('\\nEnter marks of 5 subjects for roll no. %d ' % (i+1))\n",
+ "\t\tfor j in range (0, 5) :\n",
+ "\t\t\tmarks[i][j] = int(raw_input('Subject %d: ' % (j+1)))\n",
+ "\t\t\tpercent[i] = percent[i] + marks[i][j]\n",
+ "\t\t\n",
+ "\t\tpercent[i] = percent[i]/5\n",
+ "\t\tavg = avg + percent[i]\n",
+ "\treturn avg/no_of_students, no_of_students\n",
+ "\n",
+ "def printdata (score, per, avg, no_of_students) :\n",
+ "\tfor i in range (0, no_of_students) :\n",
+ "\t\tprint ('\\nMarks for roll no. %d are ' % (i+1))\n",
+ "\t\tfor j in range (0, 5) :\n",
+ "\t\t\tprint ('%d ' % score[i][j])\n",
+ "\t\tprint ('Percentage for roll no. is %.2f ' % per[i])\n",
+ "\t\tif per[i] < avg :\n",
+ "\t\t\tprint ('Below average')\n",
+ "\t\telse :\n",
+ "\t\t\tprint ('Above average')\n",
+ "\n",
+ "average, nos = readncompute(marks, percent, avg)\n",
+ "print ('\\nThe average marks are %.2f' % average)\n",
+ "printdata (marks, percent, average, nos)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many students marks do you want to enter? 2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter marks of 5 subjects for roll no. 1 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 1: 65\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 2: 76\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 3: 54\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 4: 32\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 5: 89\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter marks of 5 subjects for roll no. 2 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 1: 76\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 2: 86\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 3: 43\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 4: 32\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 5: 21\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The average marks are 57.00\n",
+ "\n",
+ "Marks for roll no. 1 are \n",
+ "65 \n",
+ "76 \n",
+ "54 \n",
+ "32 \n",
+ "89 \n",
+ "Percentage for roll no. is 63.00 \n",
+ "Above average\n",
+ "\n",
+ "Marks for roll no. 2 are \n",
+ "76 \n",
+ "86 \n",
+ "43 \n",
+ "32 \n",
+ "21 \n",
+ "Percentage for roll no. is 51.00 \n",
+ "Below average\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.11, Page number: 351"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Function declaration, calculation and result\n",
+ "def readdata (a, n) :\n",
+ "\tfor i in range (0, n) :\n",
+ "\t\ta[i] = int(raw_input('Enter data in cell [%d] ' % i))\n",
+ "\treturn a\n",
+ "\n",
+ "def bubble (name) :\n",
+ "\tfor p in range (0, 4) :\n",
+ "\t\tfor i in range (0, 3-p) :\n",
+ "\t\t\tif name[i] > name[i+1] :\n",
+ "\t\t\t\ttemp = name[i]\n",
+ "\t\t\t\tname[i] = name[i+1]\n",
+ "\t\t\t\tname[i+1] = temp\n",
+ "\treturn name\n",
+ "\n",
+ "def printdata (name) :\n",
+ "\tfor i in range (0, 4) :\n",
+ "\t\tprint ('The data in cell [%d] is %s ' % ((i+1), name[i]))\n",
+ "\n",
+ "name = [0] * 4\n",
+ "for i in range (0, 4) :\n",
+ "\tname[i] = raw_input('Enter string[%d] ' % (i+1))\n",
+ "\n",
+ "name = bubble (name)\n",
+ "print ('\\nThe sorted data is ')\n",
+ "printdata (name)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter string[1] zaheer\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter string[2] ashita\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter string[3] tamanna\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter string[4] purti\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The sorted data is \n",
+ "The data in cell [1] is ashita \n",
+ "The data in cell [2] is purti \n",
+ "The data in cell [3] is tamanna \n",
+ "The data in cell [4] is zaheer \n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file
diff --git a/Magnifying_C_by_Arpita_Goyal/README.txt b/Magnifying_C_by_Arpita_Goyal/README.txt
new file mode 100755
index 00000000..2ec7deb7
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/README.txt
@@ -0,0 +1,10 @@
+Contributed By: Mahesh M
+Course: btech
+College/Institute/Organization: MSIT
+Department/Designation: CSE
+Book Title: Magnifying C
+Author: Arpita Gopal
+Publisher: Prentice Hall India Learning Private Limited, New Delhi
+Year of publication: 2009
+Isbn: 9788120338616
+Edition: 1 \ No newline at end of file
diff --git a/Magnifying_C_by_Arpita_Goyal/screenshots/chapter_10.png b/Magnifying_C_by_Arpita_Goyal/screenshots/chapter_10.png
new file mode 100755
index 00000000..26daa2e1
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/screenshots/chapter_10.png
Binary files differ
diff --git a/Magnifying_C_by_Arpita_Goyal/screenshots/chapter_2.png b/Magnifying_C_by_Arpita_Goyal/screenshots/chapter_2.png
new file mode 100755
index 00000000..c7b049ba
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/screenshots/chapter_2.png
Binary files differ
diff --git a/Magnifying_C_by_Arpita_Goyal/screenshots/chapter_6.png b/Magnifying_C_by_Arpita_Goyal/screenshots/chapter_6.png
new file mode 100755
index 00000000..585a8ed4
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/screenshots/chapter_6.png
Binary files differ
diff --git a/Magnifying_C_by_Arpita_Goyal/screenshots/ss_1.png b/Magnifying_C_by_Arpita_Goyal/screenshots/ss_1.png
new file mode 100755
index 00000000..c9ed06ce
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/screenshots/ss_1.png
Binary files differ
diff --git a/Magnifying_C_by_Arpita_Goyal/screenshots/ss_2.png b/Magnifying_C_by_Arpita_Goyal/screenshots/ss_2.png
new file mode 100755
index 00000000..5fc014e9
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/screenshots/ss_2.png
Binary files differ
diff --git a/Magnifying_C_by_Arpita_Goyal/screenshots/ss_3.png b/Magnifying_C_by_Arpita_Goyal/screenshots/ss_3.png
new file mode 100755
index 00000000..bd465ad3
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/screenshots/ss_3.png
Binary files differ