- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Python Program to Create a class performing Calculator Operations

When it is required to create a class that performs calculator operations, object oriented method is used. Here, a class is defined, and attributes are defined. Functions are defined within the class that perform certain operations. An instance of the class is created, and the functions are used to perform calculator operations.

Below is a demonstration for the same −

## Example

class calculator_implementation(): def __init__(self,in_1,in_2): self.a=in_1 self.b=in_2 def add_vals(self): return self.a+self.b def multiply_vals(self): return self.a*self.b def divide_vals(self): return self.a/self.b def subtract_vals(self): return self.a-self.b input_1 = int(input("Enter the first number: ")) input_2 = int(input("Enter the second number: ")) print("The entered first and second numbers are : ") print(input_1, input_2) my_instance = calculator_implementation(input_1,input_2) choice=1 while choice!=0: print("0. Exit") print("1. Addition") print("2. Subtraction") print("3. Multiplication") print("4. Division") choice=int(input("Enter your choice... ")) if choice==1: print("The computed addition result is : ",my_instance.add_vals()) elif choice==2: print("The computed subtraction result is : ",my_instance.subtract_vals()) elif choice==3: print("The computed product result is : ",my_instance.multiply_vals()) elif choice==4: print("The computed division result is : ",round(my_instance.divide_vals(),2)) elif choice==0: print("Exit") else: print("Sorry, invalid choice!") print()

## Output

Enter the first number: 70 Enter the second number: 2 The entered first and second numbers are : 70 2 0. Exit 1. Addition 2. Subtraction 3. Multiplication 4. Division Enter your choice... 1 The computed addition result is : 72 0. Exit 1. Addition 2. Subtraction 3. Multiplication 4. Division Enter your choice... 2 The computed subtraction result is : 68 0. Exit 1. Addition 2. Subtraction 3. Multiplication 4. Division Enter your choice... 3 The computed product result is : 140 0. Exit 1. Addition 2. Subtraction 3. Multiplication 4. Division Enter your choice... 4 The computed division result is : 35.0 0. Exit 1. Addition 2. Subtraction 3. Multiplication 4. Division Enter your choice... 0 Exit

## Explanation

- A class named ‘calculator_implementation’ class is defined, that has functions like ‘add_vals’, ‘subtract_vals’, ‘multiply_vals’, and ‘divide_vals’.
- These are used to perform calculator operations such as addition, subtraction, multiplication, and division respectively.
- An instance of this class is created.
- The value for the two numbers are entered and operations are performed on it.
- Relevant messages and output is displayed on the console.

- Related Questions & Answers
- Golang Program to create a Class that can perform basic Calculator Operations
- Java Program to create a Calculator
- Program to create grade calculator in Python
- Program to find maximum score from performing multiplication operations in Python
- Program to check final answer by performing given stack operations in Python
- Program to find maximum sum by performing at most k negate operations in Python
- Basic calculator program using Python program
- Basic calculator program using Python
- Program to find expected sum of subarrays of a given array by performing some operations in Python
- Performing Bitwise Operations with BigInteger in Java
- Create a Calculator function in JavaScript
- Reduce a number to 1 by performing given operations in C++
- Final string after performing given operations in C++
- Create a simple calculator using Java Swing
- What are character class operations in Python?

Advertisements