ScanSkill

Add, Subtract, Multiply, and Divide using Class in Python

In python, you can add, subtract, multiply and divide two numbers using the arithmetic operator +, -, *, and /respectively. Every operator takes two operands and returns the result of the operation.

The number in python could be of any data type like int, float, or complex. To find the result of the different operations performed, you can take the same data type or combination of any of the supported numeric data types: int, float, or complex.

Here are the examples that demonstrate the Addition, Subtraction, Multiplication, and Division using the class in Python.

Prerequisites

Examples

This program asks the user to enter two numbers and based on those two numbers (int format, converted with int() function) and also the operator (+ for addition, – for subtraction, * for multiplication, and / for division) then the result will be printed (print()) for the respective operation using class.

All the functions take two arguments and return the corresponding results. In the following example, when a class object is defined, all the properties of class aritOperations get assigned to arithObj object. Then we can access member functions of the class arithOperations using this object through dot (.) operator.

class arithOperations:
    def add(a, b):
        return a+b

    def subtract(a, b):
        return a-b

    def mult(a, b):
        return a*b

    def div(a, b):
        return a/b

# User input
num1 = int(input("Enter The First Number: "))
num2 = int(input("Enter The Second Number: "))

print("Which operation would you like to perform?")
operator = input("Enter any these operator +,-,*,/ : ")

# Defining object of class
arithOp = arithOperations()

result = 0
if operator == '+':
    operation = 'Addition'
    print(f"The {operation} of {num1} and {num2} is {arithOp.add(num1, num2)}.")
elif operator == '-':
    operation = 'Subtraction'
    print(f"The {operation} of {num1} and {num2} is {arithOp.subtract(num1, num2)}.")
elif operator == '*':
    operation = 'Multiplication'
    print(f"The {operation} of {num1} and {num2} is {arithOp.mult(num1, num2)}.")
elif operator == '/':
    operation = 'Division'
    print(f"The {operation} of {num1} and {num2} is {arithOp.div(num1, num2)}.")
else:
    print("Invalid input operator character!")

Here, the f-string is a string literal that is used as an f at the beginning and curly braces containing variables or expressions that will be replaced with its value.

Now run the code and when prompt enter the first number say 20 and press ENTER key, then enter the second number say 10, and press ENTER key and select the operator to perform respective operations. It will print the result on the output.

The new variable operation defined here is to use it dynamically when printing the output.

Note: Since the data type of both num1 and num2 is int. So, the data type of the result, result, will also be int. If one is int and another is float, then the result will be promoted to float.

Hierarchy: complex>float>int

Addition:

Enter The First Number: 20
Enter The Second Number: 10
Which operation would you like to perform?
Enter any these operator +,-,*,/ : +
The Addition of 20 and 10 is 30.

Subtraction:

Enter The First Number: 20
Enter The Second Number: 10
Which operation would you like to perform?
Enter any these operator +,-,*,/ : -
The Subtraction of 20 and 10 is 10.

Multiplication:

Enter The First Number: 20
Enter The Second Number: 10
Which operation would you like to perform?
Enter any these operator +,-,*,/ : *
The Multiplication of 20 and 10 is 200.

Division:

Enter The First Number: 20
Enter The Second Number: 10
Which operation would you like to perform?
Enter any these operator +,-,*,/ : /
The Division of 20 and 10 is 2.0.

When user input is other than the operator characters which are mentioned, it will print "Invalid input operator character!".

Conclusion

In this, we demonstrated different examples in Python to perform different arithmetic operations like addition, subtraction, multiplication, and division using user-defined functions with two numbers.