2026-03-23_this-is-python.html
001|
002|
003|
004|
005| from examples import Point
006|
007| p1 = Point(0.5, 0.25, 0.125)
008| p2 = Point(1,1,1)
009|
010| print(p1)
011| print(p2)
012| print()
013|
014| print('Point Math:')
015| print(f'{p1 + p2 = !s}')
016| print(f'{p1 - p2 = !s}')
017| print(f'{p1 * p2 = !s}')
018| print(f'{p1 / p2 = !s}\n')
019|
020| print('Scalar Math:')
021| print(f'{p1 + 1.0 = !s}')
022| print(f'{p2 - 0.5 = !s}')
023| print(f'{p1 * 2.0 = !s}')
024| print(f'{p1 / 0.5 = !s}\n')
025|
026| print(f'{1.0 + p1 = !s}')
027| print(f'{0.5 - p2 = !s}')
028| print(f'{2.0 * p1 = !s}')
029| print(f'{0.5 / p1 = !s}\n')
030|
031| print('Inplace Point Math:')
032| p0 = Point()
033| print(f'{p0=!s}')
034|
035| p0 += p1
036| print(f'p0 += p1 ⇒ {p0=!s}')
037| p0 -= p2
038| print(f'p0 -= p2 ⇒ {p0=!s}')
039| p0 *= p1
040| print(f'p0 *= p1 ⇒ {p0=!s}')
041| p0 /= p1
042| print(f'p0 /= p1 ⇒ {p0=!s}\n')
043|
044| print('Inplace Scalar Math:')
045| p0 = Point()
046| print(f'{p0=!s}')
047|
048| p0 += 0.50
049| print(f'p0 += 0.50 ⇒ {p0=!s}')
050| p0 -= 0.25
051| print(f'p0 -= 0.25 ⇒ {p0=!s}')
052| p0 *= 3
053| print(f'p0 *= 3 ⇒ {p0=!s}')
054| p0 /= 3
055| print(f'p0 /= 3 ⇒ {p0=!s}\n')
056|
057|
058| """part of examples.py"""
059|
060| class Point:
061| '''A 3D XYZ Point class.'''
062|
063| ...
064|
065| def __eq__ (self, other):
066| '''Test self & other for equality.'''
067|
068|
069| if not isinstance(other, type(self)):
070| raise ValueError(f"Can't compare a {typename(other)}!")
071|
072|
073| if self.x != other.x: return False
074| if self.y != other.y: return False
075| if self.z != other.z: return False
076|
077|
078| return True
079|
080| def __ne__ (self, other):
081| '''Test self & other for inequality.'''
082| return not self.__eq__(other)
083|
084| ...
085|
086|
087| from examples import Point
088|
089| p1 = Point(1,2,3)
090| p2 = Point(1,2,3)
091|
092| print(f'{p1} @{id(p1):012x}')
093| print(f'{p2} @{id(p2):012x}')
094| print()
095|
096| print(f'{p1 == p2 = }')
097| print(f'{p1 != p2 = }')
098| print()
099|
100|
101| p2.y = 4
102|
103| print(f'{p1 == p2 = }')
104| print(f'{p1 != p2 = }')
105| print()
106|
107|
108| print(f'{p1 is p1 = }')
109| print(f'{p1 is p2 = }')
110| print()
111|
112| print(f'{p1 is not p1 = }')
113| print(f'{p1 is not p2 = }')
114| print()
115|
116|
117| from examples import Point
118|
119| p1 = Point(1,2,3)
120| p2 = p1
121|
122| def pass_me_that_point (p):
123| print(f'function: {p is p1 = } (same object)')
124|
125|
126| print(f'{p1} @{id(p1):012x}')
127| print(f'{p2} @{id(p2):012x}')
128| print()
129|
130| print(f'{p1 == p2 = } (of course)')
131| print(f'{p1 is p2 = } (same object)')
132| print()
133|
134| pass_me_that_point(p1)
135| pass_me_that_point(p2)
136|
137|
138|
139| from examples import Point
140|
141| def shift_point (p):
142| """Alter point's location."""
143|
144| p.x += 2
145| p.y += 3
146| p.z += 4
147|
148|
149|
150| pt = Point(1, 1, 1)
151| print(pt)
152|
153|
154| shift_point(pt)
155| print(pt)
156|
157|
158| """part of examples.py"""
159| import math
160|
161| class Point:
162| '''A 3D XYZ Point class.'''
163|
164| ...
165|
166| @property
167| def magnitude (self):
168| '''Pythagorean distance from origin.'''
169| x2 = pow(self.x, 2)
170| y2 = pow(self.y, 2)
171| z2 = pow(self.z, 2)
172| return math.sqrt(x2 + y2 + z2)
173|
174| @property
175| def normalized (self):
176| '''Return same vector with length one.'''
177| if not self:
178| raise ValueError("Can't normalize a null vector!")
179|
180|
181| m = self.magnitude
182| x = self.x / m
183| y = self.y / m
184| z = self.z / m
185| return Point(x, y, z)
186|
187| ...
188|
189|
190| from examples import Point
191|
192| p0 = Point()
193| p1 = Point(1,1,1)
194| p2 = Point(2.1, 4.2, 6.3)
195|
196| print(f'{p0.magnitude = }')
197| print(f'{p1.magnitude = }')
198| print(f'{p2.magnitude = }')
199| print()
200|
201| try:
202| print('Trying to change magnitude:')
203| p1.magnitude = 4
204|
205| except Exception as e:
206| print(f'OOPS: {type(e).__name__}')
207| print(e)
208|
209|
210| from examples import Point
211|
212| p0 = Point()
213| p1 = Point(1,1,1)
214| p2 = Point(2.1, 4.2, 6.3)
215|
216| print(f'{p0.magnitude = }')
217| print(f'{p1.magnitude = }')
218| print(f'{p2.magnitude = }')
219| print()
220|
221| p1n = p1.normalized
222| p2n = p2.normalized
223| print(f'{p1n = !s}, {p1n.magnitude = }')
224| print(f'{p2n = !s}, {p2n.magnitude = }')
225| print()
226|
227| try:
228| p0n = p0.normalized
229| print(f'{p0n = !s}, {p0n.magnitude = }')
230|
231| except Exception as e:
232| print(f'OOPS: {type(e).__name__}')
233| print(e)
234|
235|
236| """part of examples.py"""
237| import math
238|
239| class Point:
240| '''A 3D XYZ Point class.'''
241|
242| ...
243|
244| def distance (self, other):
245| '''Return the distance between self and other.'''
246| x2 = pow(self.x - other.x, 2)
247| y2 = pow(self.y - other.y, 2)
248| z2 = pow(self.z - other.z, 2)
249| return math.sqrt(x2 + y2 + z2)
250|
251| def dot (self, other):
252| '''Return the dot product between self and other.'''
253| xx = self.x * other.x
254| yy = self.y * other.y
255| zz = self.z * other.z
256| return xx + yy + zz
257|
258| ...
259|
260|
261| from examples import Point
262|
263| p1 = Point(1,1,1)
264| p2 = Point(2.1, 4.2, 6.3)
265| print(p1)
266| print(p2)
267| print()
268|
269| print(f'{p1.distance(p2) = }')
270| print(f'{p2.distance(p1) = }')
271| print(f'{p2.distance(p2) = }')
272| print()
273|
274| print(f'{p1.dot(p1) = }')
275| print(f'{p2.dot(p2) = }')
276| print()
277| print(f'{p1.dot(p2) = }')
278| print(f'{p2.dot(p1) = }')
279| print()
280|
281| ux = Point(x=1)
282| uy = Point(y=1)
283| uz = Point(z=1)
284|
285| print(f'{ux.dot(p1) = }')
286| print(f'{uy.dot(p1) = }')
287| print(f'{uz.dot(p1) = }')
288| print()
289|
290| print(f'{ux.dot(p2) = }')
291| print(f'{uy.dot(p2) = }')
292| print(f'{uz.dot(p2) = }')
293| print()
294|
295|
296| """part of examples.py"""
297|
298| class Point:
299| '''A 3D XYZ Point class.'''
300|
301| ...
302|
303| def reset (self):
304| '''Reset point to default value [0,0,0].'''
305| self.x = 0.0
306| self.y = 0.0
307| self.z = 0.0
308| return self
309|
310| def move (self, x=None, y=None, z=None):
311| '''Change a point's coordinates.'''
312| if x is not None: self.x = float(x)
313| if y is not None: self.y = float(y)
314| if z is not None: self.z = float(z)
315| return self
316|
317| ...
318|
319|
320| from examples import Point
321|
322| pt = Point(2.1, 4.2, 6.3)
323|
324|
325| pt.move(x=2.7, y=3.1, z=5.5)
326| print(f'after move: {pt}')
327|
328| pt.reset()
329| print(f'after reset: {pt}')
330|
331| pt.move(y=4.2)
332| print(f'after move: {pt}')
333|
334| pt.move(z=6.3, y=3.14159)
335| print(f'after move: {pt}')
336|
337| pt.move(2.1)
338| print(f'after move: {pt}')
339|
340| pt.move(1.0, 2.0, 3.0)
341| print(f'after move: {pt}')
342| print()
343|
344|
345| """part of examples.py"""
346| import math
347|
348| class Point:
349| '''A 3D XYZ Point class.'''
350|
351| ...
352|
353| @property
354| def polar (self):
355| '''Return a polar coordinate tuple.'''
356| radius = self.magnitude
357| theta = math.atan2(self.y, self.x)
358| phi = math.atan2(self.z, math.sqrt(pow(self.x,2)+pow(self.y,2)))
359| return (radius, theta, phi)
360|
361|
362|
363| @classmethod
364| def from_polar (cls, radius=1.0, theta=0.0, phi=0.0):
365| '''Create an XYZ Point from polar coordinates.'''
366| x = math.cos(theta) * radius * math.cos(phi)
367| y = math.sin(theta) * radius * math.cos(phi)
368| z = math.sin(phi) * radius
369| return cls(x, y, z)
370|
371| @classmethod
372| def xaxis (cls): return cls(1,0,0)
373|
374| @classmethod
375| def yaxis (cls): return cls(0,1,0)
376|
377| @classmethod
378| def zaxis (cls): return cls(0,0,1)
379|
380| ...
381|
382|
383| import math
384| from examples import Point
385|
386| p0 = Point()
387| p1 = Point(1,1,1)
388| p2 = Point(2.1, 4.2, 6.3)
389|
390| print(f'{p0 = }')
391| print(f'{p1 = }')
392| print(f'{p2 = }')
393| print()
394|
395|
396| print(f'{p0.polar = }')
397| print(f'{p1.polar = }')
398| print(f'{p2.polar = }')
399| print()
400|
401|
402| a90 = math.radians(90)
403| print(f'{Point.from_polar() = !s}')
404| print(f'{Point.from_polar(2.0, a90, a90) = !s}')
405| print(f'{Point.from_polar(theta=a90) = !s}')
406| print(f'{Point.from_polar(theta=a90*2) = !s}')
407| print(f'{Point.from_polar(theta=a90*3) = !s}')
408| print(f'{Point.from_polar(phi=a90) = !s}')
409| print(f'{Point.from_polar(phi=a90*3) = !s}')
410| print()
411|
412|
413| ux = Point.xaxis()
414| uy = Point.yaxis()
415| uz = Point.zaxis()
416|
417| print(f'{ux = }')
418| print(f'{uy = }')
419| print(f'{uz = }')
420| print()
421|
422| print(f'{ux.polar = }')
423| print(f'{uy.polar = }')
424| print(f'{uz.polar = }')
425| print()
426|
427|
428|
429|
430| class Numbers (list):
431| """Implement a list of numbers."""
432|
433| def __init__ (self, last_number):
434| """Create new number list."""
435|
436|
437| nums = range(1, last_number+1)
438|
439|
440| super().__init__(nums)
441|
442| def __str__ (self):
443| """String version."""
444| ns = ','.join(str(n) for n in self)
445| return f'[{ns}]'
446|
447| def __repr__ (self):
448| """Represent."""
449| return f'{type(self).__name__}({self[-1]})'
450|
451|
452| ns = Numbers(12)
453| print(ns)
454| print(repr(ns))
455| print()
456|
457| print(f'{Numbers(0) = !s}')
458| print(f'{Numbers(1) = !s}')
459| print(f'{Numbers(7) = !s}')
460| print(f'{Numbers(18) = !s}')
461| print()
462|
463|
464|