
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Basic Calculator Program Using Python
In this tutorial, we are going to build a basic calculator in Python. As we all know that a calculator will give six options to the user from which they select one option and we will perform the respective operation.
Following are the arithmetic operations that we can perform using a basic calculator -
- Addition
- Subtraction
- Multiplication
- Division
- Floor Division
- Modulo
Steps in Developing the Basic Calculator
Following are the steps involved in creating a basic calculator in Python -
Defining Arithmetic Functions
First, we are defining all the arithmetic operations that can be performed by using a basic calculator with the help of Python -
def add(x, y): return x + y def subtract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): if y == 0: return "Cannot divide by zero!" return x / y
Here, in the above program, each function takes two inputs and returns the result, but in the divide function, we will check to avoid division by zero errors.
Displaying Menu Options
Next, we will provide the feasibility for the user to choose which operation to perform -
print("Select operation:") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide")
Taking User Input
Now we ask the user to provide the choice of the arithmetic operation and the two numbers on which the operation is to be performed -
choice = input("Enter choice (1/2/3/4): ") num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: "))
Note: We use the float() method to make sure the calculator works with both integers and decimals.
Performing the Operation
Based on the user's choice, we call the appropriate function with the help of code block below -
if choice == '1': print("Result:", add(num1, num2)) elif choice == '2': print("Result:", subtract(num1, num2)) elif choice == '3': print("Result:", multiply(num1, num2)) elif choice == '4': print("Result:", divide(num1, num2)) else: print("Invalid input")
Finally, here is the complete program of a basic calculator -
# Basic Calculator Program def add(x, y): return x + y def subtract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): if y == 0: return "Cannot divide by zero!" return x / y print("Select operation:") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide") choice = input("Enter choice (1/2/3/4): ") num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) if choice == '1': print("Result:", add(num1, num2)) elif choice == '2': print("Result:", subtract(num1, num2)) elif choice == '3': print("Result:", multiply(num1, num2)) elif choice == '4': print("Result:", divide(num1, num2)) else: print("Invalid input")
Here is the output of the above Program -
Select operation: 1. Add 2. Subtract 3. Multiply 4. Divide Enter choice (1/2/3/4): 4 Enter first number: 20 Enter second number: 0 Result: Cannot divide by zero!