A calculator is one of the most practical beginner projects in Python, but it’s also useful for professionals because it reinforces how to design clean functions, validate user input, and handle edge cases safely. In this tutorial, We’ll build a Command-Line Interface (CLI) Simple Calculator in Python that performs four essential arithmetic operations: addition, subtraction, multiplication, and division.
Even though the final script is short, it demonstrates several core programming skills that show up in real-world software:
floatif/elif/else to choose actions based on user choicesBy the end, you’ll fully understand how the code works, how to build it step-by-step, and how to run it like a real mini project.
You will build a Python program that:
1, 2, 3, or 4).This is intentionally kept lightweight and clear so that students can learn it easily, and developers can use it as a base to extend into larger projects (like loop-based calculators, history tracking, error-proof input systems, or even a GUI calculator).
You don’t need advanced knowledge to follow this tutorial. Here’s what you should have:
if/elseinput()If you can run a Python file from the terminal, you’re ready.
A small project becomes easier to manage when you give it a clean structure.
python-cli-calculatorcalculator.pyYour project is now set up. Even though it’s just one file, this “project mindset” is important when you build bigger applications later.
If you installed Anaconda, you can also write and run this project using Jupyter Notebook. Create a new notebook, paste the code into a cell, and click Run (or press Shift + Enter) to execute the cell and see the output.
Before writing code, it helps to plan the design.
You could write all calculations inside if blocks. But using functions makes your code:
In professional codebases, functions are a standard way to keep logic clean.
We will create four functions:
add(x, y) → returns x + ysubtract(x, y) → returns x - ymultiply(x, y) → returns x * ydivide(x, y) → returns x / y but must safely handle division by zeroThis design keeps every operation isolated and easy to test.
Open calculator.py and start by writing these four functions.
The add function takes two parameters and returns their sum.
Why parameters? Because you want your function to work with any inputs, not fixed numbers.
def add(x, y):
return x + y
Code language: JavaScript (javascript)
Subtraction is similar. The function returns the difference.
def subtract(x, y):
return x - y
Code language: JavaScript (javascript)
Multiplication returns the product.
def multiply(x, y):
return x * y
Code language: JavaScript (javascript)
Division is where we need to think like a developer. In mathematics, division by zero is undefined, and in programming it often causes a runtime error.
So instead of letting the program crash, we handle the case gracefully.
def divide(x, y):
if y == 0:
return "Error: Division by zero"
return x / y
Code language: JavaScript (javascript)
y == 0, we don’t divide.This is called defensive programming—writing code that anticipates and handles invalid conditions.
Now that the calculator “engine” (functions) is ready, we build the user-facing menu.
A CLI project needs good text prompts so users understand what to do.
print("=== Simple Calculator ===")
print("Choose operation:")
print("1. Add (+)")
print("2. Subtract (-)")
print("3. Multiply (*)")
print("4. Divide (/)")
Code language: PHP (php)
print() calls?You could also store menu text in a multi-line string, but this style is simple and beginner-friendly.
The input() function reads text from the user in the terminal.
op = input("Enter Number (for e.g 1 For Add, 2 for Subtract ): ")
Code language: JavaScript (javascript)
Important detail: input() always returns a string.
So if the user types 1, Python stores it as '1'. That’s why later we compare op to '1', '2', '3', and '4' (strings).
Now we ask for two numbers.
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
Code language: JavaScript (javascript)
float?10.5.float("10") becomes 10.0.If you used int(), you would limit inputs to whole numbers only.
In professional tools, numeric parsing is often combined with validation, but for this version, float() is a good starting point.
if/elif/elseNow we implement the decision-making part: depending on the user’s chosen operation, we call the correct function.
if op == '1':
result = add(a, b)
elif op == '2':
result = subtract(a, b)
elif op == '3':
result = multiply(a, b)
elif op == '4':
result = divide(a, b)
else:
result = "Invalid operator"
Code language: PHP (php)
result?We want a consistent way to print the final output. Instead of printing inside every block, we store the output once and print at the end.
This makes the code easier to extend later. For example:
elif instead of multiple if?Because only one operation should be executed. With elif, once a match is found, the rest of the conditions are skipped.
Finally, we output the result:
print("Result:", result)
Code language: PHP (php)
This line is intentionally simple. It works whether the result is:
15.0)"Error: Division by zero")"Invalid operator")This is a small example of writing code that works well with multiple possible output types.
Once your calculator.py file is saved, run it from your terminal.
python Simple_calculator.py
Code language: CSS (css)
python3 Simple_calculator.py
Code language: CSS (css)
If your Python setup uses python for Python 3, then python Simple_calculator.py also works.
User selects addition and enters two numbers:


Notice how the program does not crash. It stays stable and prints a clear message.
Even though it’s a small script, this project teaches skills that scale:
Many real applications are built from these same building blocks—just at a larger scale.
If you want to make this project more professional, here are upgrades you can implement later:
hello will crash the program because float("hello") fails.try/except.while True loop and a quit option.%), power (**), square root, etc.2 instead of 2.0 when possible.add() and divide() using pytest or unittest.These upgrades can turn a beginner project into an interview-friendly mini application.
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 "Error: Division by zero"
return x / y
print("=== Simple Calculator ===")
print("Choose operation:")
print("1. Add (+)")
print("2. Subtract (-)")
print("3. Multiply (*)")
print("4. Divide (/)")
op = input("Enter Number (for e.g 1 For Add, 2 for Subtract ): ")
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
if op == '1':
result = add(a, b)
elif op == '2':
result = subtract(a, b)
elif op == '3':
result = multiply(a, b)
elif op == '4':
result = divide(a, b)
else:
result = "Invalid operator"
print("Result:", result)
Code language: PHP (php)