A Vector Class with Operator Overloading

easy
Unit 10 · Collections0/3 to milestone

Implement an immutable 2-D Vector(x, y) supporting:

  • v + w, v - w, v * k and k * v (scalar), -v
  • abs(v) (length) and v @ w (dot product)
  • == with another Vector, and hashing (so vectors work in sets)
  • repr(v) returning Vector(1, 2)
  • iteration, so x, y = v works

Return NotImplemented from operators when the other operand has the wrong type.

#classes#dunder-methods2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
A Vector Class with Operator Overloading · TechGradify