Python Programming: An Introduction to Computer Science
4.5.FILEPROCESSING 53 Youcanthinkofa textfileasa (possiblylong)stringthathappenstobestoredondisk.Ofcourse,a typicalfilegenerally ...
54 CHAPTER4. COMPUTINGWITHSTRINGS terminology, thefileis openedforreadingandthethecontentsofthefilearethenreadintomemoryviafile ...
4.5.FILEPROCESSING 55 infile = open(someFile,’r’) for i in range(5): line = infile.readline() print line[:-1] Noticetheuseofslic ...
56 CHAPTER4. COMPUTINGWITHSTRINGS userfile.py Program to createa file of usernames in batch mode. import string def main(): prin ...
4.6.EXERCISES 57 Forcompleteness,I shouldmentionthatstringsarealsoobjectsinPython.If youhave a relativelynew versionofPython(2.0 ...
58 CHAPTER4. COMPUTINGWITHSTRINGS (d)msg = "" for s in string.split("secret","e"): msg = msg + s print msg (e)msg = "" for ch in ...
4.6.EXERCISES 59 Oneproblemwiththepreviousexerciseis thatit doesnotdealwiththecasewhenwe“dropoff the end”ofthealphabet(orASCIIe ...
60 CHAPTER4. COMPUTINGWITHSTRINGS ...
Chapter 5 Objects and Graphics Sofarwehave beenwritingprogramsthatusethebuilt-inPythondatatypesfornumbersandstrings.We sawthatea ...
62 CHAPTER5. OBJECTSANDGRAPHICS 5.2 GraphicsProgramming Moderncomputerapplicationsarenotlimitedtothesortoftextualinputandoutputt ...
5.2.GRAPHICSPROGRAMMING 63 Figure5.1:Screenshotwitha Pythonwindow anda GraphWin Alloftherestofthegraphicsexampleswillassumethatt ...
64 CHAPTER5. OBJECTSANDGRAPHICS Figure5.2:Graphicswindow withtwo pointsdrawn. polygonsandtext.Eachoftheseobjectsis createdanddra ...
5.3.USINGGRAPHICALOBJECTS 65 Figure5.3:Variousshapesfromthegraphicsmodule. BecauseFidois aninstanceofthisclass,weexpectcertainth ...
66 CHAPTER5. OBJECTSANDGRAPHICS x: 60 50 p: y: Point Figure5.4:Conceptualpictureoftheresultofp = Point(50,60). Thevariableprefer ...
5.3.USINGGRAPHICALOBJECTS 67 Circle y: x: 100 100 center: Point radius: draw( ) . . . GraphWin . . . Low-level drawing commands ...
68 CHAPTER5. OBJECTSANDGRAPHICS leftEye.setOutline(’red’) rightEye = Circle(Point(100,50), 5) rightEye.setFill(’yellow’) rightEy ...
5.4.GRAPHINGFUTUREVALUE 69 Figure5.7:Bargraphshowinggrowthof$2,000at 10%interest Print an introduction Get value of principaland ...
70 CHAPTER5. OBJECTSANDGRAPHICS objects.WhencreatingText, wespecifytheanchorpoint(thepointthetextis centeredon)andthestringto us ...
5.4.GRAPHINGFUTUREVALUE 71 0.0K 2.5K 5.0K 7.5K 10.0K (40,230) 320 (0,0) (319,239) 25 10 40 50 (315,230) 240 Figure5.8:Positionof ...
72 CHAPTER5. OBJECTSANDGRAPHICS # Create a graphicswindow with labels on left edge win = GraphWin("Investment Growth Chart", 320 ...
«
1
2
3
4
5
6
7
8
9
10
»
Free download pdf