How to create a 2D point data structure with two floating-point numbers in Python

3 Answers

0 votes
from collections import namedtuple

Point = namedtuple("Point", "x y")

p = Point(x=9.23, y=7.18)

print(p[0], p[1])
print(p.x, p.y)



'''
run:

9.23 7.18
9.23 7.18

'''

 



answered Dec 27, 2022 by avibootz
0 votes
class Point:
  def __init__(self, _x, _y):
    self.x= _x
    self.y = _y
   
  def PrintPoint(self):
    print(self.x, self.y)
    
p = Point(5.63, 7.42)
p.PrintPoint()



'''
run:

5.63 7.42

'''

 



answered Dec 27, 2022 by avibootz
0 votes
from dataclasses import dataclass

@dataclass
class Point:
    # Constructor
    x: float
    y: float



p = Point(x=3.42, y=4.91)
print(p.x, p.y)



'''
run:

3.42 4.91

'''

 



answered Dec 27, 2022 by avibootz
...