How to Build a Simple Calculator with Python (Step-by-Step)


If you're new to Python and eager to apply your skills through a hands-on project, building a simple calculator is an ideal choice. In this article, we’ll guide you step by step to create a basic calculator that performs addition, subtraction, multiplication, and division—all from the command line.

Let’s break it down into small, understandable parts.


🛠️ Step 1: Define the Calculator Functions

We start by writing simple functions that perform each math operation.

def add(x, y):
    return x + y

This function takes two numbers and returns their sum.


def subtract(x, y):
    return x - y

Subtracts the second number from the first.


def multiply(x, y):
    return x * y

Multiplies the two numbers together.


def divide(x, y):
    if y == 0:
        return "Error: Cannot divide by zero."
    return x / y

Before dividing, we check if the second number is zero to avoid a crash.


📋 Step 2: Display Options to the User

We now print a menu of operations for the user to choose from.

print("Simple Python Calculator")
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")

This menu shows up in the terminal so users can select an operation using a number.


🔢 Step 3: Take User Input

Now we ask the user for their choice and the numbers they want to calculate.

choice = input("Enter choice (1/2/3/4): ")

This reads which operation the user wants to perform.


num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

We convert the input into float to allow decimal values, not just whole numbers.


Step 4: Perform the Operation

We check the user's choice and call the correct function.

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 input.")

This block handles different inputs and shows the result of the selected operation.


🔁 Step 5: Add a Loop for Repeated Use (Optional)

If you want the calculator to keep running until the user decides to quit, use a loop.

while True:
    print("\nSimple Python Calculator")
    print("Select operation:")
    print("1. Add")
    print("2. Subtract")
    print("3. Multiply")
    print("4. Divide")
    print("5. Exit")

    choice = input("Enter choice (1/2/3/4/5): ")

    if choice == '5':
        print("Exiting calculator. Goodbye!")
        break

    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 input.")

This enhancement turns your calculator into a fully interactive app!


🧪 Example Output

Simple Python Calculator
Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide
Enter choice (1/2/3/4): 2
Enter first number: 15
Enter second number: 4
Result: 11.0

🧠 What You Learned

  • How to write and use functions in Python

  • How to get and convert user input

  • How to use conditional logic with if/elif

  • How to use a loop to repeat actions


🚀 What’s Next?

Now that you have a working calculator, here are a few challenges:

  • Add operations like modulo (%) or power (**)

  • Improve error handling using try/except

  • Build a GUI version with tkinter

  • Keep a history of calculations


Conclusion

This project is a great way to build confidence with Python basics. It’s simple enough for beginners yet opens the door to more advanced features. Keep building, keep experimenting—and soon, you’ll be making more powerful applications.

Happy coding! 🐍


0
Subscribe to my newsletter

Read articles from FavourMogbonjubola directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

FavourMogbonjubola
FavourMogbonjubola

I'm a passionate full-stack web developer with a strong foundation in front-end and back-end technologies. I've honed my skills in HTML, CSS, JavaScript, Python, C, and more. I’m driven by a love for problem-solving and innovation, particularly in the field of Artificial Intelligence. I believe in the power of technology to shape a better future, and I’m excited to contribute to projects that push boundaries and create meaningful impact