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.

 

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

  1. Bruna Moreira says:

    Awesome!!!

  2. Asma Khan says:

    Thanks!!!

  3. abma2008 says:

    I believe the coding is good, but as the procedure followed in design, I think it is a little bit questionable.
    What if I want to terminate the program through a command or option, what should I do then?
    please provide an answer if you know-how, while loop is an infinite loop, it will keep going forever until you break it.
    there has got to be another way to stop it

    Thanks

    • Suman phuyal says:

      While choice !=0 means when you put 0 as choice the while terminates because the condition is false as 0!=0:
      And loop continue when only condtion is true

    • Pradhyumna MADHUSUDAN says:

      Yes you are absolutely right, there are many ways to terminate a while loop. One way is to use the break statement. The second way is to have an option at the each of each calculation which allows the user to decide whether they want to quit or continue. Multiply ways can be applied for your scenario. You got to remember that in programming, every way provided the logic works is good code and not there is no way of doing something. There are mutliple ways

  4. Govind says:

    Hi abma2008,
    The code with quit option is below.
    class Calculator:
    def addition(self):
    return (a + b)
    def subtraction(self):
    return (a – b)
    def multiplication(self):
    return (a * b)
    def division(self):
    return (a / b)
    a = int(input(“Enter first number:”))
    b = int(input(“Enter first number:”))
    obj = Calculator()
    choice = 1
    while choice != 0:
    print(“1. ADD”)
    print(“2. SUB”)
    print(“3. MUL”)
    print(“4. DIV”)
    print(“5. QUIT”)
    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())
    elif choice == 5:
    print(“You wish to quit, Thanks for using my program”)
    choice = 0
    else:
    print(“Invalid choice”)

    • preetam says:

      what if we want to re-enter the integer, should we use
      the reenter function in class & I’m little confuse about the choice = 0 when while is != 0 it will stop then why we use 5 instead of 0
      def reenter():

      a = int(input(“enter 1st number”))
      b = int(input(“enter 2nd number”))

      reenter()
      &
      elif choice == 5:
      print(reenter())

Leave a Reply

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