PROJECT 01 • PYTHON BEGINNER PROJECT

Build a Python Calculator

Learn how to combine variables, user input, arithmetic operators, conditions and functions to create a practical Python calculator.

STEP 01

Project Introduction

In this project, we will build a simple calculator that can perform basic mathematical operations.

The calculator will allow the user to enter two numbers and choose an operation such as:

What you will learn: Variables, input(), type conversion, operators, if / elif / else and functions.
STEP 02

Taking User Input

Python's input() function allows us to receive information from the user.

Python
number1 = input("Enter first number: ")
number2 = input("Enter second number: ")

print(number1)
print(number2)

The important thing to understand is that input() normally returns text.

STEP 03

Converting Input to Numbers

Because calculator operations require numbers, we can convert the input using int() or float().

Python
number1 = float(input("Enter first number: "))
number2 = float(input("Enter second number: "))

print(number1 + number2)

Using float() also allows decimal values such as 10.5 or 25.75.

STEP 04

Understanding Operators

Our calculator needs arithmetic operators.

Python
addition = 10 + 5
subtraction = 10 - 5
multiplication = 10 * 5
division = 10 / 5

print(addition)
print(subtraction)
print(multiplication)
print(division)
STEP 05

Choosing an Operation

We can ask the user which operation they want to perform.

Python
operation = input("Enter operation (+, -, *, /): ")

print(operation)
STEP 06

Using if / elif / else

Now we can check the selected operation.

Python
if operation == "+":
    print(number1 + number2)

elif operation == "-":
    print(number1 - number2)

elif operation == "*":
    print(number1 * number2)

elif operation == "/":
    print(number1 / number2)
Important: Python uses == when comparing values.
STEP 07

Handling Division by Zero

A calculator should not attempt to divide a number by zero.

Python
elif operation == "/":

    if number2 == 0:
        print("Cannot divide by zero")
    else:
        print(number1 / number2)
STEP 08

Creating a Function

Functions allow us to organize reusable code.

Python
def calculate(a, b, operation):

    if operation == "+":
        return a + b

    elif operation == "-":
        return a - b

    elif operation == "*":
        return a * b

    elif operation == "/":

        if b == 0:
            return "Cannot divide by zero"

        return a / b

    return "Invalid operation"
STEP 09

Calling the Function

After creating the function, we can call it with our values.

Python
result = calculate(
    number1,
    number2,
    operation
)

print("Result:", result)
STEP 10

Complete Project Flow

1 Input
2 Convert
3 Operation
4 Calculate
5 Output
STEP 11

What You Practiced

Ready to Run the Complete Project?

You have learned the important concepts. Now open the complete Python Calculator and test it with different numbers and operations.

Run Complete Project →