1.
Library Management System (Object-Oriented)
A program to manage books, users, and borrowing.
class Book:
def __init__(self, title, author, isbn):
[Link] = title
[Link] = author
[Link] = isbn
[Link] = True
def __str__(self):
return f"{[Link]} by {[Link]} (ISBN: {[Link]})"
class Library:
def __init__(self):
[Link] = []
def add_book(self, book):
[Link](book)
def borrow_book(self, isbn):
for book in [Link]:
if [Link] == isbn and [Link]:
[Link] = False
return f"You borrowed: {[Link]}"
return "Book not available"
def return_book(self, isbn):
for book in [Link]:
if [Link] == isbn and not [Link]:
[Link] = True
return f"You returned: {[Link]}"
return "Invalid return"
def list_books(self):
return [str(book) + (" - Available" if [Link] else " - Borrowed") for book in
[Link]]
# Example usage
lib = Library()
lib.add_book(Book("1984", "George Orwell", "123"))
lib.add_book(Book("Python Basics", "John Doe", "456"))
print(lib.list_books())
print(lib.borrow_book("123"))
print(lib.list_books())
print(lib.return_book("123"))
2. Student Grade Management System
Handles multiple students, subjects, and calculates averages.
class Student:
def __init__(self, name):
[Link] = name
[Link] = {}
def add_grade(self, subject, grade):
[Link][subject] = grade
def average(self):
return sum([Link]()) / len([Link]) if [Link] else 0
def __str__(self):
return f"{[Link]} - Avg: {[Link]():.2f}"
class School:
def __init__(self):
[Link] = []
def add_student(self, student):
[Link](student)
def report(self):
return [str(student) for student in [Link]]
# Example usage
s1 = Student("Alice")
s1.add_grade("Math", 90)
s1.add_grade("Science", 85)
s2 = Student("Bob")
s2.add_grade("Math", 70)
s2.add_grade("Science", 75)
school = School()
school.add_student(s1)
school.add_student(s2)
print([Link]())
3. Banking System Simulation
Supports deposits, withdrawals, and transfers.
class Account:
def __init__(self, owner, balance=0):
[Link] = owner
[Link] = balance
def deposit(self, amount):
[Link] += amount
return f"{[Link]} deposited {amount}. Balance: {[Link]}"
def withdraw(self, amount):
if amount > [Link]:
return "Insufficient funds"
[Link] -= amount
return f"{[Link]} withdrew {amount}. Balance: {[Link]}"
def transfer(self, other, amount):
if amount > [Link]:
return "Insufficient funds"
[Link] -= amount
[Link] += amount
return f"Transferred {amount} from {[Link]} to {[Link]}"
# Example usage
acc1 = Account("Alice", 500)
acc2 = Account("Bob", 300)
print([Link](200))
print([Link](100))
print([Link](acc2, 250))
print([Link], [Link])
4. Tic-Tac-Toe Game (with AI)
A playable console game with simple AI.
import random
def print_board(board):
for row in board:
print(" | ".join(row))
print("-" * 5)
def check_winner(board, player):
for row in board:
if all(cell == player for cell in row):
return True
for col in range(3):
if all(board[row][col] == player for row in range(3)):
return True
if all(board[i][i] == player for i in range(3)) or all(board[i][2-i] == player for i in
range(3)):
return True
return False
def tic_tac_toe():
board = [[" "]*3 for _ in range(3)]
players = ["X", "O"]
turn = 0
while True:
print_board(board)
player = players[turn % 2]
if player == "X":
row, col = map(int, input("Enter row and col (0-2): ").split())
else:
row, col = [Link](0,2), [Link](0,2)
while board[row][col] != " ":
row, col = [Link](0,2), [Link](0,2)
if board[row][col] == " ":
board[row][col] = player
if check_winner(board, player):
print_board(board)
print(f"{player} wins!")
break
if all(cell != " " for row in board for cell in row):
print_board(board)
print("It's a draw!")
break
turn += 1
else:
print("Invalid move, try again.") # Run game # tic_tac_toe()