Complete the code to overload the addition operator for the class.
class Number: def __init__(self, value): self.value = value def __add__(self, other): return Number(self.value [1] other.value) num1 = Number(5) num2 = Number(3) result = num1 + num2 print(result.value)
other.valueThe __add__ method defines how the + operator works for the class. Using + inside returns the sum of the two values.
Complete the code to overload the multiplication operator for the class.
class Number: def __init__(self, value): self.value = value def __mul__(self, other): return Number(self.value [1] other.value) num1 = Number(4) num2 = Number(6) result = num1 * num2 print(result.value)
other directly instead of other.valueThe __mul__ method defines how the * operator works for the class. Using * inside returns the product of the two values.
Fix the error in the code to correctly overload the subtraction operator.
class Number: def __init__(self, value): self.value = value def __sub__(self, other): return Number(self.value [1] other.value) num1 = Number(10) num2 = Number(4) result = num1 - num2 print(result.value)
other.valueThe __sub__ method defines how the - operator works for the class. Using - inside returns the difference of the two values.
Fill both blanks to overload the true division operator and return a float value.
class Number: def __init__(self, value): self.value = value def __truediv__(self, other): return Number(self.value [1] other.value) num1 = Number(9) num2 = Number(2) result = num1 [2] num2 print(result.value)
The __truediv__ method defines how the / operator works for the class. Both blanks should use the division operator to perform true division.
Fill all three blanks to overload addition, subtraction, and multiplication operators correctly.
class Number: def __init__(self, value): self.value = value def __add__(self, other): return Number(self.value [1] other.value) def __sub__(self, other): return Number(self.value [2] other.value) def __mul__(self, other): return Number(self.value [3] other.value) num1 = Number(7) num2 = Number(3) print((num1 + num2).value) print((num1 - num2).value) print((num1 * num2).value)
Each method overloads a different operator: __add__ uses +, __sub__ uses -, and __mul__ uses * to perform the correct arithmetic operations.