2026-03-09_this-is-python.html


001| #:name::This is Python! (part 8)
002| #:link::https://thehardcorecoder.com/2026/03/09/this-is-python-part-8/
003| #:date::2026-03-09
004| #:code::
005| # A 3D XYZ Point data structure.
006| 
007| # Using a list...
008| pt = [2.1, 4.2, 6.3]
009| 
010| print(f'point {pt}')
011| print(f'. x = {pt[0]}')
012| print(f'. y = {pt[1]}')
013| print(f'. z = {pt[2]}')
014| print()
015| 
016| # Change the values...
017| pt[0] = 2.7 # x
018| pt[1] = 3.1 # y
019| pt[2] = 5.5 # z
020| 
021| print(f'point {pt}')
022| print(f'. x = {pt[0]}')
023| print(f'. y = {pt[1]}')
024| print(f'. z = {pt[2]}')
025| print()
026| 
027| # Using a tuple...
028| pt = (2.1, 4.2, 6.3)
029| 
030| print(f'point {pt}')
031| print(f'. x = {pt[0]}')
032| print(f'. y = {pt[1]}')
033| print(f'. z = {pt[2]}')
034| print()
035| 
036| #:code::
037| # A dictionary 3D XYZ Point data structure.
038| 
039| # Using the dict constructor...
040| pt = dict(x=2.1, y=4.2, z=6.3)
041| 
042| # Using a dictionary literal...
043| pt = {"x":2.1, "y":4.2, "z":6.3}
044| 
045| print(f'point {pt}')
046| print(f'. x = {pt["x"]}')
047| print(f'. y = {pt["y"]}')
048| print(f'. z = {pt["z"]}')
049| print()
050| 
051| # Change the values...
052| pt["x"] = 2.7 # x
053| pt["y"] = 3.1 # y
054| pt["z"] = 5.5 # z
055| 
056| print(f'point {pt}')
057| print(f'. x = {pt["x"]}')
058| print(f'. y = {pt["y"]}')
059| print(f'. z = {pt["z"]}')
060| print()
061| 
062| #:code::
063| # Defining new data types (classes)...
064| 
065| class Point:
066|     '''A 3D XYZ Point data structure.'''
067| 
068|     def __init__ (self):
069|         '''New instance.'''
070|         self.x = 0.0
071|         self.y = 0.0
072|         self.z = 0.0
073| 
074| 
075| # Create a Point object...
076| pt = Point()
077| pt.x = 2.1
078| pt.y = 4.2
079| pt.z = 6.3
080| 
081| # Print point values...
082| print(f'point {pt}')
083| print(f'. x={pt.x}')
084| print(f'. y={pt.y}')
085| print(f'. z={pt.z}')
086| print()
087| 
088| # Change the values...
089| pt.x = 2.7
090| pt.y = 3.1
091| pt.z = 5.5
092| 
093| print(f'point {pt}')
094| print(f'. x={pt.x}')
095| print(f'. y={pt.y}')
096| print(f'. z={pt.z}')
097| print()
098| 
099| #:code::
100| # Defining new data types (classes)...
101| 
102| class Point:
103|     '''A 3D XYZ Point data structure.'''
104| 
105|     def __init__ (self, x=0.0, y=0.0, z=0.0):
106|         '''New instance.'''
107|         self.x = x
108|         self.y = y
109|         self.z = z
110| 
111| 
112| # Create a Point object...
113| pt = Point(2.1, 4.2, 6.3)
114| 
115| # Print point values...
116| print(f'point {pt}')
117| print(f'. x={pt.x}')
118| print(f'. y={pt.y}')
119| print(f'. z={pt.z}')
120| print()
121| 
122| # Change the values...
123| pt.x = 2.7
124| pt.y = 3.1
125| pt.z = 5.5
126| 
127| print(f'point {pt}')
128| print(f'. x={pt.x}')
129| print(f'. y={pt.y}')
130| print(f'. z={pt.z}')
131| print()
132| 
133| #:code::
134| # Defining new data types (classes)...
135| 
136| class Point:
137|     '''A 3D XYZ Point class.'''
138| 
139|     def __init__ (self, x=0.0, y=0.0, z=0.0):
140|         '''New instance.'''
141|         self.x = x
142|         self.y = y
143|         self.z = z
144| 
145|     def __repr__ (self):
146|         '''Return a debug string.'''
147|         return f'<{type(self).__name__} @{id(self):012x}>'
148| 
149|     def __str__ (self):
150|         '''Return a string version.'''
151|         return f'[{self.x:.3f}, {self.y:.3f}, {self.z:.3f}]'
152| 
153| if __name__ == '__main__':
154|     print()
155| 
156|     # Create a Point object...
157|     pt = Point(2.1, 4.2, 6.3)
158|     print(pt)
159|     print()
160| 
161|     # Print point values...
162|     print(f'point {str(pt)}')
163|     print(f'point {repr(pt)}')
164|     print()
165|     print(f'point {pt!s} {pt!r}')
166|     print(f'. x={pt.x}')
167|     print(f'. y={pt.y}')
168|     print(f'. z={pt.z}')
169|     print()
170| 
171|     # Change the values...
172|     pt.x = 2.7
173|     pt.y = 3.1
174|     pt.z = 5.5
175| 
176|     print(f'point {pt}')
177|     print(f'point {pt=}')
178|     print()
179|     print(f'point {pt!r}')
180|     print(f'point {pt=!s}')
181|     print()
182| 
183| #:code::
184| # Defining new data types (classes)...
185| 
186| class Point:
187|     '''A 3D XYZ Point class.'''
188| 
189|     def __init__ (self, x=0.0, y=0.0, z=0.0):
190|         '''New instance.'''
191|         self.x = x
192|         self.y = y
193|         self.z = z
194| 
195|     def __str__ (self):
196|         '''Return a string version.'''
197|         x = self.x
198|         y = self.y
199|         z = self.z
200|         s = f'[{x:.3f}, {y:.3f}, {z:.3f}]'
201|         return s
202| 
203|     def __repr__ (self):
204|         '''Return a debug string.'''
205|         classname = type(self).__name__
206|         object_id = id(self)
207|         s = f'<{classname} @{object_id:012x}>'
208|         return s
209| 
210| #:code::
211| # Defining new data types (classes)...
212| 
213| class Point:
214|     '''A 3D XYZ Point class.'''
215| 
216|     def __init__ (self, x=0.0, y=0.0, z=0.0):
217|         '''New instance.'''
218|         self.x = x
219|         self.y = y
220|         self.z = z
221| 
222|     def __str__ (self):
223|         '''Return a string version.'''
224|         return f'[{self.x:.3f}, {self.y:.3f}, {self.z:.3f}]'
225| 
226|     def __repr__ (self):
227|         '''Return a debug string.'''
228|         classname = type(self).__name__
229|         return f'{classname}(x={self.x}, y={self.y}, z={self.z})'
230| 
231| 
232| if __name__ == '__main__':
233|     print()
234| 
235|     # Create new Point object...
236|     pt = Point(2.12, 4.21, 6.35)
237|     print(pt)
238|     print(id(pt))
239|     print()
240| 
241|     # Get string representation...
242|     pt_string = repr(pt)
243|     print(pt_string)
244|     print()
245| 
246|     # Create duplicate Point object...
247|     pt2 = eval(pt_string)
248|     print(pt2)
249|     print(id(pt2))
250|     print()
251| 
252| #:code::
253| # Defining new data types (classes)...
254| 
255| class Point:
256|     '''A 3D XYZ Point class.'''
257| 
258|     def __init__ (self, x=0.0, y=0.0, z=0.0):
259|         '''New instance.'''
260|         self.x = x
261|         self.y = y
262|         self.z = z
263| 
264|     def __str__ (self):
265|         '''Return a string version.'''
266|         return f'[{self.x:.3f}, {self.y:.3f}, {self.z:.3f}]'
267| 
268|     def __repr__ (self):
269|         '''Return a debug string.'''
270|         classname = type(self).__name__
271|         attrs = self.__dict__
272|         values = [f'"{name}":{attrs[name]}' for name in attrs]
273|         return f'{ {classname}: { {", ".join(values)}} } '
274| 
275| 
276| if __name__ == '__main__':
277|     print()
278| 
279|     pt = Point(2.12, 4.21, 6.35)
280|     print(pt)
281|     print(repr(pt))
282|     print()
283| 
284| #:code::
285| # Defining new data types (classes)...
286| 
287| class Point:
288|     '''A 3D XYZ Point class.'''
289| 
290|     def __init__ (self, x=0.0, y=0.0, z=0.0):
291|         '''New instance.'''
292|         self.x = x
293|         self.y = y
294|         self.z = z
295| 
296|     def __repr__ (self):
297|         '''Return a representation string.'''
298|         return f'[{self.x}, {self.y}, {self.z}]'
299| 
300|     def __bool__ (self):
301|         '''Return True or False.'''
302|         return any(self)
303| 
304|     def __iter__ (self):
305|         '''Return an iterator over the elements.'''
306|         return iter([self.x, self.y, self.z])
307| 
308| 
309| if __name__ == '__main__':
310|     print()
311| 
312|     pt0 = Point()
313|     pt1 = Point(2.12, 4.21, 6.35)
314|     print(pt0)
315|     print(pt1)
316|     print()
317| 
318|     print(f'pt0 is {bool(pt0)}')
319|     print(f'pt1 is {bool(pt1)}')
320|     print()
321| 
322|     if pt0: print('True! (0)')
323|     if pt1: print('True! (1)')
324|     print()
325| 
326|     for element in pt1:
327|         print(element)
328|     print()
329| 
330|     print(f'pt0 values: {list(pt0)}')
331|     print(f'pt1 values: {list(pt1)}')
332|     print()
333| 
334|     print(f'pt1 min: {min(pt1)}')
335|     print(f'pt1 max: {max(pt1)}')
336|     print(f'pt1 sum: {sum(pt1)}')
337|     print()
338| 
339| #:code::
340| # Defining new data types (classes)...
341| 
342| class Point:
343|     '''3D X-Y-Z point.'''
344| 
345|     def __init__ (self, x=0.0, y=0.0, z=0.0):
346|         '''New Point instance.'''
347|         self.x = x
348|         self.y = y
349|         self.z = z
350| 
351|     def __repr__ (self):
352|         '''Return a representation string.'''
353|         return f'[{self.x:.2f}, {self.y:.2f}, {self.z:.2f}]'
354| 
355|     def __add__ (self, other):
356|         '''Add two points, return sum in new Point.'''
357|         x = self.x + other.x
358|         y = self.y + other.y
359|         z = self.z + other.z
360|         return Point(x, y, z)
361| 
362|     def __iadd__ (self, other):
363|         '''Add another point to self; return self.'''
364|         self.x += other.x
365|         self.y += other.y
366|         self.z += other.z
367|         return self
368| 
369|     def __radd__ (self, other):
370|         '''Add scalar number to self; return new Point.'''
371|         if isinstance(other,int) or isinstance(other,float):
372|             x = self.x + other
373|             y = self.y + other
374|             z = self.z + other
375|             return Point(x, y, z)
376|         raise ValueError(f"Can't to add a {type(other).__name__}!")
377| 
378| 
379| if __name__ == '__main__':
380|     print()
381| 
382|     pt1 = Point()
383|     pt2 = Point(1.3, 0.5, 2.1)
384|     pt3 = Point(2.1, 4.2, 3.6)
385|     print(f'{pt1 = }')
386|     print(f'{pt2 = }')
387|     print(f'{pt3 = }')
388|     print()
389| 
390|     print(f'{pt2 + pt3 = }') # invokes __add__
391|     print()
392| 
393|     print(f'{3.333 + pt1 = }') # invokes __radd__
394|     print()
395| 
396|     pt1 += pt2 # invokes __iadd__
397|     pt1 += pt2
398|     print(f'{pt1 = }')
399|     print()
400| 
401|     print(f'{pt1+pt2+pt3 = }')
402|     print()
403| 
404| #:end:code::
405|