diff options
Diffstat (limited to 'Programming_in_C_using_ANSI_C/KamthaneChapter13.ipynb')
-rwxr-xr-x | Programming_in_C_using_ANSI_C/KamthaneChapter13.ipynb | 2367 |
1 files changed, 2367 insertions, 0 deletions
diff --git a/Programming_in_C_using_ANSI_C/KamthaneChapter13.ipynb b/Programming_in_C_using_ANSI_C/KamthaneChapter13.ipynb new file mode 100755 index 00000000..f9222003 --- /dev/null +++ b/Programming_in_C_using_ANSI_C/KamthaneChapter13.ipynb @@ -0,0 +1,2367 @@ +{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 13: Structure and Union<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.1, Page number: 409<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display size of structure elements\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class is used instead of structure in python\n",
+ "class book1:\n",
+ " book = ''\n",
+ " pages = 0\n",
+ " price = 0.00\n",
+ "\n",
+ "#Python variables uses value tagged methods for storing the values\n",
+ "\n",
+ "#Class variable declaration\n",
+ "bk1 = book1()\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nSize of Structure Elements\")\n",
+ "sys.stdout.write(\"\\nBook : %d\"%(sys.getsizeof(bk1.book)))\n",
+ "sys.stdout.write(\"\\nPages : %d\"%(sys.getsizeof(bk1.pages)))\n",
+ "sys.stdout.write(\"\\nPrice : %d\"%(sys.getsizeof(bk1.price)))\n",
+ "sys.stdout.write(\"\\nTotal Bytes : %d\"%(sys.getsizeof(bk1)))\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Size of Structure Elements\n",
+ "Book : 21\n",
+ "Pages : 12\n",
+ "Price : 16\n",
+ "Total Bytes : 36"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.2, Page number: 410<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Define a structure and initialize its member variables\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class book1:\n",
+ " book = ''\n",
+ " pages = 0\n",
+ " price = 0.0\n",
+ " \n",
+ "#Class variable declaration and Initialization\n",
+ "bk1 = book1()\n",
+ "bk1.book = \"C++\"\n",
+ "bk1.pages = 300\n",
+ "bk1.price = 285\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nBook Name : %s\"%(bk1.book))\n",
+ "sys.stdout.write(\"\\nNo. of Pages : %d\"%(bk1.pages))\n",
+ "sys.stdout.write(\"\\nBook Price : %d\"%(bk1.price))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Book Name : C++\n",
+ "No. of Pages : 300\n",
+ "Book Price : 285"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.3, Page number: 411<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Copy structure elements from one object to another object\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class disk:\n",
+ " co = ''\n",
+ " type1 = 0.0\n",
+ " price = 0\n",
+ " \n",
+ "#Class variable declaration\n",
+ "d1 = disk()\n",
+ "d2 = disk()\n",
+ "d3 = disk()\n",
+ "\n",
+ "#Class variable initialization\n",
+ "d1.co = \"SONY\"\n",
+ "d1.type1 = 1.44\n",
+ "d1.price = 20\n",
+ "\n",
+ "#Copying\n",
+ "d2.co = d1.co\n",
+ "d2.type1 = d1.type1\n",
+ "d2.price = d1.price\n",
+ "\n",
+ "d3 = d2\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\n%s %g %d\"%(d1.co,d1.type1,d1.price))\n",
+ "sys.stdout.write(\"\\n%s %g %d\"%(d2.co,d2.type1,d2.price))\n",
+ "sys.stdout.write(\"\\n%s %g %d\"%(d3.co,d3.type1,d3.price))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "SONY 1.44 20\n",
+ "SONY 1.44 20\n",
+ "SONY 1.44 20"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.4, Page number: 412<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Read values and assign them to structure variables\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class book1:\n",
+ " book = ''\n",
+ " pages = 0\n",
+ " price = 0.0\n",
+ " \n",
+ "#Class variable declaration\n",
+ "bk1 = book1()\n",
+ "\n",
+ "#Variable initialization\n",
+ "bk1.book = raw_input(\"Enter Book name,pages,price : \")\n",
+ "bk1.pages = int(raw_input(\"Enter Book name,pages,price : \"))\n",
+ "bk1.price = float(raw_input(\"Enter Book name, pages,price : \"))\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nBook Name : %s\"%(bk1.book))\n",
+ "sys.stdout.write(\"\\nNo. of Pages : %d\"%(bk1.pages))\n",
+ "sys.stdout.write(\"\\nBook Price : %d\"%(bk1.price))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Book name,pages,price : C\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Book name,pages,price : 500\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Book name, pages,price : 450\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Book Name : C\n",
+ "No. of Pages : 500\n",
+ "Book Price : 450"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.5, Page number: 414<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Read and display car details\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class time:\n",
+ " second = 0\n",
+ " minute = 0\n",
+ " hour = 0\n",
+ " \n",
+ "class t:\n",
+ " carno = 0\n",
+ " st = time()\n",
+ " rt = time()\n",
+ " \n",
+ "#Class variable declaration\n",
+ "r1 = t()\n",
+ "\n",
+ "#Variable Initialization\n",
+ "r1.carno = int(raw_input(\"Car No., Starting Time, Reaching Time :\"))\n",
+ "r1.st.hour = int(raw_input(\"Car No., Starting Time, Reaching Time :\"))\n",
+ "r1.st.minute = int(raw_input(\"Car No., Starting Time, Reaching Time :\"))\n",
+ "r1.st.second = int(raw_input(\"Car No., Starting Time, Reaching Time :\"))\n",
+ "r1.rt.hour = int(raw_input(\"Car No., Starting Time, Reaching Time :\"))\n",
+ "r1.rt.minute = int(raw_input(\"Car No., Starting Time, Reaching Time :\"))\n",
+ "r1.rt.second = int(raw_input(\"Car No., Starting Time, Reaching Time :\"))\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nCar No. \\tStarting Time \\tReaching Time\\n\")\n",
+ "sys.stdout.write(\"%d\\t\"%(r1.carno))\n",
+ "sys.stdout.write(\"\\t%d:%d:%d\\t\\t\"%(r1.st.hour,r1.st.minute,r1.st.second))\n",
+ "sys.stdout.write(\"%d:%d:%d\"%(r1.rt.hour,r1.rt.minute,r1.rt.second))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No., Starting Time, Reaching Time :125\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No., Starting Time, Reaching Time :2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No., Starting Time, Reaching Time :50\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No., Starting Time, Reaching Time :30\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No., Starting Time, Reaching Time :3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No., Starting Time, Reaching Time :50\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No., Starting Time, Reaching Time :25\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Car No. \tStarting Time \tReaching Time\n",
+ "125\t\t2:50:30\t\t3:50:25"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.6, Page number: 415<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Read and display the details of a person\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class name:\n",
+ " first = ''\n",
+ " second = ''\n",
+ " last = ''\n",
+ " \n",
+ "class b_date:\n",
+ " day = 0\n",
+ " month = 0\n",
+ " year = 0\n",
+ " \n",
+ "class data:\n",
+ " nm = name()\n",
+ " bt = b_date()\n",
+ " \n",
+ "#Class variable declaration\n",
+ "r1 = data()\n",
+ "\n",
+ "#Variable Initialization\n",
+ "r1.nm.first = raw_input(\"Enter Name (First/Second/Last)\")\n",
+ "r1.nm.second = raw_input(\"Enter Name (First/Second/Last)\")\n",
+ "r1.nm.last = raw_input(\"Enter Name (First/Second/Last)\")\n",
+ "r1.bt.day = int(raw_input(\"Enter Birth Date Day/Month/Year\"))\n",
+ "r1.bt.month = int(raw_input(\"Enter Birth Date Day/Month/Year\"))\n",
+ "r1.bt.year = int(raw_input(\"Enter Birth Date Day/Month/Year\"))\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"Name : %s %s %s\\n\"%(r1.nm.first,r1.nm.second,r1.nm.last))\n",
+ "sys.stdout.write(\"Birth Date : %d.%d.%d\"%(r1.bt.day,r1.bt.month,r1.bt.year))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Name (First/Second/Last)Ram\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Name (First/Second/Last)Sham\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Name (First/Second/Last)Pande\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Birth Date Day/Month/Year12\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Birth Date Day/Month/Year12\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Birth Date Day/Month/Year1980\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Name : Ram Sham Pande\n",
+ "Birth Date : 12.12.1980"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.7, Page number: 416<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Create array of structure objects\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class time:\n",
+ " second = 0\n",
+ " minute = 0\n",
+ " hour = 0\n",
+ " \n",
+ "class t:\n",
+ " carno = 0\n",
+ " st = time()\n",
+ " rt = time()\n",
+ " \n",
+ "#Class variable declaration\n",
+ "r1 = [t() for i in range(0,3)]\n",
+ "\n",
+ "#Variable Initialization\n",
+ "for k in range(0,3):\n",
+ " r1[k].carno = int(raw_input(\"Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)\"))\n",
+ " r1[k].st.hour = int(raw_input(\"Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)\"))\n",
+ " r1[k].st.minute = int(raw_input(\"Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)\"))\n",
+ " r1[k].st.second = int(raw_input(\"Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)\"))\n",
+ " r1[k].rt.hour = int(raw_input(\"Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)\"))\n",
+ " r1[k].rt.minute = int(raw_input(\"Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)\"))\n",
+ " r1[k].rt.second = int(raw_input(\"Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)\"))\n",
+ " \n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nCar No.\\t\\tStarting Time\\tReaching Time\")\n",
+ "for k in range(0,3):\n",
+ " sys.stdout.write(\"\\n%d\\t\"%(r1[k].carno))\n",
+ " sys.stdout.write(\"\\t%d:%d:%d\\t\"%(r1[k].st.hour,r1[k].st.minute,r1[k].st.second))\n",
+ " sys.stdout.write(\"\\t%d:%d:%d\\t\"%(r1[k].rt.hour,r1[k].rt.minute,r1[k].rt.second))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)120\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)20\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)25\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)25\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)58\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)121\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)25\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)40\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)40\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)25\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)122\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)30\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)52\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)40\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Car No.\t\tStarting Time\tReaching Time\n",
+ "120\t\t4:30:52\t\t5:40:10\t\n",
+ "121\t\t4:30:52\t\t5:40:10\t\n",
+ "122\t\t4:30:52\t\t5:40:10\t"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.8, Page number: 417<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Read and display student details\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class stud:\n",
+ " name = ''\n",
+ " rollno = 0\n",
+ " grade = ''\n",
+ " \n",
+ "#Class variable declaration\n",
+ "st = [stud() for i in range(0,3)]\n",
+ "\n",
+ "#Variable Initialization\n",
+ "k = 0 \n",
+ "while k < 3:\n",
+ " st[k].name = raw_input(\"Name :\")\n",
+ " st[k].rollno = int(raw_input(\"Roll No. :\"))\n",
+ " st[k].grade = raw_input(\"Grade : \")\n",
+ " k += 1\n",
+ " \n",
+ "#Result\n",
+ "k = 0\n",
+ "sys.stdout.write(\"\\nName \\tRollno \\tGrade\\n\")\n",
+ "while k < 3:\n",
+ " sys.stdout.write(\"\\n%s\\t%d\\t%s\"%(st[k].name,st[k].rollno,st[k].grade))\n",
+ " k += 1\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Name :Suresh\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Roll No. :125\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Grade : A\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Name :Mahesh\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Roll No. :126\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Grade : B\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Name :Rajesh\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Roll No. :127\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Grade : A\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Name \tRollno \tGrade\n",
+ "\n",
+ "Suresh\t125\tA\n",
+ "Mahesh\t126\tB\n",
+ "Rajesh\t127\tA"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.9, Page number: 419<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Pointer to structure \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class book:\n",
+ " name = ''\n",
+ " author = ''\n",
+ " pages = 0\n",
+ " \n",
+ "#Class variable declaration\n",
+ "b1 = book()\n",
+ "\n",
+ "#Variable Initialization\n",
+ "b1.name = \"JAVA COMPLETE REFERENCE\"\n",
+ "b1.author = \"P.NAUGHTON\"\n",
+ "b1.pages = 886\n",
+ "\n",
+ "#There is no pointer concept in python\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\n%s by %s of %d pages\"%(b1.name,b1.author,b1.pages))\n",
+ "sys.stdout.write(\"\\n%s by %s of %d pages\"%(b1.name,b1.author,b1.pages))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "JAVA COMPLETE REFERENCE by P.NAUGHTON of 886 pages\n",
+ "JAVA COMPLETE REFERENCE by P.NAUGHTON of 886 pages"
+ ]
+ }
+ ],
+ "prompt_number": 27
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.10, Page number: 420<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Pointer as members of structure \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class boy:\n",
+ " name = ''\n",
+ " age = 0\n",
+ " height = 0.0\n",
+ " \n",
+ "#Class variable declaration\n",
+ "sp = boy()\n",
+ "\n",
+ "#Variable Initialization\n",
+ "sp.name = \"Mahesh\"\n",
+ "sp.age = 20\n",
+ "sp.height = 5.40\n",
+ "\n",
+ "#There is no pointer concept in python\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nName = %s\"%(sp.name))\n",
+ "sys.stdout.write(\"\\nAge = %s\"%(sp.age))\n",
+ "sys.stdout.write(\"\\nHeight = %s\"%(sp.height))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Name = Mahesh\n",
+ "Age = 20\n",
+ "Height = 5.4"
+ ]
+ }
+ ],
+ "prompt_number": 28
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.11, Page number: 421<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Pointer as members of structure \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class boy:\n",
+ " name = ''\n",
+ " age = 0\n",
+ " height = 0.0\n",
+ " \n",
+ "#Class variable declaration\n",
+ "b = boy()\n",
+ "\n",
+ "#Variable Initialization\n",
+ "nm = \"Somesh\"\n",
+ "ag = 20\n",
+ "ht = 5.40\n",
+ "\n",
+ "#There is no pointer concept in python\n",
+ "b.name = nm\n",
+ "b.age = ag\n",
+ "b.height = ht\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nName : %s\"%(b.name))\n",
+ "sys.stdout.write(\"\\nAge = %d\"%(b.age))\n",
+ "sys.stdout.write(\"\\nHeight = %.2g\"%(b.height))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Name : Somesh\n",
+ "Age = 20\n",
+ "Height = 5.4"
+ ]
+ }
+ ],
+ "prompt_number": 30
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.12, Page number: 422<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display the contents of the structure using ordinary pointer\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class num:\n",
+ " a = 0\n",
+ " b = 0\n",
+ " c = 0\n",
+ "\n",
+ "#Class variable declaration\n",
+ "d = num()\n",
+ "\n",
+ "#Variable Initialization\n",
+ "d.a = 2\n",
+ "d.b = 3\n",
+ "d.c = 4\n",
+ "\n",
+ "#There is no pointer concept in python\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\na = %d\"%(d.a))\n",
+ "sys.stdout.write(\"\\nb = %d\"%(d.b))\n",
+ "sys.stdout.write(\"\\nc = %d\"%(d.c))\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "a = 2\n",
+ "b = 3\n",
+ "c = 4"
+ ]
+ }
+ ],
+ "prompt_number": 32
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.13, Page number: 423<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Passing address of structure variable\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class book:\n",
+ " name = ''\n",
+ " author = ''\n",
+ " pages = 0\n",
+ " \n",
+ "#Class variable declaration and initialization\n",
+ "b1 = book()\n",
+ "b1.name = \"JAVA COMPLETE REFERENCE\"\n",
+ "b1.author = \"P.NAUGHTON\"\n",
+ "b1.pages = 886\n",
+ "\n",
+ "#Function definition\n",
+ "def show(b1):\n",
+ " sys.stdout.write(\"\\n%s by %s of %d pages\"%(b1.name,b1.author,b1.pages))\n",
+ "\n",
+ "#Function call\n",
+ "show(b1)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "JAVA COMPLETE REFERENCE by P.NAUGHTON of 886 pages"
+ ]
+ }
+ ],
+ "prompt_number": 34
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.14, Page number: 424<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Passing structure elements to function\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class boy:\n",
+ " name = ''\n",
+ " age = 0\n",
+ " wt = 0\n",
+ " \n",
+ "#Class variable declaration and initialization\n",
+ "b1 = boy()\n",
+ "b1.name = \"Amit\"\n",
+ "b1.age = 20\n",
+ "b1.wt = 25\n",
+ "\n",
+ "#Function definition\n",
+ "def print1(s,t,n):\n",
+ " sys.stdout.write(\"\\n%s %d %d\"%(s,t,n))\n",
+ " \n",
+ "#Function call\n",
+ "print1(b1.name,b1.age,b1.wt)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Amit 20 25"
+ ]
+ }
+ ],
+ "prompt_number": 36
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.15, Page number: 425<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Pass entire structure to user defined function\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class boy:\n",
+ " name = ''\n",
+ " age = 0\n",
+ " wt = 0\n",
+ " \n",
+ "#Class variable declaration and initialization\n",
+ "b1 = boy()\n",
+ "b1.name = \"Amit\"\n",
+ "b1.age = 20\n",
+ "b1.wt = 25\n",
+ "\n",
+ "#Function definition\n",
+ "def print1(b):\n",
+ " sys.stdout.write(\"\\n%s %d %d\"%(b.name,b.age,b1.wt))\n",
+ " \n",
+ "#Function call\n",
+ "print1(b1)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Amit 20 25"
+ ]
+ }
+ ],
+ "prompt_number": 37
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.16, Page number: 426<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#User defined data type \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no preprocessor directive in python\n",
+ "H = 60\n",
+ "\n",
+ "#There is no typedef function in python and no separate variable declaration is needed\n",
+ "hrs = int(raw_input(\"Enter Hours :\"))\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nMinutes = %d\"%(hrs*H))\n",
+ "sys.stdout.write(\"\\nSeconds = %d\"%(hrs*H*H))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Hours :2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Minutes = 120\n",
+ "Seconds = 7200"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.17, Page number: 426<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#String data type\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no typedef function in python\n",
+ "a = \" Hello \"\n",
+ "b = raw_input(\"Enter Your Name : \")\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"%s %s\"%(a,b))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Your Name : KAMAL\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Hello KAMAL"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.18, Page number: 427<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Create user defined data type from structure\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "#There is no typedef function in python\n",
+ "class struct:\n",
+ " name = ''\n",
+ " sex = ''\n",
+ " acno = 0\n",
+ " \n",
+ "#class variable declaration and initialization\n",
+ "employee = struct()\n",
+ "employee.name = \"Sanjay\"\n",
+ "employee.sex = \"M\"\n",
+ "employee.acno = 125\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nName\\tSex\\tA/c No.\\n\")\n",
+ "sys.stdout.write(\"%s\\t\"%(employee.name))\n",
+ "sys.stdout.write(\"%s\\t\"%(employee.sex))\n",
+ "sys.stdout.write(\"%s\\n\"%(employee.acno))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Name\tSex\tA/c No.\n",
+ "Sanjay\tM\t125\n"
+ ]
+ }
+ ],
+ "prompt_number": 44
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.19, Page number: 427<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Create user defined data type from structure\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class struct:\n",
+ " name = ''\n",
+ " sex = ''\n",
+ " acno = 0\n",
+ " \n",
+ "#class Variable declaration and initialization\n",
+ "employee = [struct() for i in range(0,2)]\n",
+ "\n",
+ "for k in range(0,2):\n",
+ " employee[k].name = raw_input(\"Name of the Employee :\")\n",
+ " employee[k].sex = raw_input(\"Sex\")\n",
+ " employee[k].acno = raw_input(\"A/c No.\")\n",
+ " \n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nName\\tSex\\tA/c No.\\n\")\n",
+ "for k in range(0,2):\n",
+ " sys.stdout.write(\"%s\\t\"%(employee[k].name))\n",
+ " sys.stdout.write(\"%s\\t\"%(employee[k].sex))\n",
+ " sys.stdout.write(\"%s\\n\"%(employee[k].acno))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Name of the Employee :AJAY\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "SexM\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "A/c No.122\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Name of the Employee :ANITA\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "SexF\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "A/c No.124\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Name\tSex\tA/c No.\n",
+ "AJAY\tM\t122\n",
+ "ANITA\tF\t124\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.20, Page number: 429<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Structure containing the details of the employees\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no typedef function in python\n",
+ "\n",
+ "#Class definition\n",
+ "class struct:\n",
+ " first = ''\n",
+ " middle = ''\n",
+ " last = ''\n",
+ " city = ''\n",
+ " pincode = 0\n",
+ " \n",
+ "#Class variable declaration and initialization\n",
+ "person = [struct() for i in range(0,2)]\n",
+ "\n",
+ "for j in range(0,2):\n",
+ " person[j].first = raw_input(\"First Name :\")\n",
+ " person[j].middle = raw_input(\"Middle Name : \")\n",
+ " person[j].last = raw_input(\"Last Name : \")\n",
+ " person[j].city = raw_input(\"City & Pincode\")\n",
+ " person[j].pincode = int(raw_input(\"City & Pincode\"))\n",
+ " \n",
+ "#Result\n",
+ "for j in range(0,2):\n",
+ " sys.stdout.write(\"\\nRecord No : %d\"%(j+1))\n",
+ " sys.stdout.write(\"\\nFirst Name : %s\"%(person[j].first))\n",
+ " sys.stdout.write(\"\\nMiddle Name : %s\"%(person[j].middle))\n",
+ " sys.stdout.write(\"\\nLast Name : %s\"%(person[j].last))\n",
+ " sys.stdout.write(\"\\nCity & Pincode : %s - %d\\n\"%(person[j].city,person[j].pincode))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "First Name :Jay\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Middle Name : Mohan\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Last Name : Deshmukh\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "City & PincodeNanded\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "City & Pincode431602\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "First Name :Vijay\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Middle Name : Kamal\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Last Name : Nandedkar\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "City & PincodeNanded\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "City & Pincode431602\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Record No : 1\n",
+ "First Name : Jay\n",
+ "Middle Name : Mohan\n",
+ "Last Name : Deshmukh\n",
+ "City & Pincode : Nanded - 431602\n",
+ "\n",
+ "Record No : 2\n",
+ "First Name : Vijay\n",
+ "Middle Name : Kamal\n",
+ "Last Name : Nandedkar\n",
+ "City & Pincode : Nanded - 431602\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.21, Page number: 431<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Information of vehicles\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "PETROL = 1\n",
+ "DISEL = 2\n",
+ "TWO_WH = 3\n",
+ "FOUR_WH = 4\n",
+ "OLD = 5\n",
+ "NEW = 6\n",
+ "\n",
+ "#Class declaration\n",
+ "class vehicle:\n",
+ " type1 = 3\n",
+ " fuel = 2\n",
+ " model = 3\n",
+ " \n",
+ "#Class variable declaration and initialization\n",
+ "v = vehicle()\n",
+ "v.type1 = FOUR_WH\n",
+ "v.fuel = DISEL\n",
+ "v.model = OLD\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nType of Vehicle : %d\"%(v.type1))\n",
+ "sys.stdout.write(\"\\nFuel : %d\"%(v.fuel))\n",
+ "sys.stdout.write(\"\\nModel : %d\"%(v.model))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Type of Vehicle : 4\n",
+ "Fuel : 2\n",
+ "Model : 5"
+ ]
+ }
+ ],
+ "prompt_number": 52
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.22, Page number: 432<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display the examination result of the student \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "#There is no preprocessor directives in python\n",
+ "PASS = 1\n",
+ "FAIL = 0\n",
+ "A = 0\n",
+ "B = 1\n",
+ "C = 2\n",
+ "\n",
+ "#Class declaration\n",
+ "class student:\n",
+ " name = ''\n",
+ " result = 1\n",
+ " grade = 2\n",
+ " \n",
+ "#Class variable declaration and initialization\n",
+ "v = student()\n",
+ "\n",
+ "v.name = \"Sachin\"\n",
+ "v.result = PASS\n",
+ "v.grade = C\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nName : %s\"%(v.name))\n",
+ "sys.stdout.write(\"\\nResult : %d\"%(v.result))\n",
+ "sys.stdout.write(\"\\nGrade : %d\"%(v.grade))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Name : Sachin\n",
+ "Result : 1\n",
+ "Grade : 2"
+ ]
+ }
+ ],
+ "prompt_number": 53
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.23, Page number: 433<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Enumerated data type for 12 months\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no enumerated data type in python and an alternate is to use class\n",
+ "#Class declaration\n",
+ "class month:\n",
+ " Jan = 0\n",
+ " Feb = 1\n",
+ " Mar = 2\n",
+ " Apr = 3\n",
+ " May = 4\n",
+ " June = 5\n",
+ " July = 6\n",
+ " Aug = 7\n",
+ " Sep = 8\n",
+ " Oct = 9\n",
+ " Nov = 10\n",
+ " Dec = 11\n",
+ " \n",
+ "#Class variable declaration\n",
+ "c = month()\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nJan = %d\"%(c.Jan))\n",
+ "sys.stdout.write(\"\\nFeb = %d\"%(c.Feb))\n",
+ "sys.stdout.write(\"\\nJune = %d\"%(c.June))\n",
+ "sys.stdout.write(\"\\nDec = %d\"%(c.Dec))\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Jan = 0\n",
+ "Feb = 1\n",
+ "June = 5\n",
+ "Dec = 11"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.24, Page number: 434<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Enumerated data type \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no enumerated data type in python and an alternate is to use class\n",
+ "#Class declaration\n",
+ "class month:\n",
+ " Jan = 1\n",
+ " Feb = 2\n",
+ " Mar = 3\n",
+ " Apr = 4\n",
+ " May = 5\n",
+ " June = 6\n",
+ " July = 7\n",
+ " Aug = 8\n",
+ " Sep = 9\n",
+ " Oct = 10\n",
+ " Nov = 11\n",
+ " Dec = 12\n",
+ " \n",
+ "#Class variable declaration\n",
+ "c = month()\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nJan = %d\"%(c.Jan))\n",
+ "sys.stdout.write(\"\\nFeb = %d\"%(c.Feb))\n",
+ "sys.stdout.write(\"\\nJune = %d\"%(c.June))\n",
+ "sys.stdout.write(\"\\nDec = %d\"%(c.Dec))\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Jan = 1\n",
+ "Feb = 2\n",
+ "June = 6\n",
+ "Dec = 12"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.25, Page number: 434<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display name of month using enumerated data type\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no enumerated data type in python and an alternate is to use class\n",
+ "#Class declaration\n",
+ "class month:\n",
+ " Jan = 1\n",
+ " Feb = 2\n",
+ " Mar = 3\n",
+ " Apr = 4\n",
+ " May = 5\n",
+ " June = 6\n",
+ " July = 7\n",
+ " Aug = 8\n",
+ " Sep = 9\n",
+ " Oct = 10\n",
+ " Nov = 11\n",
+ " Dec = 12\n",
+ " \n",
+ "#Class variable declaration\n",
+ "c = month()\n",
+ "\n",
+ "#Result\n",
+ "for f in range(c.Jan,c.Dec+1):\n",
+ " #There is no switch case statement in python\n",
+ " if f == c.Jan:\n",
+ " sys.stdout.write(\"\\nJanuary\")\n",
+ " else:\n",
+ " if f == c.Feb:\n",
+ " sys.stdout.write(\"\\nFebruary\")\n",
+ " else:\n",
+ " if f == c.Mar:\n",
+ " sys.stdout.write(\"\\nMarch\")\n",
+ " else:\n",
+ " if f == c.Apr:\n",
+ " sys.stdout.write(\"\\nApril\")\n",
+ " else:\n",
+ " if f == c.May:\n",
+ " sys.stdout.write(\"\\nMay\")\n",
+ " else:\n",
+ " if f == c.June:\n",
+ " sys.stdout.write(\"\\nJune\")\n",
+ " else:\n",
+ " if f == c.July:\n",
+ " sys.stdout.write(\"\\nJuly\")\n",
+ " else:\n",
+ " if f == c.Aug:\n",
+ " sys.stdout.write(\"\\nAugust\")\n",
+ " else:\n",
+ " if f == c.Sep:\n",
+ " sys.stdout.write(\"\\nSeptember\")\n",
+ " else:\n",
+ " if f == c.Oct:\n",
+ " sys.stdout.write(\"\\nOctober\")\n",
+ " else:\n",
+ " if f == c.Nov:\n",
+ " sys.stdout.write(\"\\nNovember\")\n",
+ " else:\n",
+ " sys.stdout.write(\"\\nDecember\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "January\n",
+ "February\n",
+ "March\n",
+ "April\n",
+ "May\n",
+ "June\n",
+ "July\n",
+ "August\n",
+ "September\n",
+ "October\n",
+ "November\n",
+ "December"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.26, Page number: 436<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Enumerated data type\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no enumerated data type in python. class is used instead\n",
+ "class capital:\n",
+ " Mumbai = 0\n",
+ " Hyderabad = 1\n",
+ " Bangalore = 2\n",
+ "\n",
+ "class state:\n",
+ " name = ''\n",
+ " c = capital()\n",
+ " \n",
+ "#Class variable declaration\n",
+ "s = state()\n",
+ "c1 = capital()\n",
+ "\n",
+ "#Class variable initialization\n",
+ "s.name = \"Andhra Pradesh\"\n",
+ "s.c = s.c.Hyderabad\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nState : %s\"%(s.name))\n",
+ "sys.stdout.write(\"\\nCapital : %d\"%(s.c))\n",
+ "\n",
+ "if s.c == c1.Hyderabad:\n",
+ " sys.stdout.write(\"\\nHyderabad is Capital of %s\"%(s.name))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "State : Andhra Pradesh\n",
+ "Capital : 1\n",
+ "Hyderabad is Capital of Andhra Pradesh"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.27, Page number: 437<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Identify the type of entered character using enumerated data type.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration instead of enumerated data type\n",
+ "class ctype:\n",
+ " Letter = 0\n",
+ " Digit = 1\n",
+ " Other = 2\n",
+ " \n",
+ "#Variable Initialization\n",
+ "ch = raw_input(\"Enter any character\")\n",
+ "c = ctype()\n",
+ "f = ch.isalpha()\n",
+ "\n",
+ "#Result\n",
+ "if f != 0:\n",
+ " sys.stdout.write(\"\\n%c is %d type of symbol\"%(ch,c.Letter))\n",
+ "else:\n",
+ " f = ch.isdigit()\n",
+ " if f != 0:\n",
+ " sys.stdout.write(\"\\n%c is %d type of symbol\"%(ch,c.Digit))\n",
+ " else:\n",
+ " sys.stdout.write(\"\\n%c is %d type of symbol\"%(ch,c.Other))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any character=\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "= is 2 type of symbol"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.28, Page number: 438<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Size of union and number of bytes reserved for it\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no union/structure in python. class is used instead\n",
+ "#Class declaration\n",
+ "class result:\n",
+ " marks = 0\n",
+ " grade = ''\n",
+ " \n",
+ "class res:\n",
+ " name = ''\n",
+ " age = 0\n",
+ " perf = result()\n",
+ " \n",
+ "#Class variable declaration\n",
+ "data = res()\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"Size of Union : %d\\n\"%(sys.getsizeof(data.perf)))\n",
+ "sys.stdout.write(\"Size of Structure : %d\\n\"%(sys.getsizeof(data)))\n",
+ "\n",
+ "#in python, value tagged method is used for data storage and can represent large numbers"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Size of Union : 36\n",
+ "Size of Structure : 36\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.29, Page number: 440<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Memory size of the computer\n",
+ "\n",
+ "import psutil\n",
+ "\n",
+ "psutil.phymem_usage()\n",
+ "\n",
+ "#Displays current status of the memory\n",
+ "#different systems will have different memory status"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "pyout",
+ "prompt_number": 4,
+ "text": [
+ "usage(total=1600512000L, used=1496383488L, free=104128512L, percent=93.5)"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.30, Page number: 440<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display the system time at specified cursor position\n",
+ "\n",
+ "import sys\n",
+ "import datetime\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"%s\"%(datetime.datetime.now()))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2013-09-20 11:04:15.649000"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.31, Page number: 441<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Change the cursor in different sizes\n",
+ "\n",
+ "import turtle\n",
+ "#Ipython supports graphics through turtle module\n",
+ "\n",
+ "from Tkinter import *\n",
+ "import Tkinter\n",
+ "\n",
+ "top = Tkinter.Tk()\n",
+ "\n",
+ "B1 = Tkinter.Button(top, text =\"circle\", relief=RAISED,\\\n",
+ " cursor=\"circle\")\n",
+ "B2 = Tkinter.Button(top, text =\"plus\", relief=RAISED,\\\n",
+ " cursor=\"plus\")\n",
+ "B1.pack()\n",
+ "B2.pack()\n",
+ "#top.mainloop()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.32, Page number: 441<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Create a directory using dos interrupt\n",
+ "\n",
+ "import os\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "dir1 = raw_input(\"Enter Directory Name : \")\n",
+ "\n",
+ "#Result\n",
+ "if not os.path.exists(dir1):\n",
+ " os.makedirs(dir1)\n",
+ " sys.stdout.write(\"Directory %s created\"%(dir1))\n",
+ "else:\n",
+ " sys.stdout.write(\"Directory %s not created\"%(dir1))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Directory XYZ created"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.33, Page number: 442<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display the given character on the screen\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "dl = 67\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"%c\"%(chr(dl)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "C"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.34, Page number: 442<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display the attributes of a file using DOS interrupt\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "import os\n",
+ "\n",
+ "print os.stat(\"IO.SYS\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=0L, st_atime=1383454467L, st_mtime=1383454467L, st_ctime=1383454467L)\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.35, Page number: 444<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Delete a file using dos interrupt\n",
+ "\n",
+ "import os\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "file1 = raw_input(\"Enter a file name : \")\n",
+ "\n",
+ "try:\n",
+ " os.remove(file1)\n",
+ " sys.stdout.write(\"File successfully deleted\")\n",
+ "except:\n",
+ " sys.stdout.write(\"File could not be deleted\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "File successfully deleted"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.36, Page number: 445<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Structuret within union\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#there is no union/structure in python. class is used instead\n",
+ "#Class declaration\n",
+ "class x:\n",
+ " f = 0.0\n",
+ " p = ['' for i in range(0,2)]\n",
+ " \n",
+ "class z:\n",
+ " set1 = x()\n",
+ " \n",
+ "#Class variable declaration and initialization\n",
+ "st = z()\n",
+ "\n",
+ "st.set1.f = 5.5\n",
+ "st.set1.p[0] = 65\n",
+ "st.set1.p[1] = 66\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\n%g\"%(st.set1.f))\n",
+ "sys.stdout.write(\"\\n%c\"%(st.set1.p[0]))\n",
+ "sys.stdout.write(\"\\n%c\"%(st.set1.p[1]))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "5.5\n",
+ "A\n",
+ "B"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file |