Python program to create a class which performs basic calculator operations

In this module, we will learn to create a class which performs basic calculator operations in Python.

Being an object-oriented programming language, python stresses on concepts like classes and objects. Classes are required to create objects. They act like a blueprint or template for the creation of objects. Similar types of variables and functions are collected and are placed into a class. We can hence reuse this class to build different objects. Classes make the code more efficient to use. As related data are grouped together, code looks clear and simple enough to understand. Classes are defined with a keyword class.

Syntax:

class Class_name:

To create an object we simply use the syntax:

object = Class_name()

You may also read this: Voice Command Calculator in Python using speech recognition and PyAudio



Create a basic calculator using class in python

Problem statement: Write a python program to create a class which performs basic calculator operations.

Let us solve this step by step,

STEP 1: Create a class Calculator and define all the functions of a basic calculator like addition, subtraction, multiplication and division.

class Calculator:

    def addition(self):
        print(a + b)

    def subtraction(self):
        print(a - b)

    def multiplication(self):
        print(a * b)

    def division(self):
        print(a / b)

Here, self is used because while calling function using obj.function() (in the following steps), the function will call itself.

STEP 2: Next, take inputs from the user and create an object.

a = int(input("Enter first number:"))
b = int(input("Enter first number:"))

obj = Calculator()

STEP 3: Lastly, create choices for the user to perform which operation they need and print out the solution.

choice = 1
while choice != 0:
    print("1. ADD")
    print("2. SUB")
    print("3. MUL")
    print("4. DIV")
    choice = int(input("Enter your choice:"))
    
    if choice == 1:
        print(obj.addition())
    elif choice == 2:
        print(obj.subtraction())
    elif choice == 3:
        print(obj.multiplication())
    elif choice == 4:
        print(obj.division())
    else:
        print("Invalid choice")

Here is the whole code:

class Calculator:

    def addition(self):
        print(a + b)

    def subtraction(self):
        print(a - b)

    def multiplication(self):
        print(a * b)

    def division(self):
        print(a / b)


a = int(input("Enter first number:"))
b = int(input("Enter first number:"))

obj = Calculator()

choice = 1
while choice != 0:
    print("1. ADDITION")
    print("2. SUBTRACTION")
    print("3. MULTIPLICATION")
    print("4. DIVISION")
    choice = int(input("Enter your choice:"))
    if choice == 1:
        print(obj.addition())
    elif choice == 2:
        print(obj.subtraction())
    elif choice == 3:
        print(obj.multiplication())
    elif choice == 4:
        print(obj.division())
    else:
        print("Invalid choice")

Output:

Enter first number:3
Enter first number:2
1. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
Enter your choice:1
5
1. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
Enter your choice:2
1
1. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
Enter your choice:3
6
1. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
Enter your choice:4
1.5
1. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
Enter your choice:5
Invalid choice

Hence, we have successfully created a class of basic calculator in python.

NOTE: There may be other possible methods to solve this problem.

 

2 responses to “Python program to create a class which performs basic calculator operations”

  1. Bruna Moreira says:

    Awesome!!!

  2. Asma Khan says:

    Thanks!!!

Leave a Reply

Your email address will not be published. Required fields are marked *