0
0
Pythonprogramming~10 mins

Arithmetic operator overloading in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to overload the addition operator for the class.

Python
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)
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different operator like '-' or '*' instead of '+'
Forgetting to access other.value
Returning a raw number instead of a new Number instance
2fill in blank
medium

Complete the code to overload the multiplication operator for the class.

Python
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)
Drag options to blanks, or click blank then click option'
A/
B-
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '-' instead of '*'
Not returning a new Number instance
Accessing other directly instead of other.value
3fill in blank
hard

Fix the error in the code to correctly overload the subtraction operator.

Python
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)
Drag options to blanks, or click blank then click option'
A+
B/
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-'
Returning a raw number instead of a Number instance
Not accessing other.value
4fill in blank
hard

Fill both blanks to overload the true division operator and return a float value.

Python
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)
Drag options to blanks, or click blank then click option'
A/
B*
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' or '+' instead of '/'
Using integer division operator '//' instead of '/'
Not returning a new Number instance
5fill in blank
hard

Fill all three blanks to overload addition, subtraction, and multiplication operators correctly.

Python
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)
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up operators between methods
Using division '/' in any of these methods
Not returning a new Number instance