Projects are one of the best ways to practice Python programming. After learning variables, data types, conditions, loops, functions, lists, dictionaries, files, OOP, databases and other concepts, you can combine these concepts to build useful applications.
A calculator is a simple beginner-level Python project. It can perform basic arithmetic operations such as addition, subtraction, multiplication and division.
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
choice = input("Enter your choice: ")
if choice == "1":
print("Result:", num1 + num2)
elif choice == "2":
print("Result:", num1 - num2)
elif choice == "3":
print("Result:", num1 * num2)
elif choice == "4":
if num2 != 0:
print("Result:", num1 / num2)
else:
print("Cannot divide by zero.")
else:
print("Invalid choice.")
In a number guessing game, Python generates a random number and the user tries to guess it.
import random
number = random.randint(1, 100)
while True:
guess = int(input("Guess the number: "))
if guess == number:
print("Congratulations! Correct guess.")
break
elif guess < number:
print("Try a higher number.")
else:
print("Try a lower number.")
This project helps you practice loops, conditions and the random module.
This project checks whether a number is even or odd.
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even Number")
else:
print("Odd Number")
The modulus operator % returns the remainder of a division.
A student marks project can calculate total marks, percentage and grade.
name = input("Enter student name: ")
math = float(input("Enter Maths marks: "))
english = float(input("Enter English marks: "))
science = float(input("Enter Science marks: "))
total = math + english + science
percentage = total / 3
print("Student:", name)
print("Total Marks:", total)
print("Percentage:", percentage)
if percentage >= 80:
grade = "A"
elif percentage >= 60:
grade = "B"
elif percentage >= 40:
grade = "C"
else:
grade = "F"
print("Grade:", grade)
A quiz application asks questions and calculates the user's score.
score = 0
answer = input("What is the capital of India? ")
if answer.lower() == "new delhi":
print("Correct!")
score += 1
else:
print("Wrong!")
answer = input("Which language are we learning? ")
if answer.lower() == "python":
print("Correct!")
score += 1
else:
print("Wrong!")
print("Your Score:", score)
You can expand this project by storing questions in a list or dictionary.
A To-Do List application allows users to add, view and remove tasks.
tasks = []
while True:
print("\n1. Add Task")
print("2. View Tasks")
print("3. Remove Task")
print("4. Exit")
choice = input("Enter choice: ")
if choice == "1":
task = input("Enter task: ")
tasks.append(task)
print("Task added.")
elif choice == "2":
if len(tasks) == 0:
print("No tasks available.")
else:
for i, task in enumerate(tasks, start=1):
print(i, task)
elif choice == "3":
number = int(input("Enter task number: "))
if 1 <= number <= len(tasks):
tasks.pop(number - 1)
print("Task removed.")
else:
print("Invalid task number.")
elif choice == "4":
break
else:
print("Invalid choice.")
A contact book can store names and phone numbers using a dictionary.
contacts = {}
name = input("Enter name: ")
phone = input("Enter phone number: ")
contacts[name] = phone
print("\nContact List:")
for name, phone in contacts.items():
print(name, ":", phone)
You can improve the project by adding search, update and delete features.
A password generator creates random passwords using letters, numbers and special characters.
import random
import string
characters = string.ascii_letters + string.digits + string.punctuation
length = int(input("Enter password length: "))
password = ""
for i in range(length):
password += random.choice(characters)
print("Generated Password:", password)
The string module provides useful collections of characters.
An expense tracker can store expenses and calculate the total amount spent.
expenses = []
while True:
item = input("Enter expense item: ")
if item.lower() == "done":
break
amount = float(input("Enter amount: "))
expenses.append({
"item": item,
"amount": amount
})
total = 0
for expense in expenses:
print(expense["item"], ":", expense["amount"])
total += expense["amount"]
print("Total Expense:", total)
This project combines lists, dictionaries, loops and functions.
Python can store student information in files. This can be used to create a small student management system.
name = input("Enter student name: ")
course = input("Enter course: ")
mobile = input("Enter mobile number: ")
with open("students.txt", "a") as file:
file.write(name + "," + course + "," + mobile + "\n")
print("Student saved successfully.")
The project can later be extended with student search, update and delete functionality.
Functions make projects easier to organize and maintain.
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
print(add(10, 5))
print(subtract(10, 5))
print(multiply(10, 5))
Instead of writing the same code repeatedly, create functions and call them whenever required.
Object-oriented programming can be used when a project contains multiple related objects.
class Student:
def __init__(self, name, course):
self.name = name
self.course = course
def display(self):
print("Name:", self.name)
print("Course:", self.course)
student1 = Student("Rahul", "Python")
student1.display()
Classes are useful for larger applications because they help organize data and behavior together.
JSON is useful for storing structured data in a file.
import json
student = {
"name": "Rahul",
"course": "Python",
"age": 20
}
with open("student.json", "w") as file:
json.dump(student, file, indent=4)
print("Data saved.")
JSON is commonly used when applications need to store or exchange structured data.
Python can connect to databases such as SQLite and MySQL. A student management system is a good project for practicing database programming.
import sqlite3
connection = sqlite3.connect("school.db")
cursor = connection.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY,
name TEXT,
course TEXT
)
""")
cursor.execute(
"INSERT INTO students (name, course) VALUES (?, ?)",
("Rahul", "Python")
)
connection.commit()
connection.close()
Always use parameterized SQL queries when inserting values into a database.
After learning core Python, you can use Flask to build web applications.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Welcome to My Python Website"
if __name__ == "__main__":
app.run(debug=True)
Flask can be used to build websites, APIs and database-driven applications.
You can create applications that communicate with web APIs using the requests library.
import requests
url = "https://example.com/api/data"
response = requests.get(url, timeout=10)
if response.ok:
data = response.json()
print(data)
else:
print("Request failed:", response.status_code)
API projects help you understand HTTP requests, JSON data and client-server communication.
Step 1: Choose a simple problem.
Step 2: Decide what features your application needs.
Step 3: Create the project folder.
Step 4: Break the project into smaller functions.
Step 5: Create the required data structures.
Step 6: Write and test each feature separately.
Step 7: Add error handling.
Step 8: Add file or database storage if required.
Step 9: Test the complete application.
Step 10: Improve the user interface and documentation.
A larger Python application can be divided into multiple files. For example:
student_project/
│
├── main.py
├── database.py
├── models.py
├── functions.py
├── config.py
│
├── data/
│ └── students.json
│
└── README.md
Separating responsibilities makes the application easier to understand and maintain.
Testing helps you find errors before users encounter them.
def add(a, b):
return a + b
result = add(10, 20)
assert result == 30
print("Test passed!")
You should test normal inputs as well as invalid and unexpected inputs.
Debugging means finding and fixing problems in a program.
Common problems include:
Use error messages, print statements, debugging tools and tests to identify problems.
Now create your own Student Management System.
Your project can contain:
Start with a command-line version and later convert it into a web application using Flask.
Question: Which Python concept is commonly used to divide a large project into reusable blocks of code?