Introduction
The Python Calculator Project is one of the most popular because it helps developers understand fundamental programming concepts such as variables, data types, operators, conditional statements, loops, functions, and user input handling.
A calculator is a simple application that performs mathematical operations like addition, subtraction, multiplication, and division.
Project Overview
The Python Calculator Project allows users to:
- Add two numbers
- Subtract two numbers
- Multiply two numbers
- Divide two numbers
- Perform multiple calculations
- Handle invalid inputs
Project Features
- User-friendly menu
- Supports basic arithmetic operations
- Error handling for division by zero
- Function-based implementation
- Reusable and maintainable code
Project Workflow
The calculator follows the following workflow:
Start
↓
Display Menu
↓
Enter First Number
↓
Enter Second Number
↓
Select Operation
↓
Perform Calculation
↓
Display Result
↓
Continue?
↓
Yes → Repeat
No → Exit
Algorithm
- Start the program.
- Take two numbers as input.
- Ask the user to select an operation.
- Perform the selected calculation.
- Display the result.
- Ask whether the user wants another calculation.
- Repeat if required.
- Exit the program.
Basic Calculator Project
Let’s create a simple calculator using conditional statements.
Source Code
num1 = float(input("Enter First Number: "))
num2 = float(input("Enter Second Number: "))
operator = input(
"Choose (+, -, *, /): "
)
if operator == "+":
print("Result =", num1 + num2)
elif operator == "-":
print("Result =", num1 - num2)
elif operator == "*":
print("Result =", num1 * num2)
elif operator == "/":
if num2 != 0:
print("Result =", num1 / num2)
else:
print("Cannot divide by zero")
else:
print("Invalid Operator")
Output:
Addition
Enter Second Number: 5
Choose (+, -, *, /): +
Result = 15
Multiplication
Enter Second Number: 4
Choose (+, -, *, /): *
Result = 48
Understanding the Code
Taking User Input
num1 = float(input())
num2 = float(input())
The float() function converts user input into decimal numbers.
Selecting an Operation
operator = input()
The user chooses which calculation to perform.
Using Conditional Statements
if operator == "+":
The calculator checks the selected operator and performs the corresponding operation.
Calculator Using Functions
Using functions improves code readability and reusability.
Source Code
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b == 0:
return "Cannot divide by zero"
return a / b
num1 = float(input("First Number: "))
num2 = float(input("Second Number: "))
operator = input(
"Choose (+,-,*,/): "
)
if operator == "+":
print(add(num1, num2))
elif operator == "-":
print(subtract(num1, num2))
elif operator == "*":
print(multiply(num1, num2))
elif operator == "/":
print(divide(num1, num2))
else:
print("Invalid Operator")
Advantages of Using Functions
- Better code organization
- Easy debugging
- Code reusability
- Improved readability
- Easier maintenance
Calculator with Multiple Calculations
A practical calculator should continue running until the user exits.
Source Code
while True:
num1 = float(
input("Enter First Number: ")
)
num2 = float(
input("Enter Second Number: ")
)
operator = input(
"Choose (+,-,*,/): "
)
if operator == "+":
print("Result =", num1 + num2)
elif operator == "-":
print("Result =", num1 - num2)
elif operator == "*":
print("Result =", num1 * num2)
elif operator == "/":
if num2 == 0:
print(
"Cannot divide by zero"
)
else:
print(
"Result =",
num1 / num2
)
else:
print("Invalid Operator")
choice = input(
"Continue? (yes/no): "
)
if choice.lower() != "yes":
break
Output:
Enter Second Number: 4
Choose (+,-,*,/): /
Result = 5.0
Continue? (yes/no): yes
Advanced Calculator Project
Let’s add more mathematical operations.
| Operation | Symbol |
|---|---|
| Addition | + |
| Subtraction | – |
| Multiplication | * |
| Division | / |
| Modulus | % |
| Exponent | ** |
Example
num1 = 10
num2 = 3
print(num1 % num2)
Output:
Power Operation
print(2 ** 4)
Output:
Scientific Calculator Using Math Module
Python’s built-in math module can be used to create a scientific calculator.
Import Math Module
import math
Square Root
import math
print(
math.sqrt(64)
)
Output:
Power Function
import math
print(
math.pow(2, 5)
)
Output:
Trigonometric Function
import math
print(
math.sin(0)
)
Output:
GUI Calculator Using Tkinter
Python’s Tkinter library can be used to create graphical calculators.
Example:
from tkinter import *
root = Tk()
root.title("Calculator")
root.geometry("300x300")
root.mainloop()
This creates a basic calculator window.
Common Errors and Solutions
1. Division by Zero
Incorrect:
10 / 0
Error:
Solution:
if num2 != 0:
print(num1 / num2)
2. Invalid Data Type
Incorrect:
num = input()
Input:
abc
Solution:
try:
num = float(input())
except ValueError:
print("Invalid Number")
3. Invalid Operator
Incorrect:
&
Solution:
else:
print("Invalid Operator")
Best Practices
Use Functions
Keep logic separate and reusable.
Validate Inputs
Prevent unexpected errors.
Use Exception Handling
try:
pass
except:
pass
Write Meaningful Variable Names
Good:
first_number
second_number
Bad:
a
b
Project Enhancement Ideas
Once you’ve built the basic calculator, try adding:
- Scientific Calculator
- GUI Calculator using Tkinter
- Percentage Calculator
- Currency Converter
- BMI Calculator
- GST Calculator
- Age Calculator
- Calculator History
- Dark Mode Interface
- Voice-Based Calculator
These enhancements make the project more practical and portfolio-worthy.
Conclusion
The Python Calculator Project is one of the best beginner-friendly projects for learning programming through practical implementation. It introduces essential concepts such as variables, operators, conditions, loops, functions, and exception handling while providing hands-on experience in building a real-world application.