Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Basic calculator program using Python program
In this tutorial, we are going to build a basic calculator in Python. A calculator provides multiple arithmetic operations to users, allowing them to select one option and perform the respective calculation.
Following are the arithmetic operations that we can perform using a basic calculator −
- Addition
- Subtraction
- Multiplication
- Division
Steps in Developing the Basic Calculator
Following are the steps involved in creating a basic calculator in Python −
Defining Arithmetic Functions
First, we define all the arithmetic operations that can be performed by using a basic calculator ?
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
# Test the functions
print("Testing functions:")
print("5 + 3 =", add(5, 3))
print("5 - 3 =", subtract(5, 3))
print("5 * 3 =", multiply(5, 3))
print("5 / 3 =", divide(5, 3))
Testing functions: 5 + 3 = 8 5 - 3 = 2 5 * 3 = 15 5 / 3 = 1.6666666666666667
Here, each function takes two inputs and returns the result. In the divide function, we check to avoid division by zero errors.
Complete Calculator Program
Now let's create the complete calculator program that displays menu options, takes user input, and performs operations ?
# 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
# Display menu options
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
# Simulate user input for demonstration
choice = "1" # User chooses addition
num1 = 15.5 # First number
num2 = 4.2 # Second number
print(f"Enter choice (1/2/3/4): {choice}")
print(f"Enter first number: {num1}")
print(f"Enter second number: {num2}")
# Perform the operation
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")
Select operation: 1. Add 2. Subtract 3. Multiply 4. Divide Enter choice (1/2/3/4): 1 Enter first number: 15.5 Enter second number: 4.2 Result: 19.7
Division by Zero Example
Here's an example demonstrating the division by zero handling ?
def divide(x, y):
if y == 0:
return "Cannot divide by zero!"
return x / y
# Test division by zero
result = divide(20, 0)
print("20 / 0 =", result)
# Test normal division
result = divide(20, 4)
print("20 / 4 =", result)
20 / 0 = Cannot divide by zero! 20 / 4 = 5.0
Enhanced Calculator with Loop
For a more practical calculator, we can add a loop to perform multiple calculations ?
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
# Simulate multiple operations
operations = [
("1", 10, 5), # Addition
("2", 10, 3), # Subtraction
("3", 4, 7), # Multiplication
("4", 15, 3), # Division
]
for choice, num1, num2 in operations:
print(f"\nOperation: {choice}, Numbers: {num1}, {num2}")
if choice == '1':
print(f"Result: {num1} + {num2} = {add(num1, num2)}")
elif choice == '2':
print(f"Result: {num1} - {num2} = {subtract(num1, num2)}")
elif choice == '3':
print(f"Result: {num1} * {num2} = {multiply(num1, num2)}")
elif choice == '4':
print(f"Result: {num1} / {num2} = {divide(num1, num2)}")
Operation: 1, Numbers: 10, 5 Result: 10 + 5 = 15 Operation: 2, Numbers: 10, 3 Result: 10 - 3 = 7 Operation: 3, Numbers: 4, 7 Result: 4 * 7 = 28 Operation: 4, Numbers: 15, 3 Result: 15 / 3 = 5.0
Key Points
- Use
float()to handle both integers and decimal numbers - Always check for division by zero to prevent errors
- Use clear function names and proper error handling
- Consider adding input validation for robust programs
Conclusion
This basic calculator demonstrates fundamental Python concepts including functions, conditional statements, and user input handling. The program can be extended with additional operations like exponentiation, square root, or more advanced mathematical functions.
