Python Classes


class Point:
          pass

p1=Point()
p2=Point()

p1.x = 5
p1.y = 4

p2.x = 3
p2.y = 6

a = p1.x + p2.y
b = p1.y + p2.x 

print('x =',p1.x,'y = ',p1.y)
print('x1 =',p2.x,'y1 = ',p2.y)

out:
>>>
x = 5 y = 4
x1 = 3 y1 = 6


-----------------

class Point:
      def reset(self):
             self.x = 0
             self.y = 0
p = Point()
p.reset()
print(p.x, p.y)

out:
>>>
0  0

----------------
import math
class Point:
      def move(self, x, y):
             self.x = x
             self.y = y
      def reset(self):
            self.move(0,0)
      def calculate_distance(self, other_point):
            return math.sqrt(
                  (self.x - other_point.x)**2 +
                  (self.y - other_point.y)**2)

# how to use it:

point1 = Point()
point2 = Point()
point1.reset()
point2.move(5,0)
print(point2.calculate_distance(point1))
assert (point2.calculate_distance(point1) ==
           point1.calculate_distance(point2))
point1.move(3,4)
print(point1.calculate_distance(point2))
print(point1.calculate_distance(point1))

output:

5.0
4.472135955
0.0

----------------------
class Point:
      def __init__(self, x, y):
             self.move(x, y)
      def move(self, x, y):
            self.x = x
            self.y = y
      def reset(self):
            self.move(0,0)

# Constructing a Point
point = Point(3, 5)
print(point.x, point.y)

-----------------------

import math
class Point:
       'Represents a point in two-dimensional geometric coordinates'
       def __init__(self, x=0, y=0):
             '''Initialize the position of a new point. The x and y
             coordinates can be specified. If they are not, the point defaults to the origin.'''
             self.move(x, y)
      def move(self, x, y):
            "Move the point to a new location in two-dimensional space."
            self.x = x
            self.y = y
      def reset(self):
            'Reset the point back to the geometric origin: 0, 0'
            self.move(0, 0)
      def calculate_distance(self, other_point):
            """Calculate the distance from this point to a second point  passed as a parameter.
            This function uses the Pythagorean Theorem to calculate the distance between the two points.                               distance is returned as a float."""
        return math.sqrt(
              (self.x - other_point.x)**2 +
              (self.y – other_point.y)**2)






SHARE

About Lasha Gogua

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment