how to make a calculator with python

Try the Interactive Calculator

Use this quick calculator to test basic operations while you learn how to build the same logic in Python.

Tip: Press Enter in any field to calculate instantly.

Why build a calculator in Python?

A calculator is one of the best beginner projects because it teaches core programming skills fast: variables, user input, conditionals, functions, loops, and error handling. You can start with a tiny command-line calculator and grow it into a GUI app with buttons.

If you're learning Python, this project gives you a practical way to understand how programs take input, process logic, and return output.

What you need

  • Python 3 installed on your computer
  • A text editor (VS Code, PyCharm, or even Notepad)
  • Basic understanding of print(), input(), and variables

Version 1: Simple Python calculator (2 numbers + operation)

Start with this beginner-friendly script. It asks the user for two numbers, then applies one operation.

num1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: "))

if operator == "+":
    result = num1 + num2
elif operator == "-":
    result = num1 - num2
elif operator == "*":
    result = num1 * num2
elif operator == "/":
    if num2 != 0:
        result = num1 / num2
    else:
        result = "Error: Cannot divide by zero."
else:
    result = "Error: Invalid operator."

print("Result:", result)

How it works

  • float(input(...)) converts typed input into decimal numbers.
  • if / elif / else checks which operator was entered.
  • A divide-by-zero check prevents runtime errors.

Version 2: Cleaner calculator using functions

Functions make your code easier to read and reuse. This is closer to production-style code.

def calculate(num1, num2, operator):
    if operator == "+":
        return num1 + num2
    elif operator == "-":
        return num1 - num2
    elif operator == "*":
        return num1 * num2
    elif operator == "/":
        if num2 == 0:
            return "Error: Cannot divide by zero."
        return num1 / num2
    elif operator == "^":
        return num1 ** num2
    elif operator == "%":
        if num2 == 0:
            return "Error: Cannot use modulo by zero."
        return num1 % num2
    else:
        return "Error: Invalid operator."

a = float(input("First number: "))
op = input("Operator (+, -, *, /, ^, %): ")
b = float(input("Second number: "))

print("Result:", calculate(a, b, op))

Why this is better

With a function, you can call the same logic from a command-line app, a GUI app, or even a web API later.

Version 3: Repeating calculator with a loop

Most real calculators don't close after one answer. Add a loop so users can calculate repeatedly.

def calculate(num1, num2, operator):
    if operator == "+":
        return num1 + num2
    elif operator == "-":
        return num1 - num2
    elif operator == "*":
        return num1 * num2
    elif operator == "/":
        if num2 == 0:
            return "Error: Cannot divide by zero."
        return num1 / num2
    return "Error: Invalid operator."

while True:
    try:
        n1 = float(input("Enter first number: "))
        op = input("Enter operator (+, -, *, /): ")
        n2 = float(input("Enter second number: "))
        print("Result:", calculate(n1, n2, op))
    except ValueError:
        print("Error: Please enter valid numbers.")

    again = input("Do another calculation? (y/n): ").lower()
    if again != "y":
        print("Goodbye!")
        break

Common mistakes (and quick fixes)

  • Using strings as numbers: Convert with int() or float().
  • Forgetting divide-by-zero handling: Always check before dividing.
  • No input validation: Use try/except to catch invalid input.
  • Messy branching: Move operation logic into a function.

Want a GUI calculator next?

After mastering command-line calculators, try building one with tkinter so users can click buttons. You'll learn event-driven programming and UI layout, which are valuable for desktop applications.

Sample growth path

  • Step 1: CLI calculator (done)
  • Step 2: Add loop and error handling
  • Step 3: Add advanced operations (power, roots, percentages)
  • Step 4: Build GUI with tkinter
  • Step 5: Package it as a desktop app

Final thoughts

If you're asking how to make a calculator with Python, you're on the right path. It is simple enough to finish quickly but deep enough to teach strong fundamentals. Start small, improve one feature at a time, and you'll build confidence fast.

๐Ÿ”— Related Calculators