This is a beginner‑friendly Python project designed to teach core programming concepts while gradually introducing more advanced features. This document provides explanations, examples, and learning outcomes.
For this project you will need:
def calculator():
history = []
print("Type 'history' to view calculations.")
print("Type 'q' to quit.\n")
while True:
try:
first = input("Enter the first number: ").strip()
if first.lower() == "q":
print("Goodbye!")
break
if first.lower() == "history":
show_history(history)
continue
num1 = float(first)
operator = input(
"Enter an operator (+, -, *, /, **, %, //): "
).strip()
if operator.lower() == "q":
print("Goodbye!")
break
if operator not in ["+", "-", "*", "/", "**", "%", "//"]:
print("Invalid operator.\n")
continue
second = input("Enter the second number: ").strip()
if second.lower() == "q":
print("Goodbye!")
break
num2 = float(second)
if operator in ["/", "%", "//"] and num2 == 0:
print("You cannot divide by zero.\n")
continue
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "*":
result = num1 * num2
elif operator == "/":
result = num1 / num2
elif operator == "**":
result = num1 ** num2
elif operator == "%":
result = num1 % num2
elif operator == "//":
result = num1 // num2
calculation = f"{num1:g} {operator} {num2:g} = {result:g}"
print(f"\nResult: {calculation}\n")
history.append(calculation)
except ValueError:
print("Please enter a valid number.\n")
def show_history(history):
print("\n---------- HISTORY ----------")
if not history:
print("No calculations yet.")
else:
for number, calculation in enumerate(history, start=1):
print(f"{number}. {calculation}")
print("-----------------------------\n")
calculator()
The program asks for:
Special commands:
The try/except block prevents crashes when users enter invalid numbers.
Each operator performs a different calculation:
Every successful calculation is stored in a list and displayed on request.
Type 'history' to view calculations. Type 'q' to quit. Enter the first number: 12 Enter an operator (+, -, *, /, **, %, //): * Enter the second number: 5 Result: 12 * 5 = 60 Enter the first number: history ---------- HISTORY ---------- 1. 12 * 5 = 60 -----------------------------
The Calculator is a perfect progression project for beginners. It starts simple, grows naturally, and ends with a visually impressive GUI version.