MCPcopy Create free account
hub / github.com/CodeWithHarry/The-Ultimate-Python-Course / Vector

Class Vector

Chapter 11 - PS/06_problem6.py:1–16  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Vector:
2 def __init__(self, x, y, z):
3 self.x = x
4 self.y = y
5 self.z = z
6
7 def __add__(self, other):
8 result = Vector(self.x + other.x, self.y + other.y, self.z + other.z)
9 return result
10
11 def __mul__(self, other):
12 result = self.x * other.x + self.y * other.y + self.z * other.z
13 return result
14
15 def __str__(self):
16 return f"{self.x}i + {self.y}j + {self.z}k"
17
18# Test the implementation
19v1 = Vector(1, 2, 3)

Callers 2

__add__Method · 0.70
06_problem6.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected