Python Programming in Context

Python Programming in Context
Slide Note
Embed
Share

Delve into the intricacies of Python programming as presented in Chapter 10 of the text. Explore advanced concepts that build upon previous knowledge, providing a deeper understanding of Python's capabilities. This chapter offers practical examples and code snippets to strengthen your proficiency in the language. Gain insights into complex topics such as data structures, algorithms, and more, enhancing your coding skills and problem-solving abilities. Whether you are a novice or experienced programmer, Chapter 10 will empower you to write efficient and effective Python code.

  • Python programming
  • Advanced concepts
  • Data structures
  • Algorithms
  • Code examples

Uploaded on Mar 05, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Python Programming in Context Chapter 10

  2. Objectives To explore classes and objects further To understand how to construct a class To write constructors, accessor methods, and mutator methods To understand the concept of self To explore instance data To implement a graphical simulation using objects

  3. Object Oriented Programming Objects are instances of Classes Objects can perform methods Instance Data What an object knows about itself Methods What an object can do

  4. Turtle objects Instance Data Color Heading Tail position Methods Forward Backward Up

  5. Astronomy Design classes to represent planets, sun, moons, etc. Use these classes to write programs that manipulate the solar system

  6. Planet Class Instance Data Name Mass Distance from sun radius

  7. Listing 10.1 class Planet: def __init__(self, iname, irad, im, idist): self.name = iname self.radius = irad self.mass = im self.distance = idist

  8. Figure 10.1

  9. Types of Methods Constructor Used to make a new instance of the class Accessor Used to get information from an object Get instance data Mutator Used to change something about an object Change instance data

  10. Listing 10.2 def getName(self): return self.name def getRadius(self): return self.radius def getMass(self): return self.mass def getDistance(self): return self.distance

  11. Listing 10.3 def getVolume(self): v = 4.0/3 * math.pi * self.radius**3 return v def getSurfaceArea(self): sa = 4.0 * math.pi * self.radius**2 return sa def getDensity(self): d = self.mass / self.getVolume() return d

  12. Listing 10.4 def setName(self, newname): self.name = newname

  13. Listing 10.5 def show(self): print(self.name)

  14. Listing 10.6 def __str__(self): return self.name

  15. Figure 10.2

  16. Namespaces Namespaces and reference work as they did before self is the name of the implicit parameter that always refers to the object itself Never explicitly pass a value to the implicit parameter

  17. Figure 10.3

  18. Figure 10.4

  19. The Sun Class Instance Data Name Mass Radius Temperature

  20. Listing 10.7 import math class Sun: def __init__(self, iname, irad, im, itemp): self.name = iname self.radius = irad self.mass = im self.temp = itemp def getMass(self): return self.mass def __str__(self): return self.name

  21. Solar System Class A sun Many planets Use a list to keep the collection of planets

  22. Listing 10.8 class SolarSystem: def __init__(self, asun): self.thesun = asun self.planets = [] def addPlanet(self, aplanet): self.planets.append(aplanet) def showPlanets(self): for aplanet in self.planets: print(aplanet)

  23. Figure 10.5

  24. Visualize and Animate Use a turtle to draw the sun and the planets Animate the solar system by moving the turtle Need to develop a simple understanding of planetary movement Velocity Acceleration Mass

  25. Listing 10.9 class SolarSystem: def __init__(self, width, height): self.thesun = None self.planets = [] self.ssturtle = turtle.Turtle() self.ssturtle.hideturtle() self.ssscreen = turtle.Screen() self.ssscreen.setworldcoordinates(-width/2.0,-height/2.0,width/2.0,height/2.0) def addPlanet(self, aplanet): self.planets.append(aplanet) def addSun(self, asun): self.thesun = asun def showPlanets(self): for aplanet in self.planets: print(aplanet) def freeze(self): self.ssscreen.exitonclick()

  26. Listing 10.10 class Sun: def __init__(self, iname, irad, im, itemp): self.name = iname self.radius = irad self.mass = im self.temp = itemp self.x = 0 self.y = 0 self.sturtle = turtle.Turtle() self.sturtle.shape("circle") self.sturtle.color("yellow") #other methods as before def getXPos(self): return self.x def getYPos(self): return self.y

  27. Listing 10.11 class Planet: def __init__(self, iname, irad, im, idist, ic): self.name = iname self.radius = irad self.mass = im self.distance = idist self.x = idist self.y = 0 self.color = ic self.pturtle = turtle.Turtle() self.pturtle.color(self.color) self.pturtle.shape("circle") self.pturtle.up() self.pturtle.goto(self.x,self.y) self.pturtle.down() #other methods as before def getXPos(self): return self.x def getYPos(self): return self.y

  28. Figure 10.6

  29. Figure 10.7

  30. Figure 10.8

  31. Listing 10.12 class Planet: def __init__(self, iname, irad, im, idist, ivx, ivy, ic): #other instance variables as before self.velx = ivx self.vely = ivy

  32. Listing 10.13 def moveTo(self, newx, newy): self.x = newx self.y = newy self.pturtle.goto(newx, newy) def getXVel(self): return self.velx def getYVel(self): return self.vely def setXVel(self, newvx): self.velx = newvx def setYVel(self, newvy): self.vely = newvy

  33. Listing 10.14 def movePlanets(self): G = .1 dt = .001 for p in self.planets: p.moveTo(p.getXPos() + dt * p.getXVel(), p.getYPos() + dt * p.getYVel()) rx = self.thesun.getXPos() - p.getXPos() ry = self.thesun.getYPos() - p.getYPos() r = math.sqrt(rx**2 + ry**2) accx = G * self.thesun.getMass()*rx/r**3 accy = G * self.thesun.getMass()*ry/r**3 p.setXVel(p.getXVel() + dt * accx) p.setYVel(p.getYVel() + dt * accy)

  34. Listing 10.15 def createSSandAnimate(): ss = SolarSystem(2,2) sun = Sun("SUN", 5000, 10, 5800) ss.addSun(sun) m = Planet("MERCURY", 19.5, 1000, .25, 0, 2, "blue") ss.addPlanet(m) m = Planet("EARTH", 47.5, 5000, 0.3, 0, 2.0, "green") ss.addPlanet(m) m = Planet("MARS", 50, 9000, 0.5, 0, 1.63, "red") ss.addPlanet(m) m = Planet("JUPITER", 100, 49000, 0.7, 0, 1, "black") ss.addPlanet(m) numTimePeriods = 2000 for amove in range(numTimePeriods): ss.movePlanets() ss.freeze() createSSandAnimate()

  35. Figure 10.9

More Related Content