Home โ†’ Projects โ†’ Calculator

Project 01: Calculator

Build a simple calculator using Python.

Beginner Python Project 01

๐Ÿ“Œ Project Overview

In this project, you will create a simple calculator that performs basic mathematical operations.

What you will learn:
  • Taking input from the user
  • Converting input into numbers
  • Using arithmetic operators
  • Using if-elif-else
  • Displaying results

โš™๏ธ Calculator Operations

+ Addition
- Subtraction
ร— Multiplication
รท Division

Step 1: Take Input

First, take two numbers from the user.

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

Step 2: Choose an Operation

Ask the user which mathematical operation they want to perform.

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

Step 3: Perform Calculation

Use conditional statements to perform the selected operation.

Python
if operator == "+":
    result = num1 + num2

elif operator == "-":
    result = num1 - num2

elif operator == "*":
    result = num1 * num2

elif operator == "/":
    result = num1 / num2

else:
    print("Invalid operator")

๐Ÿš€ Complete Python Code

Now combine all the steps into one program.

calculator.py
print("===== Python Calculator =====")

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

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

if operator == "+":
    result = num1 + num2

elif operator == "-":
    result = num1 - num2

elif operator == "*":
    result = num1 * num2

elif operator == "/":

    if num2 != 0:
        result = num1 / num2
    else:
        print("Cannot divide by zero")
        result = None

else:
    print("Invalid operator")
    result = None


if result is not None:
    print("Result =", result)

๐Ÿงฎ Try the Calculator

Enter two numbers and choose an operation.

Result will appear here.

๐Ÿ’ป Example Output

===== Python Calculator =====

Enter first number: 20
Enter second number: 5
Enter operator (+, -, *, /): *

Result = 100.0

๐Ÿง  Mini Challenge

Try improving the calculator by adding these features:

โ† All Projects Next Project โ†’