Your First Python Project: Build a Calculator
Build a command-line calculator in about 30 lines of Python, learning functions, loops, conditions and error handling along the way.
Short answer: to make a calculator in Python, write a function that takes two numbers and an operator, then wrap it in a loop that reads the user's input and handles mistakes. The full program below is about 30 lines, uses no libraries and teaches functions, conditions, loops and error handling. It is a good first Python project for beginners.
What Will You Build?
A command-line calculator. The user types something like 8 * 3 and the program prints 24.0. It supports addition, subtraction, multiplication and division, and it does not crash on bad input. You need Python installed; if not, follow the learning guide and the setup chapter first.
Step 1: Put the Logic in a Function
Keep the maths separate from the input and output. A function with one clear job is easy to test. This is the same idea taught in the Python functions chapter.
def calculate(a, op, b):
if op == "+":
return a + b
if op == "-":
return a - b
if op == "*":
return a * b
if op == "/":
if b == 0:
raise ZeroDivisionError("Cannot divide by zero")
return a / b
raise ValueError(f"Unknown operator: {op}")
Step 2: Read Input in a Loop
The calculator should keep asking until the user types q. A while True loop with break does this. The line text.split() breaks "8 * 3" into three parts, and float() turns text into numbers.
def main():
print("Simple calculator. Type q to quit.")
while True:
text = input("Enter: number operator number (e.g. 8 * 3): ").strip()
if text.lower() == "q":
break
try:
a, op, b = text.split()
print("Result:", calculate(float(a), op, float(b)))
except ValueError as error:
print("Invalid input:", error)
except ZeroDivisionError as error:
print("Math error:", error)
if __name__ == "__main__":
main()
Save both parts in one file called calculator.py and run python calculator.py. Example session:
Enter: number operator number (e.g. 8 * 3): 8 * 3
Result: 24.0
Enter: number operator number (e.g. 8 * 3): 5 / 0
Math error: Cannot divide by zero
Enter: number operator number (e.g. 8 * 3): 2 ^ 3
Invalid input: Unknown operator: ^
How Does the Error Handling Work?
Three things can go wrong: the user types the wrong number of parts, a part that should be a number is not, or they divide by zero. Python raises a ValueError for the first two and we raise ZeroDivisionError ourselves. The try and except blocks catch them and print a friendly message instead of crashing. See errors and exceptions for more.
Ideas to Extend It
Add
**for powers and%for remainders.Keep a history list and print it when the user types
h.Write three tests with
assert calculate(2, "+", 3) == 5.Later, put it on GitHub with a short README: your first portfolio project.
Common Mistakes
Forgetting that
input()returns text."2" + "3"gives"23", so convert withfloat()first.Not handling division by zero. It is the classic crash.
Putting everything in one block. Separate functions are easier to read, test and reuse.
Using
eval()as a shortcut. It runs any code the user types, which is unsafe.
Key Takeaway
A calculator is a small project that combines functions, conditions, loops and error handling. Build it, break it with odd inputs, fix it, then extend it. That cycle is how programming skill grows.
Ready to start? You will learn the Python basics behind this project, then build bigger ones step by step. Begin with the free course Python for AI: From Zero to Your First AI App.
Related articles
More in Python
Jupyter vs VS Code vs Google Colab
Where should you write your first Python code? Compare Jupyter Notebook, VS Code and Google Colab, with a simple rule for choosing.
4 min readNov 15, 202610 Common Python Errors and How to Fix Them
The ten Python errors beginners meet most, each with its usual cause and a short code fix, plus how to read a traceback.
4 min readNov 8, 2026NumPy vs Pandas: What's the Difference?
NumPy handles fast numerical arrays and Pandas handles labelled tables built on top of it. See the differences, small code examples and which to learn first.
4 min readNov 6, 2026Python vs C++ for Beginners: Which Should You Learn First?
Python and C++ differ in how they run, how they handle types and memory, and what they are used for. Here is a fair comparison and a guide to choosing by goal.
4 min readNov 3, 2026Python vs JavaScript: Which Should You Learn First?
Python or JavaScript as a first language? Compare purpose, syntax, ecosystems and learning curve, then pick by your goal.
3 min readNov 1, 2026What Is Python and What Can You Build With It?
Python is a readable, general-purpose language used for AI, data analysis, automation and web backends. Here is what it is and what you can build.
4 min readOct 29, 2026
Comments
Sign in to join the discussion.

