Building a basic calculator is a great way to get started with Python programming. In this guide, we'll explore how to create a simple Python calculator that can perform basic arithmetic operations. We'll provide sample code to demonstrate the process.


Before you start building a Python calculator, ensure you have the following prerequisites:

  • Python installed on your system.
  • Basic knowledge of Python programming.

Building the Python Calculator

Let's create a basic Python calculator that can perform addition, subtraction, multiplication, and division.

# Function to add two numbers
def add(x, y):
return x + y
# Function to subtract two numbers
def subtract(x, y):
return x - y
# Function to multiply two numbers
def multiply(x, y):
return x * y
# Function to divide two numbers
def divide(x, y):
if y == 0:
return "Cannot divide by zero"
return x / y
# User input
num1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: ")
# Perform the calculation
if operator == '+':
result = add(num1, num2)
elif operator == '-':
result = subtract(num1, num2)
elif operator == '*':
result = multiply(num1, num2)
elif operator == '/':
result = divide(num1, num2)
result = "Invalid operator"
# Display the result
print("Result:", result)

Testing the Calculator

After running the Python calculator code, you can enter numbers and operators to perform calculations.

Enter first number: 5
Enter operator (+, -, *, /): +
Enter second number: 3
Result: 8.0


Building a basic Python calculator is a fun and educational project for beginners. It allows you to practice fundamental programming concepts and user input handling. You can expand the calculator's functionality as you become more comfortable with Python.