Python Programming: An Introduction to Computer Science
10.6. EXERCISES 173 10.6 Exercises Explainthesimilaritiesanddifferencesbetweeninstancevariablesand“regular”functionvariables. S ...
174 CHAPTER10. DEFININGCLASSES mouthOff= size / 2.0 self.head= Circle(center, size) self.head.draw(window) self.leftEye= Circle( ...
10.6. EXERCISES 175 RedotheregressionproblemfromChapter8 usingaRegressionclass.Yournewclasswillkeep trackofthevariousquantities ...
176 CHAPTER10. DEFININGCLASSES ...
Chapter 11 Data Collections Asyousaw inthelastchapter, classesareonemechanismforstructuringthedatainourprograms.Classes alone,ho ...
178 CHAPTER11. DATA COLLECTIONS Supposewewanttoextendthisprogramsothatit computesnotonlythemean,butalsothemedianand standard dev ...
11.2. APPLYINGLISTS 179 Whenthey wanttorefertospecificvaluesinthesequence,thesevaluesaredenotedbysubscripts. Inthis example,thef ...
180 CHAPTER11. DATA COLLECTIONS ’l’ myString[2] = ’z’ Traceback (innermostlast): File "", line1, in? TypeError: object doesn’ ...
11.3. STATISTICSWITHLISTS 181 We have seenhow listscangrow byappendingnew items.Listscanalsoshrinkwhenitemsaredeleted. Individua ...
182 CHAPTER11. DATA COLLECTIONS for num in nums: sum = sum + num return sum / len(nums) Noticehow theaverageis computedandreturn ...
11.3. STATISTICSWITHLISTS 183 def median(nums): nums.sort() size = len(nums) midPos = size / 2 if size % 2 == 0: median = (nums[ ...
184 CHAPTER11. DATA COLLECTIONS def stdDev(nums, xbar): sumDevSq = 0.0 for num in nums: dev = num - xbar sumDevSq = sumDevSq+ de ...
11.4. COMBININGLISTSANDCLASSES 185 We wanttoreplacetheselineswithcodetocreatea listofpips.Oneapproachwouldbetostartwithan emptyl ...
186 CHAPTER11. DATA COLLECTIONS for i in [0,3,6]: self.pips[i].setFill(self.foreground) Usinganindex variableina loop,wecanturna ...
11.4. COMBININGLISTSANDCLASSES 187 class DieView: """ DieView is a widgetthat displays a graphical representation of a standard ...
188 CHAPTER11. DATA COLLECTIONS # Turn the appropriatepips back on for i in self.onTable[value]: self.pips[i].setFill(self.foreg ...
11.5. CASESTUDY: PYTHONCALCULATOR 189 def __init__(self): # create the windowfor the calculator win = GraphWin("Calculator") win ...
190 CHAPTER11. DATA COLLECTIONS self.buttons.append(Button(self.win, Point(4.5,1), 1.75,.75, "=")) I couldhave writtena linelike ...
11.5. CASESTUDY: PYTHONCALCULATOR 191 Theclearkey blanksthedisplay. self.display.setText("") Thebackspacestripsoff onecharacter. ...
192 CHAPTER11. DATA COLLECTIONS def __createDisplay(self): bg = Rectangle(Point(.5,5.5), Point(5.5,6.5)) bg.setFill(’white’) bg. ...
«
5
6
7
8
9
10
11
12
13
14
»
Free download pdf