🔰 Project Overview

A calculator is one of the simplest yet most effective projects to start with in Python.
You will learn:
- Taking user input.
- Using functions for organization.
- Handling basic operations (addition, subtraction, multiplication, division).
- Using conditionals to control the logic.
✍️ Step 1: Plan the Calculator
We need:
- A menu for users to choose an operation.
- Functions for each operation.
- A loop to keep the calculator running until the user decides to quit.
🖥️ Step 2: Write the Code
# Basic Calculator in Python
# Define operations
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 a / b
else:
return "Error! Division by zero."
# Main program loop
while True:
print("\n📌 Simple Calculator")
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")
print("5. Exit")
choice = input("👉 Choose an operation (1-5): ")
if choice == "5":
print("✅ Exiting the calculator. Goodbye!")
break
if choice in ["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 choice, please try again.")
🎯 Sample Output
📌 Simple Calculator
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Exit
👉 Choose an operation (1-5): 1
Enter first number: 10
Enter second number: 5
Result: 15.0
📊 Operation Summary Table
| Operation | Function Used | Example Input | Example Output |
|---|---|---|---|
| Addition | add(a, b) | 10 + 5 | 15.0 |
| Subtraction | subtract(a,b) | 10 – 5 | 5.0 |
| Multiplication | multiply(a,b) | 10 * 5 | 50.0 |
| Division | divide(a,b) | 10 / 5 | 2.0 |
| Division by 0 | divide(a,b) | 10 / 0 | Error message |
🧠 Exercise for You
- Add a power function (exponentiation).
- Add a modulus function (remainder).
- Improve the calculator by allowing the user to enter the operation as a symbol (
+,-,*,/) instead of numbers.
📝 Summary
- You learned how to build a basic calculator using Python.
- You practiced functions, loops, conditionals, and input handling.
- This project is the foundation for building more advanced tools like a scientific calculator.