{
 "metadata": {
  "name": "",
  "signature": "sha256:987b4dacc78713a17909fe49b56cfa38644dbc08cbbaecbb827fc7655cf390d8"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter7, Structures and File Input/Output"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C28ST1, Page number:592"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from collections import namedtuple\n",
      "\n",
      "#Structure declaration and definition\n",
      "struct_cd_collection = namedtuple(\"struct_cd_collection\", \"title artist num_songs price date_purch\")\n",
      "\n",
      "#Structure for Cd Collection\n",
      "cd1 = struct_cd_collection(\"Red Moon Men\", \"Sam and the Sneeds\", 12,11.95,\"02/13/92\")\n",
      "\n",
      "#Result\n",
      "print \"Here is the CD information :\\n\\n\"\n",
      "print \"Title:\",cd1.title,\"\\n\"\n",
      "print \"Artist:\",cd1.artist,\"\\n\"\n",
      "print \"Songs:\",cd1.num_songs,\"\\n\"\n",
      "print \"Price:\",cd1.price,\"\\n\"\n",
      "print \"Date purchased:\",cd1.date_purch,\"\\n\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Here is the CD information :\n",
        "\n",
        "\n",
        "Title: Red Moon Men \n",
        "\n",
        "Artist: Sam and the Sneeds \n",
        "\n",
        "Songs: 12 \n",
        "\n",
        "Price: 11.95 \n",
        "\n",
        "Date purchased: 02/13/92 \n",
        "\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C28ST2, Page number:593"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from collections import namedtuple\n",
      "\n",
      "#Structure initial declaration\n",
      "students = namedtuple(\"students\", \"name age average\")\n",
      "\n",
      "\n",
      "#Get data\n",
      "print \"What is first student's name? \",\n",
      "x=raw_input()\n",
      "print \"What is the first student's age? \",\n",
      "y=input()\n",
      "print \"What is the first student's average ? \",\n",
      "z=input()\n",
      "\n",
      "#Assign data to the structure variable\n",
      "student1=students(x,y,z)\n",
      "\n",
      "print \"\\n\"\n",
      "\n",
      "#Get data\n",
      "print \"What is second student's name? \",\n",
      "x=raw_input()\n",
      "print \"What is the second student's age? \",\n",
      "y=input()\n",
      "print \"What is the second student's average ? \",\n",
      "z=input()\n",
      "\n",
      "#Assign data to the structure variable\n",
      "student2=students(x,y,z)\n",
      "\n",
      "#Result\n",
      "print \"\\n\\nHere is the student information you entered:\\n\\n\"\n",
      "print \"Student #1:\\n\"\n",
      "print \"Name:    \",student1.name,\"\\n\"\n",
      "print \"Age:     \",student1.age,\"\\n\"\n",
      "print \"Average: \",'%.2f'%student1.average,\"\\n\"\n",
      "\n",
      "print \"Student #2:\\n\"\n",
      "print \"Name:    \",student2.name,\"\\n\"\n",
      "print \"Age:     \",student2.age,\"\\n\"\n",
      "print \"Average: \",'%.2f'%student2.average,\"\\n\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "What is first student's name? "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Larry\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " What is the first student's age? "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "14\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " What is the first student's average ? "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "87.67\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " \n",
        "\n",
        "What is second student's name? "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Judy\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " What is the second student's age? "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "15\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " What is the second student's average ? "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "95.38\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " \n",
        "\n",
        "Here is the student information you entered:\n",
        "\n",
        "\n",
        "Student #1:\n",
        "\n",
        "Name:     Larry \n",
        "\n",
        "Age:      14 \n",
        "\n",
        "Average:  87.67 \n",
        "\n",
        "Student #2:\n",
        "\n",
        "Name:     Judy \n",
        "\n",
        "Age:      15 \n",
        "\n",
        "Average:  95.38 \n",
        "\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C28ST3, Page number:596"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Function definitions\n",
      "\n",
      "def fill_structs(student_var):\n",
      "    #Get student data\n",
      "    print \"What is student's name? \",\n",
      "    x=raw_input()\n",
      "    print \"What is the student's age? \",\n",
      "    y=input()\n",
      "    print \"What is the student's average ? \",\n",
      "    z=input()\n",
      "    student_var=students(x,y,z)\n",
      "    return student_var\n",
      "\n",
      "\n",
      "def pr_students(student_var):\n",
      "    print \"Name:    \",student_var.name,\"\\n\",\n",
      "    print \"Age:     \",student_var.age,\"\\n\",\n",
      "    print \"Average: \",student_var.average,\"\\n\",\n",
      "\n",
      "#Structure declaration\n",
      "from collections import namedtuple\n",
      "students=namedtuple(\"students\",\"name age average\")\n",
      "\n",
      "#Structure variable declaration\n",
      "student1=students(\" \",0,0.0)\n",
      "student2=students(\" \",0,0.0)\n",
      "\n",
      "#Stucture variable is passed as copy to the function\n",
      "student1=fill_structs(student1)\n",
      "student2=fill_structs(student2)\n",
      "\n",
      "#Result\n",
      "print \"\\n\\nHere is the student information you entered:\\n\\n\"\n",
      "pr_students(student1)\n",
      "pr_students(student2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "What is student's name? "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Larry\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " What is the student's age? "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "14\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " What is the student's average ? "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "87.67\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " What is student's name? "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Judy\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " What is the student's age? "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "15\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " What is the student's average ? "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "97.38\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " \n",
        "\n",
        "Here is the student information you entered:\n",
        "\n",
        "\n",
        "Name:     Larry \n",
        "Age:      14 \n",
        "Average:  87.67 \n",
        "Name:     Judy \n",
        "Age:      15 \n",
        "Average:  97.38 \n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C28CUST, Page number:598"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Structure definition\n",
      "\n",
      "from collections import namedtuple\n",
      "customer_rec=namedtuple(\"customer_rec\",\"cust_name balance dist_rate\")\n",
      "\n",
      "customer=customer_rec(\"Steve Thompson\",431.23,.25)\n",
      "print \"Before the update\",customer.cust_name,\" has a balance of $\",'%.2f' %customer.balance,\"\\n\",\n",
      "\n",
      "#Update the balance\n",
      "customer_rec.balance=customer.balance*(1.0-customer.dist_rate)\n",
      "\n",
      "#Result\n",
      "print \"After the update\",customer.cust_name,\" has a balance of $\",'%.2f' %customer.balance,\"\\n\","
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Before the update Steve Thompson  has a balance of $ 431.23 \n",
        "After the update Steve Thompson  has a balance of $ 323.42 \n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C28STCPY, Page number:599"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Structure declaration\n",
      "from collections import namedtuple\n",
      "students=namedtuple(\"students\",\"st_name grade age average\")\n",
      "\n",
      "#Structure variable declaration\n",
      "std1=students(\"Joe Brown\",'A',13,91.4)\n",
      "std2=students(\" \",\" \",0,0.0)\n",
      "std3=students(\" \",\" \",0,0.0)\n",
      "\n",
      "#Assigning one structure variable to another.\n",
      "std2=std1\n",
      "std3=std1\n",
      "\n",
      "\n",
      "#Result\n",
      "print \"The contents of std2:\\n\",\n",
      "print std2.st_name,\", \",std2.grade,\", \",std2.age,\", \",'%.1f' %std2.average,\"\\n\\n\"\n",
      "\n",
      "print \"The contents of std3:\\n\",\n",
      "print std3.st_name,\", \",std3.grade,\", \",std3.age,\", \",'%.1f' %std3.average,\"\\n\\n\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The contents of std2:\n",
        "Joe Brown ,  A ,  13 ,  91.4 \n",
        "\n",
        "\n",
        "The contents of std3:\n",
        "Joe Brown ,  A ,  13 ,  91.4 \n",
        "\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C30WR1, Page number:635"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#open a new file in write mode\n",
      "fp = open ( \"C:\\Users\\THEBEST\\Desktop\\NAMES.txt\", \"w\" ) \n",
      "\n",
      "fp.writelines(\"Michel Langston\\n\")\n",
      "fp.writelines(\"Sally Redding\\n\")\n",
      "fp.writelines(\"Jane Kirk\\n\")\n",
      "fp.writelines(\"Stacy Wikert\\n\")\n",
      "fp.writelines(\"Joe Hiquet\\n\")\n",
      "\n",
      "#Close file\n",
      "fp.close()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C30WR2, Page number:636"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Creates a file in write mode\n",
      "fp = open ( \"C:\\Users\\THEBEST\\Desktop\\NUMS.1.txt\", \"w\" )      \n",
      "\n",
      "if not fp:\n",
      "    print \"Error opening file.\\n\"\n",
      "else:\n",
      "    for ctr in range(1,101,1):\n",
      "        fp.write('%d' %ctr)                     #Writes number from 1-100 into the file\n",
      "fp.close()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C30AP1, Page number:638"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#opens existing file\n",
      "fp = open ( \"C:\\Users\\THEBEST\\Desktop\\NAMES.TXT\", \"a\" ) \n",
      "\n",
      "#Adds to file\n",
      "fp.writelines(\"Johnny Smith\\n\")\n",
      "fp.writelines(\"Laura Hull \\n\")\n",
      "fp.writelines(\"Mark Brown\\n\")\n",
      "\n",
      "#Close file\n",
      "fp.close()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C30RE1, Page number:640"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#print \"What is the name of the file you want to see? \"\n",
      "#filename=raw_input()\n",
      "filename = \"NAMES.txt\"\n",
      "filename=\"C:/Users/THEBEST/Desktop/\"+ filename\n",
      "\n",
      "#Open file in read mode\n",
      "fp = open (filename,\"rb\")\n",
      "if not fp:\n",
      "    print \"\\n\\n*** That file does not exist ***\\n\"\n",
      "    exit(0)\n",
      "else:\n",
      "        print in_char\n",
      "\n",
      "#Close file\n",
      "fp.close()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C30RE2, Page number:641"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#File1 to take backup\n",
      "print \"What is the name of the file you want to back up? \"\n",
      "in_filename=raw_input()\n",
      "\n",
      "#File2 to copy contents\n",
      "print \"What is the name of the file you want to copy \",in_filename,\"to? \"\n",
      "out_filename=raw_input()\n",
      "\n",
      "in_fp=open(in_filename,\"r\")\n",
      "if not in_fp:\n",
      "    print \"\\n\\n*** \",in_filename,\"does not exist***\\n\"\n",
      "    exit(0)\n",
      "else:\n",
      "    out_fp=open(out_filename,\"w\")\n",
      "    if not out_fp:\n",
      "        print \"\\n\\n*** Error opening \",out_filename,\"***\\n\"\n",
      "        exit(0)\n",
      "    else:\n",
      "        print \"\\nCopying...\\n\"                          #Reads from old file \n",
      "        for in_char in in_fp.readlines():               #and copies it to new file\n",
      "            out_fp.writelines(in_char)\n",
      "        print \"\\nThe file is copied.\\n\"\n",
      "\n",
      "#Close all files\n",
      "in_fp.close()\n",
      "out_fp.close()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C32CON, Page number:664"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from math import *\n",
      "PI = 3.14159\n",
      "\n",
      "class Sphere:                                                           #Class declaration\n",
      "    def __init__(self, xcoord,ycoord,zcoord,radius):\n",
      "        self.r = radius\n",
      "        self.x = xcoord\n",
      "        self.y = ycoord\n",
      "        self.z = zcoord\n",
      "        \n",
      "    def volume(self):\n",
      "        return r*r*r*4*PI/3\n",
      "\n",
      "    def surface_area(self):\n",
      "        return r*r*4*PI\n",
      "\n",
      "def main():\n",
      "    s = Sphere(1.0,2.0,3.0,4.0)                                      #Class object\n",
      "    \n",
      "    print \"X=\",s.x,\", Y=\",s.y,\", Z=\",s.z,\", R=\",s.r         #Result\n",
      "\n",
      "main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "X= 1.0 , Y= 2.0 , Z= 3.0 , R= 4.0\n"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C32DES, Page number:665"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from math import *\n",
      "PI = 3.14159\n",
      "\n",
      "#Class declaration\n",
      "class Sphere:                                                          \n",
      "    def __init__( self,xcoord,ycoord,zcoord,radius):\n",
      "        self.r = radius\n",
      "        self.x = xcoord\n",
      "        self.y = ycoord\n",
      "        self.z = zcoord\n",
      "\n",
      "    def __del__(self):\n",
      "        print \"Sphere(\",self.x,\",\" ,self.y,\",\",self.z,\",\",self.r,\") destroyed\"\n",
      "\n",
      "        \n",
      "    def volume(self):\n",
      "        return self.r*self.r*self.r*4*PI/3\n",
      "\n",
      "    def surface_area(self):\n",
      "        return self.r*self.r*4*PI\n",
      "\n",
      "def main():\n",
      "    #Class object\n",
      "    s = Sphere(1.0,2.0,3.0,4.0)                                      \n",
      "    \n",
      "    #Result\n",
      "    print \"X=\",s.x,\", Y=\",s.y,\", Z=\",s.z,\", R=\",s.r         \n",
      "\n",
      "\n",
      "main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "X= 1.0 , Y= 2.0 , Z= 3.0 , R= 4.0\n",
        "Sphere( 1.0 , 2.0 , 3.0 , 4.0 ) destroyed\n"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C32MEM, Page number:667"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from math import *\n",
      "PI = 3.14159\n",
      "\n",
      "#Class declaration\n",
      "class Sphere:                                                           \n",
      "    def __init__( self,xcoord,ycoord,zcoord,radius):\n",
      "        self.r = radius\n",
      "        self.x = xcoord\n",
      "        self.y = ycoord\n",
      "        self.z = zcoord\n",
      "\n",
      "    def __del__(self):\n",
      "        print \"Sphere(\",self.x,\",\" ,self.y,\",\",self.z,\",\",self.r,\") destroyed\"\n",
      "\n",
      "        \n",
      "    def volume(self):\n",
      "        return self.r*self.r*self.r*4*PI/3\n",
      "\n",
      "    def surface_area(self):\n",
      "        return self.r*self.r*4*PI\n",
      "\n",
      "def main():\n",
      "    #Class object\n",
      "    s = Sphere(1.0,2.0,3.0,4.0)                                      \n",
      "    #Result\n",
      "    print \"X=\",s.x,\", Y=\",s.y,\", Z=\",s.z,\", R=\",s.r         \n",
      "    print \"The volume is \",s.volume(),\"\\n\",\n",
      "    print \"The surface area is \",s.surface_area(),\"\\n\",\n",
      "\n",
      "main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "X= 1.0 , Y= 2.0 , Z= 3.0 , R= 4.0\n",
        "The volume is  268.082346667 \n",
        "The surface area is  201.06176 \n",
        "Sphere( 1.0 , 2.0 , 3.0 , 4.0 ) destroyed\n"
       ]
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C32MEM1, Page number:668"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from math import *\n",
      "PI = 3.14159\n",
      "\n",
      "#Class declaration\n",
      "class Sphere:                                                           \n",
      "    def __init__( self,xcoord,ycoord,zcoord,radius):\n",
      "        self.r = radius\n",
      "        self.x = xcoord\n",
      "        self.y = ycoord\n",
      "        self.z = zcoord\n",
      "\n",
      "    def __del__(self):\n",
      "        print \"Sphere(\",self.x,\",\" ,self.y,\",\",self.z,\",\",self.r,\") destroyed\"\n",
      "\n",
      "    def volume(self):\n",
      "        return self.r*self.r*self.r*4*PI/3\n",
      "\n",
      "\n",
      "\n",
      "    def surface_area(self):\n",
      "        return self.r*self.r*4*PI\n",
      "\n",
      "def main():\n",
      "    #Class object\n",
      "    s = Sphere(1.0,2.0,3.0,4.0)                                      \n",
      "    #Result\n",
      "    print \"X=\",s.x,\", Y=\",s.y,\", Z=\",s.z,\", R=\",s.r         \n",
      "    print \"The volume is \",s.volume(),\"\\n\",       \n",
      "    print \"The surface area is \",s.surface_area(),\"\\n\",\n",
      "\n",
      "main()\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "X= 1.0 , Y= 2.0 , Z= 3.0 , R= 4.0\n",
        "The volume is  268.082346667 \n",
        "The surface area is  201.06176 \n",
        "Sphere( 1.0 , 2.0 , 3.0 , 4.0 ) destroyed\n"
       ]
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C32MEM1A, Page number:669"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from math import *\n",
      "PI = 3.14159\n",
      "\n",
      "#Class declaration\n",
      "class Sphere:                                                           \n",
      "    def __init__( self,xcoord,ycoord,zcoord,radius):\n",
      "        self.r = radius\n",
      "        self.x = xcoord\n",
      "        self.y = ycoord\n",
      "        self.z = zcoord\n",
      "\n",
      "    def __del__(self):\n",
      "        print \"Sphere(\",self.x,\",\" ,self.y,\",\",self.z,\",\",self.r,\") destroyed\"\n",
      "\n",
      "    def volume(self):\n",
      "        return self.r*self.r*self.r*4*PI/3\n",
      "\n",
      "\n",
      "\n",
      "    def surface_area(self):\n",
      "        return self.r*self.r*4*PI\n",
      "\n",
      "def main():\n",
      "    #Class object\n",
      "    s = Sphere(1.0,2.0,3.0,4.0)                                      \n",
      "    #Result\n",
      "    print \"X=\",s.x,\", Y=\",s.y,\", Z=\",s.z,\", R=\",s.r         \n",
      "    print \"The volume is \",(s.r*s.r*s.r*4*PI/3),\"\\n\",       #volume() function is expanded here\n",
      "    print \"The surface area is \",s.surface_area(),\"\\n\",\n",
      "\n",
      "main()\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "X= 1.0 , Y= 2.0 , Z= 3.0 , R= 4.0\n",
        "The volume is  268.082346667 \n",
        "The surface area is  201.06176 \n",
        "Sphere( 1.0 , 2.0 , 3.0 , 4.0 ) destroyed\n"
       ]
      }
     ],
     "prompt_number": 19
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C32DEF, Page number:671"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from math import *\n",
      "PI = 3.14159\n",
      "\n",
      "#Class declaration\n",
      "class Sphere:                                                           \n",
      "    def __init__( self,xcoord,ycoord=2.0,zcoord=2.5,radius=1.0):\n",
      "        self.r = radius\n",
      "        self.x = xcoord\n",
      "        self.y = ycoord\n",
      "        self.z = zcoord\n",
      "\n",
      "    def __del__(self):\n",
      "        print \"Sphere(\",self.x,\",\" ,self.y,\",\",self.z,\",\",self.r,\") destroyed\"\n",
      "\n",
      "    def volume(self):\n",
      "        return self.r*self.r*self.r*4*PI/3\n",
      "\n",
      "\n",
      "\n",
      "    def surface_area(self):\n",
      "        return self.r*self.r*4*PI\n",
      "\n",
      "def main():\n",
      "\n",
      "    #Class objects\n",
      "    s = Sphere(1.0)                                     \n",
      "    t = Sphere(1.0,1.1)\n",
      "    u = Sphere(1.0,1.1,1.2)\n",
      "    v = Sphere(1.0,1.1,1.2,1.3)\n",
      "\n",
      "    #Result\n",
      "    print \"s: X=\",s.x,\", Y=\",s.y,\", Z=\",s.z,\", R=\",s.r         \n",
      "    print \"The volume is \",s.volume(),\"\\n\",       \n",
      "    print \"The surface area is \",s.surface_area(),\"\\n\",\n",
      "\n",
      "    print \"t: X=\",t.x,\", Y=\",t.y,\", Z=\",t.z,\", R=\",t.r        \n",
      "    print \"The volume is \",t.volume(),\"\\n\",      \n",
      "    print \"The surface area is \",t.surface_area(),\"\\n\",\n",
      "\n",
      "    print \"u: X=\",u.x,\", Y=\",u.y,\", Z=\",u.z,\", R=\",u.r         \n",
      "    print \"The volume is \",u.volume(),\"\\n\",       \n",
      "    print \"The surface area is \",u.surface_area(),\"\\n\",\n",
      "\n",
      "    print \"v: X=\",v.x,\", Y=\",v.y,\", Z=\",v.z,\", R=\",v.r        \n",
      "    print \"The volume is \",v.volume(),\"\\n\",       \n",
      "    print \"The surface area is \",v.surface_area(),\"\\n\",\n",
      "\n",
      "\n",
      "main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s: X= 1.0 , Y= 2.0 , Z= 2.5 , R= 1.0\n",
        "The volume is  4.18878666667 \n",
        "The surface area is  12.56636 \n",
        "t: X= 1.0 , Y= 1.1 , Z= 2.5 , R= 1.0\n",
        "The volume is  4.18878666667 \n",
        "The surface area is  12.56636 \n",
        "u: X= 1.0 , Y= 1.1 , Z= 1.2 , R= 1.0\n",
        "The volume is  4.18878666667 \n",
        "The surface area is  12.56636 \n",
        "v: X= 1.0 , Y= 1.1 , Z= 1.2 , R= 1.3\n",
        "The volume is  9.20276430667 \n",
        "The surface area is  21.2371484 \n",
        "Sphere( 1.0 , 2.0 , 2.5 , 1.0 ) destroyed\n",
        "Sphere( 1.0 , 1.1 , 2.5 , 1.0 ) destroyed\n",
        "Sphere( 1.0 , 1.1 , 1.2 , 1.0 ) destroyed\n",
        "Sphere( 1.0 , 1.1 , 1.2 , 1.3 ) destroyed\n"
       ]
      }
     ],
     "prompt_number": 20
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "C32OVCON, Page number:673"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from math import *\n",
      "PI = 3.14159\n",
      "\n",
      "#Class declaration\n",
      "class Sphere:                                                               \n",
      "    def __init__( self,xcoord,ycoord,zcoord,radius):\n",
      "        self.r = radius\n",
      "        self.x = xcoord\n",
      "        self.y = ycoord\n",
      "        self.z = zcoord\n",
      "\n",
      "    def __del__(self):\n",
      "        print \"Sphere(\",self.x,\",\" ,self.y,\",\",self.z,\",\",self.r,\") destroyed\"\n",
      "\n",
      "    def volume(self):\n",
      "        return self.r*self.r*self.r*4*PI/3\n",
      "\n",
      "\n",
      "\n",
      "    def surface_area(self):\n",
      "        return self.r*self.r*4*PI\n",
      "\n",
      "def main():\n",
      "    #Class object\n",
      "    s = Sphere(1.0,2.0,3.0,4.0)                                      \n",
      "    #Result\n",
      "    print \"X=\",s.x,\", Y=\",s.y,\", Z=\",s.z,\", R=\",s.r         \n",
      "    print \"The volume is \",(s.r*s.r*s.r*4*PI/3),\"\\n\",       #volume() function is expanded here\n",
      "    print \"The surface area is \",s.surface_area(),\"\\n\",\n",
      "\n",
      "main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "X= 1.0 , Y= 2.0 , Z= 3.0 , R= 4.0\n",
        "The volume is  268.082346667 \n",
        "The surface area is  201.06176 \n",
        "Sphere( 1.0 , 2.0 , 3.0 , 4.0 ) destroyed\n"
       ]
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}