How to Build a Calculator in Python (Step-by-Step for Beginners)

In this tutorial, we’ll build a simple Python calculator that performs basic arithmetic operations like addition, subtraction, multiplication, and division.
Table of Contents:
Introduction
Python is great for building basic tools, and a calculator is a perfect starting project for any beginner. We'll be using Python’s built-in functions, and you’ll learn how to handle user input, write functions, and manage control flow.
Step 1: Define the Functions
Let's start by defining functions for each operation we want our calculator to perform.
Syntax for function definition:
def function_name():
codes come here
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: Cannot divide by zero."
return x / y
Step 2: Take User Input
Now, let’s take input from the user. We’ll ask the user to select an operation and input two numbers.
Syntax for taking user input:
variable = input("message you want to display to the user")
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
choice = input("Enter choice (1/2/3/4): ")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
Step 3: Perform the Operation and Show the Result
We can now use if-else statements to perform the selected operation and show the result.
Syntax for an if*-*else statement in python:
if condition:
# Code to execute if the condition is True
else:
# Code to execute if the condition is False
if choice == '1':
result = add(num1, num2)
print("Result:", result)
elif choice == '2':
result = subtract(num1, num2)
print("Result:", result)
elif choice == '3':
result = multiply(num1, num2)
print("Result:", result)
elif choice == '4':
result = divide(num1, num2)
print("Result:", result)
else:
print("Invalid input. Please select a valid operation.")
Step 4: Add a Loop to Repeat the Calculator
To make the calculator usable multiple times without restarting the program, let’s add a loop that allows the user to continue using the calculator until they decide to quit.
while True:
print("\nSelect 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. Please select a valid operation.")
Conclusion
Congrats! 🎉 You've just built a simple Python calculator that can perform basic arithmetic operations. This project teaches you valuable skills like handling user input, writing functions, and using control flow in Python.
What’s Next?
Now that you've created this basic calculator, here are some ideas to extend the project:
Add more advanced operations (modulus, exponentiation, square root)
Build a graphical user interface (GUI) for the calculator using
Tkinter
Turn the calculator into a web app with Flask or Django
Happy coding! 👨💻💡
Subscribe to my newsletter
Read articles from Michael directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
