{ "metadata": { "name": "", "signature": "sha256:3c26b0f763ca451c03b73133e1c2db791af8fb7ada7862c248f0876f5e8843fe" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 9 : Structures" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.1, Page No 194" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Prod:\n", " def __init__(self, cName, iId, dPrice, iNo, cSupp):\n", " self.cName = cName\n", " self.iId = iId\n", " self.dPrice = dPrice\n", " self.iNo = iNo\n", " self.cSupp = cSupp\n", " def println(self):\n", " print self.cName + \"\\t \",\n", " print str(self.iId) + \"\\t \",\n", " print str(self.dPrice) + \"\\t \",\n", " print str(self.iNo) + \"\\t \",\n", " print self.cSupp + \"\\t\\n\",\n", "if __name__ == '__main__':\n", " prodOne = Prod(\"Olive Oil\",1001,120.50,250,\"Frescati Oil S/A\")\n", " print \"Enter information for product\"\n", " cName = raw_input(\"Start with the product name: \")\n", " iId = int(raw_input(\"The product id: \"))\n", " dPrice = float(raw_input(\"The price: \"))\n", " iNo = int(raw_input(\"How many items are there in stock: \"))\n", " cSupp = raw_input(\"Who supplies the product: \")\n", " prodTwo = Prod(cName,iId,dPrice,iNo,cSupp)\n", " print \"Productname \\t Product id \\t Price \\t Quantity \\t Supplier\\n\"\n", " prodOne.println()\n", " prodTwo.println()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter information for product\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Start with the product name: Coconut Oil\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "The product id: 1002\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "The price: 120.50\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "How many items are there in stock: 230\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Who supplies the product: Frescati Oil S/A\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Productname \t Product id \t Price \t Quantity \t Supplier\n", "\n", "Olive Oil\t 1001\t 120.5\t 250\t Frescati Oil S/A\t\n", "Coconut Oil\t 1002\t 120.5\t 230\t Frescati Oil S/A\t\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.2, Page No 200" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Prod:\n", " def __init__(self, cName, iId, dPrice, iNo, cSupp):\n", " self.cName = cName\n", " self.iId = iId\n", " self.dPrice = dPrice\n", " self.iNo = iNo\n", " self.cSupp = cSupp\n", "def printOnScreen(prodOne1):\n", " print prodOne1.cName + \"\\t \",\n", " print str(prodOne1.iId) + \"\\t \",\n", " print str(prodOne1.dPrice) + \"\\t \",\n", " print str(prodOne1.iNo) + \"\\t \",\n", " print prodOne1.cSupp + \"\\t\\n\",\n", "\n", "if __name__ == '__main__':\n", " prodOne = Prod(\"Olive Oil\",1001,120.50,250,\"Frescati Oil S/A\")\n", " printOnScreen(prodOne)\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Olive Oil\t 1001\t 120.5\t 250\t Frescati Oil S/A\t\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.3, Page No 202" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Prod:\n", " def __init__(self, cName, iId, dPrice, iNo, cSupp):\n", " self.cName = cName\n", " self.iId = iId\n", " self.dPrice = dPrice\n", " self.iNo = iNo\n", " self.cSupp = cSupp\n", "def printOnScreen(p,n):\n", " for i in range(n):\n", " print p[i].cName + \"\\t \",\n", " print str(p[i].iId) + \"\\t \",\n", " print str(p[i].dPrice) + \"\\t \",\n", " print str(p[i].iNo) + \"\\t \",\n", " print p[i].cSupp + \"\\t\\n\",\n", "if __name__ == '__main__':\n", " sProds = []\n", " sProds.append(Prod(\"Food Oil\",101,12.50,100,\"Felix Ltd\"))\n", " sProds.append(Prod(\"Baby Oil\",102,23.75,25,\"Baby Prod\"))\n", " sProds.append(Prod(\"Boiler Oil\",103,6100,123000,\"Shell\"))\n", " printOnScreen(sProds,3)\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Food Oil\t 101\t 12.5\t 100\t Felix Ltd\t\n", "Baby Oil\t 102\t 23.75\t 25\t Baby Prod\t\n", "Boiler Oil\t 103\t 6100\t 123000\t Shell\t\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.4, Page No 205" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Prod:\n", " def __init__(self, cName, iId, dPrice, iNo, cSupp):\n", " self.cName = cName\n", " self.iId = iId\n", " self.dPrice = dPrice\n", " self.iNo = iNo\n", " self.cSupp = cSupp\n", "def printOnScreen(p,n):\n", " for i in range(n):\n", " print p[i].cName + \"\\t \",\n", " print str(p[i].iId) + \"\\t \",\n", " print str(p[i].dPrice) + \"\\t \",\n", " print str(p[i].iNo) + \"\\t \",\n", " print p[i].cSupp + \"\\t\\n\",\n", "if __name__ == '__main__':\n", " sProds = []\n", " sProds.append(Prod(\"Food Oil\",101,12.50,100,\"Felix Ltd\"))\n", " sProds.append(Prod(\"Baby Oil\",102,23.75,25,\"Baby Prod\"))\n", " sProds.append(Prod(\"Boiler Oil\",103,6100,123000,\"Shell\"))\n", " pProds = id(sProds)\n", " printOnScreen(sProds,3)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Food Oil\t 101\t 12.5\t 100\t Felix Ltd\t\n", "Baby Oil\t 102\t 23.75\t 25\t Baby Prod\t\n", "Boiler Oil\t 103\t 6100\t 123000\t Shell\t\n" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }