Python Calculator App - A Complete Project for School Students

A calculator is one of the best beginner-friendly Python projects. This tutorial will guide you through building a basic calculator using Python, suitable for 10th and 12th standard students.


πŸ“Œ Project Overview

  • Project Name: Simple Calculator
  • Programming Language: Python
  • Difficulty Level: Beginner
  • Tools Required: Python (IDLE, VS Code, or PyCharm)

πŸ› οΈ Step 1: Install Python

Before starting, install Python (if not already installed).

πŸ”— Download Python: https://www.python.org/downloads/

βœ… Check Python Version:

python --version

✍️ Step 2: Write the Basic Calculator Code

Create a new Python file (calculator.py) and write the following code:

# Simple Calculator in Python

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! Division by zero."
    return x / y

print("Select Operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")

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

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

if choice == '1':
    print(f"Result: {add(num1, num2)}")
elif choice == '2':
    print(f"Result: {subtract(num1, num2)}")
elif choice == '3':
    print(f"Result: {multiply(num1, num2)}")
elif choice == '4':
    print(f"Result: {divide(num1, num2)}")
else:
    print("Invalid Input")

🎯 Step 3: Run the Calculator Program

Save the file as calculator.py and run it using the terminal:

python calculator.py

πŸ–₯️ Example Output:

Select Operation: 1. Add 2. Subtract 3. Multiply 4. Divide Enter choice (1/2/3/4): 1 Enter first number: 10 Enter second number: 5 Result: 15.0

πŸ–ŒοΈ Step 4: Improve with a GUI (Tkinter)

We can enhance the calculator by adding a Graphical User Interface (GUI) using Tkinter.

πŸ“Œ Install Tkinter:
No installation is requiredβ€”it’s built into Python.

βœ… GUI Calculator Code:

import tkinter as tk
from tkinter import messagebox

def click_button(button_text):
    current_text = entry.get()
    entry.delete(0, tk.END)
    entry.insert(0, current_text + button_text)

def clear_entry():
    entry.delete(0, tk.END)

def calculate():
    try:
        result = eval(entry.get())
        entry.delete(0, tk.END)
        entry.insert(0, str(result))
    except:
        messagebox.showerror("Error", "Invalid Expression")

# Create GUI Window
root = tk.Tk()
root.title("Simple Calculator")
root.geometry("320x420")
root.configure(bg="#1e1e2e")

entry = tk.Entry(root, width=20, font=("Arial", 20), bd=5, relief=tk.FLAT, justify='right')
entry.grid(row=0, column=0, columnspan=4, pady=10, padx=10)
entry.configure(bg="#f8f8f2", fg="#282a36")

button_style = {"font": ("Arial", 16), "bd": 3, "relief": tk.RAISED, "width": 5, "height": 2}

buttons = [
    ('7', 1, 0), ('8', 1, 1), ('9', 1, 2), ('/', 1, 3),
    ('4', 2, 0), ('5', 2, 1), ('6', 2, 2), ('*', 2, 3),
    ('1', 3, 0), ('2', 3, 1), ('3', 3, 2), ('-', 3, 3),
    ('0', 4, 0), ('.', 4, 1), ('=', 4, 2), ('+', 4, 3),
]

for text, row, col in buttons:
    if text == '=':
        btn = tk.Button(root, text=text, command=calculate, **button_style, bg="#50fa7b", fg="#282a36")
    else:
        btn = tk.Button(root, text=text, command=lambda t=text: click_button(t), **button_style, bg="#44475a", fg="#f8f8f2")
    btn.grid(row=row, column=col, padx=5, pady=5)

tk.Button(root, text="C", command=clear_entry, **button_style, bg="#ff5555", fg="#f8f8f2").grid(row=5, column=0, columnspan=4, pady=10, ipadx=50)

root.mainloop()

βœ… Run the program, and a calculator window will appear! πŸŽ‰

πŸ“– Best Practices for Writing Python Projects

βœ… Use Functions: Keep the code modular by using functions for operations.
βœ… Error Handling: Always handle errors (e.g., division by zero).
βœ… User Input Validation: Ensure valid input before calculations.
βœ… Comment the Code: Use comments to explain complex logic.
βœ… Use GUI for Better UX: A Tkinter-based GUI makes it user-friendly.