{ "metadata": { "name": "", "signature": "sha256:bd4e7931ddf89d8dc8befb681e1e57c7a9c742cd8abe8e18a494a55156d56cee" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 6: Objects and Classes" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.1, Page Number: 216" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class smallobj: #define a class\n", " \n", " def setdata(self,d): #member function to set class variable somdata\n", " self.__somedata = d\n", " \n", " def showdata(self): #member function to display somedata \n", " print 'Data is ' , self.__somedata\n", "\n", "\n", "#define two objects of class smallobj\n", "s1=smallobj()\n", "s2=smallobj()\n", "\n", "#call member function to set data \n", "s1.setdata(1066)\n", "s2.setdata(1776)\n", "\n", "#call member function to display data \n", "s1.showdata()\n", "s2.showdata()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Data is 1066\n", "Data is 1776\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.2, Page Number: 223" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class part: #define class \n", " \n", " def setpart(self,mn,pn,c): #set data\n", " self.__modelnumber = mn\n", " self.__partnumber = pn\n", " self.__cost = c\n", " \n", " def showpart(self): #display data \n", " print 'Model' , self.__modelnumber ,\n", " print ', part' , self.__partnumber , \n", " print ', costs $',self.__cost\n", " \n", "#define object of class part \n", "part1 = part()\n", "\n", "#call member function setpart\n", "part1.setpart(6244,373,217.55)\n", "\n", "#call member function showpart\n", "part1.showpart()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Model 6244 , part 373 , costs $ 217.55\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.3, Page Number: 225" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from turtle import * #importing turtles library\n", "\n", "class circle: #defining circle class\n", " \n", " def set(self,x,y,r,fc): #sets circle attribute\n", " self._xCo = x\n", " self._yCo = y\n", " self._radius = r\n", " self._fillcolor = fc\n", " \n", " def draw(self): #draws the circle \n", " setup() #set screen\n", " turtle = Turtle() #object of Turtle class\n", " turtle.begin_fill() #start filling color in circle\n", " turtle.color(self._fillcolor) #color\n", " turtle.up()\n", " turtle.goto(self._xCo,self._yCo) #set center of circle\n", " turtle.circle(self._radius) #draw circle of radius self.__radius\n", " turtle.end_fill() #stop filling\n", " turtle.hideturtle()\n", " done()\n", "\n", "#creating objects of class circle \n", "c1 = circle()\n", "c2 = circle()\n", "c3 = circle()\n", "\n", "#sending the value to set fnction\n", "c1.set(15,7,5,\"blue\")\n", "c2.set(41,12,7,\"red\")\n", "c3.set(65,18,4,\"green\")\n", "\n", "#draw circle\n", "c1.draw()\n", "c2.draw()\n", "c3.draw()\n", "\n", "#In the above example the cirlcle's in the book are constructed using 'X' and 'O' but such feature is not available in Python.\n", "#So i have created a simple circle filled with color" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.4, Page Number: 226" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Distance: #Distance class\n", " \n", " def setdist(self,ft,inc): #set distance to class variables\n", " self.__feet = ft\n", " self.__inches = inc\n", " \n", " def getdist(self): #get distance from user\n", " self.__feet = input('Enter feet:')\n", " self.__inches = input('Enter inches:')\n", " \n", " def showdist(self): #display distance\n", " print self.__feet , '\\' -' , self.__inches , '\\\"'\n", "\n", "#define two distance\n", "dist1 = Distance()\n", "dist2 = Distance()\n", "\n", "dist1.setdist(11,6.25) #set dist1\n", "dist2.getdist() #set dist2 from user\n", "\n", "#show distances\n", "print \"dist1 = \",\n", "dist1.showdist()\n", "print 'dist2 = ',\n", "dist2.showdist()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter feet:17\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter inches:5.75\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "dist1 = 11 ' - 6.25 \"\n", "dist2 = 17 ' - 5.75 \"\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.5, Page Number: 228" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Counter:\n", " \n", " def __init__(self): #constructor\n", " self.__count = 0\n", " \n", " def inc_count(self): #increment count\n", " self.__count = self.__count + 1\n", " \n", " def get_count(self): #return count\n", " return self.__count\n", "\n", "#define and initialize class objects\n", "c1=Counter()\n", "c2=Counter()\n", "\n", "#display count for each object\n", "print 'c1 =',c1.get_count()\n", "print 'c2 =',c2.get_count()\n", "\n", "\n", "c1.inc_count() #increment c1\n", "c2.inc_count() #increment c2\n", "c2.inc_count() #increment c2\n", "\n", "#display count again for each object\n", "print 'c1 =',c1.get_count()\n", "print 'c2 =',c2.get_count()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "c1 = 0\n", "c2 = 0\n", "c1 = 1\n", "c2 = 2\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.6, Page Number: 231" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from turtle import * #importing turtles library\n", "\n", "class circle: #defining circle class\n", " \n", " def __init__(self,x,y,r,fc): #constructor for set circle attribute\n", " self._xCo = x\n", " self._yCo = y\n", " self._radius = r\n", " self._fillcolor = fc\n", " \n", " def draw(self): #draws the circle\n", " setup()\n", " turtle = Turtle()\n", " turtle.begin_fill()\n", " turtle.color(self._fillcolor)\n", " turtle.up()\n", " turtle.goto(self._xCo,self._yCo)\n", " turtle.down()\n", " turtle.circle(self._radius)\n", " turtle.end_fill()\n", " turtle.hideturtle()\n", " done()\n", "\n", "#creating objects of class circle \n", "c1 = circle(15,7,5,\"blue\") \n", "c2 = circle(41,12,7,\"red\")\n", "c3 = circle(65,18,4,\"green\")\n", "\n", "#draw circle\n", "c1.draw()\n", "c2.draw()\n", "c3.draw()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.7, Page Number: 233" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Distance: #Distance class\n", " \n", " def __init__(self,ft=0,inc=0): #constructor \n", " self.__feet = ft\n", " self.__inches = inc\n", " \n", " def getdist(self): #get length from user\n", " self.__feet = input('Enter feet:')\n", " self.__inches = input('Enter inches:')\n", " \n", " def showdist(self): #display distance\n", " print self.__feet , '\\' -' , self.__inches , '\\\"'\n", " \n", " def add_dist(self,d2,d3): #add length d2 and d3\n", " self.__inches = d2.__inches + d3.__inches #add inches\n", " self.__feet = 0\n", " if self.__inches >= 12.0: #if total exceeds 12.0\n", " self.__inches = self.__inches - 12.0 #then decrease inches by 12.0\n", " self.__feet = self.__feet + 1 #and increase feet by 1\n", " self.__feet = self.__feet + d2.__feet + d3.__feet #add the feet\n", "\n", "#define two length\n", "dist1 = Distance()\n", "dist3 = Distance()\n", "\n", "#define and initialize dist2\n", "dist2 = Distance(11,6.25)\n", "\n", "#get dist1 from user\n", "dist1.getdist()\n", "\n", "#dist3 = dist1 + dist2\n", "dist3.add_dist(dist1,dist2)\n", "\n", "#display all lengths\n", "print 'dist1 = ',\n", "dist1.showdist()\n", "print 'dist2 = ',\n", "dist2.showdist()\n", "print 'dist3 = ',\n", "dist3.showdist()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter feet:17\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter inches:5.75\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "dist1 = 17 ' - 5.75 \"\n", "dist2 = 11 ' - 6.25 \"\n", "dist3 = 29 ' - 0.0 \"\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.8, Page Number: 238" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Distance: #Distance class\n", " \n", " def __init__(self,ft=0,inc=0): #overloaded constructor that takes no arguments or two args or one object(copy constructor)\n", " if isinstance(ft,int):\n", " self.__feet = ft\n", " self.__inches = inc\n", " else:\n", " self.__feet = ft.__feet\n", " self.__inches = ft.__inches\n", " \n", " def getdist(self): #get length from user\n", " self.__feet = input('Enter feet:')\n", " self.__inches = input('Enter inches:')\n", " \n", " def showdist(self): #display distance\n", " print self.__feet , '\\' -' , self.__inches , '\\\"'\n", "\n", "#two argument constructor\n", "dist1 = Distance(11,6.25)\n", "\n", "#one argument(object) constructor explicitly pass\n", "dist2 = Distance(dist1)\n", "\n", "#also one argument(object) constructor implicitly pass\n", "dist3 = dist1\n", "\n", "#display all lengths\n", "print 'dist1 = ',\n", "dist1.showdist()\n", "print 'dist2 = ',\n", "dist2.showdist()\n", "print 'dist3 = ',\n", "dist3.showdist()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "dist1 = 11 ' - 6.25 \"\n", "dist2 = 11 ' - 6.25 \"\n", "dist3 = 11 ' - 6.25 \"\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.9, Page Number: 240" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Distance: #Distance class\n", " \n", " def __init__(self,ft=0,inc=0): #constructor \n", " self.__feet = ft\n", " self.__inches = inc\n", " \n", " def getdist(self): #get length from user\n", " self.__feet = input('Enter feet:')\n", " self.__inches = input('Enter inches:')\n", " \n", " def showdist(self): #display distance\n", " print self.__feet , '\\' -' , self.__inches , '\\\"'\n", " \n", " def add_dist(self,d2): #add this length to d2 and return object\n", " temp = Distance() #temporary object\n", " temp.__inches = self.__inches + d2.__inches\n", " if temp.__inches >= 12.0:\n", " temp.__inches = temp.__inches - 12.0\n", " temp.__feet = 1\n", " temp.__feet = temp.__feet + self.__feet + d2.__feet\n", " return temp #return sum as object\n", "\n", "#define two length\n", "dist1 = Distance()\n", "dist3 = Distance()\n", "\n", "#define and initialize dist2\n", "dist2 = Distance(11,6.25)\n", "\n", "#get dist1 from user\n", "dist1.getdist()\n", "\n", "#dist3 = dist1 + dist2\n", "dist3 = dist1.add_dist(dist2)\n", "\n", "#display all lengths\n", "print 'dist1 = ',\n", "dist1.showdist()\n", "print 'dist2 = ',\n", "dist2.showdist()\n", "print 'dist3 = ',\n", "dist3.showdist()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter feet:17\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter inches:5.75\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "dist1 = 17 ' - 5.75 \"\n", "dist2 = 11 ' - 6.25 \"\n", "dist3 = 29 ' - 0.0 \"\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.10, Page Number: 243" ] }, { "cell_type": "code", "collapsed": false, "input": [ "Suit = [\"clubs\",\"diamonds\",\"hearts\",\"spades\"] \n", "\n", "(clubs,diamonds,hearts,spades) = (0,1,2,3) #Atteching the names with number \n", "\n", "\n", "#from 2 to 10 are integers without names\n", "jack = 11 \n", "queen = 12 \n", "king = 13\n", "ace = 14\n", "\n", "\n", "class card: \n", " \n", " def __init__(self,n=None,s=None): #constructor\n", " self.__number = n #2 to 10, jack, queen, king, ace\n", " self.__suit = s #clubs, diamonds, hearts, spades\n", " \n", " def display(self): #display the cards\n", " \n", " if self.__number >= 2 and self.__number<=10:\n", " print self.__number , 'of',\n", " \n", " else:\n", " if self.__number == jack:\n", " print 'jack of',\n", " elif self.__number == queen:\n", " print 'queen of',\n", " elif self.__number == king:\n", " print 'king of',\n", " else:\n", " print 'ace of',\n", " \n", " if self.__suit == clubs:\n", " print 'clubs'\n", " elif self.__suit == diamonds:\n", " print 'diamonds'\n", " elif self.__suit == hearts:\n", " print 'hearts'\n", " else:\n", " print 'spades'\n", " \n", " def isEqual(self,c2): #return 1 if cards equal\n", " \n", " if self.__number == c2.__number and self.__suit == c2.__suit:\n", " return 1\n", " else:\n", " return 0\n", "\n", "\n", "#define various cards\n", "temp = card()\n", "chosen = card()\n", "prize = card()\n", "\n", "\n", "#define and initialize card1\n", "card1 = card(7,clubs)\n", "print 'card 1 is the',\n", "card1.display() #display card1\n", "\n", "#define and initialize card2\n", "card2 = card(jack,hearts)\n", "print 'card 2 is the',\n", "card2.display() #display card2\n", "\n", "#define and initialize card3\n", "card3 = card(ace,spades)\n", "print 'card 3 is the',\n", "card3.display() #display card3\n", "\n", "\n", "#prize is the card to guess\n", "prize = card3\n", "\n", "\n", "#swapping cards\n", "print 'I\\'m swapping card 1 and card 3'\n", "temp = card3\n", "card3 = card1\n", "card1 = temp\n", "\n", "print 'I\\'m swapping card 2 and card 3'\n", "temp = card2\n", "card3 = card2\n", "card2 = temp\n", "\n", "print 'I\\'m swapping card 1 and card 2'\n", "temp = card2\n", "card2 = card1\n", "card1 = temp\n", "\n", "print 'Now, where (1,2, or 3) is the',\n", "prize.display() #display prize\n", "print '?'\n", "\n", "position = input() #get user's guess of position\n", "\n", "\n", "#set chosen to user's choice \n", "if position == 1:\n", " chosen = card1\n", "elif position == 2:\n", " chosen = card2\n", "else:\n", " chosen = card3\n", "\n", "#is chosen card the prize?\n", "\n", "x=chosen.isEqual(prize)\n", "\n", "if x==1:\n", " print 'That\\'s right! You win!'\n", "else:\n", " print 'Sorry. You lose.'\n", "\n", "print 'You choose the',\n", "\n", "#display chosen card\n", "chosen.display()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "card 1 is the 7 of clubs\n", "card 2 is the jack of hearts\n", "card 3 is the ace of spades\n", "I'm swapping card 1 and card 3\n", "I'm swapping card 2 and card 3\n", "I'm swapping card 1 and card 2\n", "Now, where (1,2, or 3) is the ace of spades\n", "?\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "That's right! You win!\n", "You choose the ace of spades\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.11, Page Number: 249" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class foo: \n", " \n", " __count = 0 #only one data item for all objects\n", " \n", " def __init__(self):\n", " foo.__count = foo.__count + 1 #increment count when object created\n", " \n", " def getcount(self): #returns count\n", " return foo.__count\n", "\n", "#create three objecs\n", "f1 = foo()\n", "f2 = foo()\n", "f3 = foo()\n", "\n", "#Each object displays the same count value\n", "print 'count is', f1.getcount()\n", "print 'count is', f2.getcount()\n", "print 'count is', f3.getcount()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "count is 3\n", "count is 3\n", "count is 3\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.12, Page Number: 253" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Distance: #Distance class\n", " \n", " def __init__(self,ft=0,inc=0.0): #constructor \n", " self.__feet = ft\n", " self.__inches = inc\n", " \n", " def getdist(self): #get length from user\n", " self.__feet = input('Enter feet:')\n", " self.__inches = input('Enter inches:')\n", " \n", " def showdist(self): #display distance\n", " print self.__feet , '\\' -' , self.__inches , '\\\"'\n", "\n", "#There's no const keyword \n", " \n", " def add_dist(self,d2): #add this length to d2 and return object\n", " \n", " temp = Distance()\n", " temp.__inches = self.__inches + d2.__inches\n", " \n", " if temp.__inches >= 12.0:\n", " temp.__inches = temp.__inches - 12.0\n", " temp.__feet = 1\n", " \n", " temp.__feet = temp.__feet + self.__feet + d2.__feet\n", " \n", " return temp #return sum as object\n", " \n", "dist1 = Distance()\n", "dist3 = Distance()\n", "dist2 = Distance(11,6.25)\n", "\n", "dist1.getdist()\n", "\n", "dist3 = dist1.add_dist(dist2)\n", "\n", "print 'dist1 = ',\n", "dist1.showdist()\n", "print 'dist2 = ',\n", "dist2.showdist()\n", "print 'dist3 = ',\n", "dist3.showdist()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter feet:17\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter inches:5.75\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "dist1 = 17 ' - 5.75 \"\n", "dist2 = 11 ' - 6.25 \"\n", "dist3 = 29 ' - 0.0 \"\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.13, Page Number: 255" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Distance:\n", " \n", " def __init__(self,ft,inc):\n", " self.__feet = ft\n", " self.__inches = inc\n", " \n", " def getdist(self):\n", " self.__feet = input('Enter feet:')\n", " self.__inches = input('Enter inches:')\n", " \n", " def showdist(self):\n", " print self.__feet , '\\' -' , self.__inches , '\\\"'\n", "\n", "football = Distance(300,0)\n", "\n", "print 'football = ',\n", "football.showdist()\n", "\n", "#There's no const keyword in python" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "football = 300 ' - 0 \"\n" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }