0% found this document useful (0 votes)
19 views

Python Code For Class XII

python code for class XII

Uploaded by

hc.mehlam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Python Code For Class XII

python code for class XII

Uploaded by

hc.mehlam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

ELECTRICAL AND ELECTRONICS ENGINEERING

MINI PROJECT
PYTHON PROGRAMMING
231GES201T
PROJECT TITLE
LIBRARY MANAGEMENT SYSTEM

DHANUSHKUMAR SM 310623105009

IDRIS SADIQ 310623105018

P JOYSON GUNA SELVAN 310623105019

2023-24
Contents
S.No Title Page Number
1 PROJECT DESCRIPTION 2
2 TOOLS USED 4
3 WORKING 5
SOURCE CODE
4 10

SAMPLE OUTPUTS
5 32

CONCLUSION
6 41

7 FUTURE ENHANCEMENTS 42
8 BIBLIOGRAPHY 43

1
PROJECT DESCRIPTION
This Library Management System is a Python-based application
designed to manage a library's inventory and user data. It leverages
MySQL for data storage and uses the prettytable module for displaying
tabular data. The system provides functionalities for adding, deleting,
updating, and sorting books and user information. Additionally, it
includes features for tracking the lending and returning of books.
Key Features
1. User Authentication:
● Sign-in and sign-up functionality with password protection.
● Admin code required for new user registration.
2. Book Management:

2
● Add Books: Enter book details including author, title,
publication, genre, and status (available or lent out).
● Delete Books: Remove books from the database based on
various criteria (author, title, genre, publication).
● Update Books: Modify book details or update the status when
a book is lent out or returned.
● Sort and Find Books: Sort books based on different attributes
(author, title, genre, etc.) and find specific books using these
attributes.
● View All Books: Display a table of all books in the database.
● Lending Details: Track and display details of books that are
currently lent out, including due dates and user contact
information.
3. User Management:
● Add Users: Enter user information including membership
number, name, address, lent books, and phone number.
● Delete Users: Remove user records from the database.
● Find Users: Search for users based on membership number,
name, address, lent books, or phone number.
● View All Users: Display a table of all users in the database.
4. Database Maintenance:
● Clear Database: Option to delete all records in the database.
5. Command-Line Interface:
● Interactive menu-driven interface for ease of use.
● Clear screen function to enhance readability during user
interactions.

3
TOOLS USED

Language
● Python

Modules
● mysql.connector: To connect to and interact with the MySQL

database.
● prettytable: To format and display data in a table format.

● datetime: To handle date and time operations.

● subprocess: To run system commands, such as clearing the screen.

● time: To introduce delays for better user experience.

4
Functions
● clear_screen(): Clears the console screen using a system

command.
● enterarow(): Adds new book entries to the database.

● deleterow(): Deletes book entries from the database based on user-

specified criteria.
● sort(): Sorts and displays book records based on user-selected

criteria.
● clear(): Drops the entire database to clear all records.

● find(): Finds and displays specific book records based on user

input.
● replace(): Updates book details in the database.

● goneout(): Displays books that are currently lent out.

● update(): Updates the status of books (either marking them as

available or lending them out).


● seetable(): Displays all book records in the database.

● menu(): Main menu function for navigating through different

options in the application.


● User(): Manages user-related operations including adding,
deleting, and finding users.
● seeuser(): Displays all user records in the database.

Software
● Microsoft Visual Studio Code (VS Code): Used as the Integrated

Development Environment (IDE) for writing and debugging the


Python code.
Summary
The project leverages Python as the primary programming language,
incorporating several modules for database connectivity, table

5
formatting, date/time management, and system operations. Key
functionalities are implemented through a set of defined functions, each
responsible for specific operations related to book and user management.
Microsoft Visual Studio Code (VS Code) was utilized for code
development and debugging, providing a robust environment for
managing the project.

WORKING

About Library Management System:


A Library Management System (LMS) is a software application
designed to help libraries manage their resources and operations
efficiently. This system typically includes functionalities for managing
books, tracking users, lending and returning books, and maintaining an
organized catalog. Below is an overview of the Library Management
System you developed:
Objectives
● Organize Book Inventory: Keep track of all the books in the
library, including their details and status.
● Manage User Information: Maintain records of library members,
including their personal details and borrowed books.

6
● Facilitate Lending and Returning: Streamline the process of
lending books to users and handling their returns.
● Generate Reports: Provide insights into the library's operations,
such as which books are borrowed and the due dates.
Key Features
1. User Authentication:
● Sign-In and Sign-Up: Users can sign in or register as new
members. Registration requires an admin code to ensure only
authorized users can create new accounts.

2. Book Management:
● Add Books: Administrators can add new books to the library
database by entering details such as the author, title,
publication, genre, and status.
● Delete Books: Books can be removed from the system based
on criteria like author, title, genre, or publication.
● Update Books: Information about books can be updated,
including changing their status when they are lent out or
returned.
● Sort and Find Books: Books can be sorted by various
attributes (author, title, genre, etc.) and searched using these
criteria.
● View All Books: Administrators can view a comprehensive
list of all books in the library, including details about their
status.
3. User Management:

7
● Add Users: New users can be added to the system, recording
their membership number, name, address, lent books, and
phone number.
● Delete Users: Users can be removed from the system.
● Find Users: Administrators can search for users based on
various attributes.
● View All Users: A complete list of all users and their
information can be viewed.
4. Lending and Returning:
● Lend Books: Books can be marked as lent out, recording the
lending date, due date, and the user who borrowed the book.
● Return Books: When books are returned, their status is
updated to available.
5. Reporting:
● Books Lent Out: A report can be generated to show which
books are currently lent out, including details of the
borrowers and due dates.
Working of the system:
Imagine you're a librarian using a command-line Library Management
System (LMS) to manage your library's books and users. Here's a quick
overview of how you'd interact with the system.
Getting Started
1. Launch the Program:
● Prompt to sign in or sign up.
2. Sign In or Sign Up:
● Sign In: Enter your username and password.

8
● Sign Up: Enter the admin code, then create a username and
password.
Main Menu
Once signed in, you see the main menu:
1. User Related Formatting
● Add, delete, or find users.
2. Table Related Formatting
● Add, delete, or update book information.
● Change book status (available or lending).
3. Access the Tables
● Sort or find book information.
● View lent books.
● View all books or users.
4. Clear Database
● Delete all book and user records.
5. Exit
User Related Formatting
● Add User: Enter details like membership number, name, address,
books lent, and phone number.
● Delete User: Enter the name of the user to delete.
● Find User: Search by membership number, name, address, lent
books, or phone number.
Table Related Formatting

9
● Add Book: Enter details like author name, book title, publication,
genre, and status.
● Delete Book: Delete by author name, book title, genre, or
publication.
● Update Book: Change author, title, publication, or genre.
● Change Status: Mark book as lent or returned.
Accessing the Tables
● Sort Books: By author name, book title, genre, e_no, publication,
or availability.
● Find Books: By e_no, author name, book title, genre, or
publication.
● View Lent Books: List of lent books with return dates and
borrower info.
● View All Books: Display all books.
● View All Users: Display all users.
Clear Database
● Delete all records in the database.
Exit
● Quit the program.
Summary
● Start the Program: Sign in or sign up.
● Main Menu: Manage users, books, view tables, clear database, or
exit.
● User Management: Add, delete, or find users.
● Book Management: Add, delete, update, or change status of books.

10
● View Tables: Sort and find books or users, view lent books.
● Clear Database: Delete all records.
● Exit: Quit the program.
This LMS is designed to make managing a library's books and users
easy with simple text inputs and organized menus.

SOURCE CODE
import mysql.connector
import time
import sys,subprocess
from prettytable import PrettyTable
from datetime import datetime,timedelta
def clear_screen():
subprocess.run('cls',shell=True)
def enterarow():# to write rows
clear_screen()
n=1
while n==1:
E=int(input('Enter e_no of the book which you want to add:'))

11
an=input('Enter The Author\'s Name:')
bn=input('Enter The Book\'s Name:')
pn=input('Enter The Publication:')
g=input('Enter The Genre:')
s=input('enter status as available or lending')
if s=='lending':
god=input("enter date of lending in dd-mm-yyyy format")
userph=input("enter the phone no. of the person book lent to")
gof=datetime.strptime(god,'%d-%m-%Y')
fog=gof + timedelta(days=15)
dday=fog.strftime('%Y-%m-%d')

cur.execute("insert into
shelf(e_no,author_name,book_name,publication,genre,status,godate,due
date,user)values\
(%s,%s,%s,%s,%s,%s,%s,%s,%s)",
(E,an,bn,pn,g,s,god,dday,userph))

else:
cur.execute("insert into
shelf(e_no,author_name,book_name,publication,genre,status)
values(%s,%s,%s,%s,%s,%s)",(E,an,bn,pn,g,s))
con.commit()
print('Row Added')
print('_'*60)

12
n=int(input('\nTo continue adding rows press 1,press any key to
continue'))
print('_'*60)

def deleterow():# to delete a record


clear_screen()
n=1
while n==1:
an=int(input('Type 1 to delete a record using author\'s name\nType
2 to delete a record using book name\nType 3 to delete a record using
genre\
\nType 4 to delete a record using publication\nEnter
value: '))
clear_screen()

if an ==1:
a=input('Enter the name of the author whose book you want to
delete')
cur.execute('delete from shelf where author_name=%s',(a,))
print('Row Deleted')
elif an==2:
b=input('Enter the name of the book you want to delete')
cur.execute('delete from shelf where book_name=%s',(b,))
print('Row Deleted')

13
elif an==3:
c=input('Enter the Genre of the book you want to delete')
cur.execute('delete from shelf where genre=%s',(c,))
print('Row Deleted')
elif an==4:
d=input('Enter the publication of the book you want to delete')
cur.execute('delete from shelf where publication=%s',(d,))
print('_'*60)
n=int(input('\nTo continue making changes press 1,press any key
to continue'))
print('_'*60)
clear_screen()

con.commit()
def sort():# sorts
clear_screen()
n=int(input('If you want to sort the books in alphabetical order of their
Author\'s Name, Type 1\
\nIf you want to sort the books in alphabetical order of their
name, Type 2\
\nIf you want to sort the books by genre, Type 3\nIf you want
to sort the books by ecode, Type 4\
\nIf you want to sort the books by publication, Type 5\
\nif you want to sort the books in order of availibility ,type 6\
nEnter Value'))

14
clear_screen()
if n==1:
cur.execute('select * from shelf order by author_name')
data=cur.fetchall()
for x in data:
print(x)
elif n==2:
cur.execute('select * from shelf order by book_name')
data=cur.fetchall()
for x in data:
print(x)
elif n==3:
cur.execute('select * from shelf group by genre')
data=cur.fetchall()
for x in data:
print(x)
elif n==4:

cur.execute('select * from shelf order by e_no')


data=cur.fetchall()
for x in data:
print(x)

15
elif n==5:
cur.execute('select * from shelf group by publication')
data=cur.fetchall()
for x in data:
print(x)
elif n==6:
cur.execute("select * from shelf where status in('available')")
data=cur.fetchall()
for x in data:
print(x)

else:
print('Please Enter Valid Data')

con.commit()
def clear():#to delete everything in the database
clear_screen()
cur.execute('drop database library')
print("database deleted")
con.commit()

def find():#to find

16
clear_screen()
n=int(input('Type 1 to find using eno.\nType 2 to find using author\'s
name\nType 3 to find using book name\
\ntype 4 to find using publication\ntype 5 to find using genre\
nEnter Value: '))
clear_screen()
if n==1:
s=int(input('Enter the e_no you want to find'))
cur.execute('select * from shelf where e_no=%s ',(s,))
data=cur.fetchall()
for x in data:
print(x)

elif n==2:
s=(input('Enter the author you want to find'))
cur.execute('select * from shelf where author_name=%s ',(s,))
data=cur.fetchall()
for x in data:
print(x)

elif n==3:
s=(input('Enter the book you want to find'))
cur.execute('select * from shelf where book_name=%s',(s,) )
data=cur.fetchall()

17
for x in data:
print(x)

elif n==4:
s=(input('Enter the Publication to be searched'))
cur.execute('select * from shelf where publication=%s',(s,))
data=cur.fetchall()
for x in data:
print(x)
elif n==5:
s=(input('Enter the Genre you want to find'))
cur.execute('select * from shelf where genre=%s',(s,) )
data=cur.fetchall()
for x in data:
print(x)
else:
print('Please Enter Valid Data')
con.commit()

def replace():# replaces using book name


clear_screen()

18
s=input('Enter the bookname of the row to which you want to make
changes')
n=int(input('Type 1 if you want to make changes in the name of the
Author\nType 2 if you want to make changes in the name of the book\
\nType 3 if you want to make changes in publication\nType 4
if you want to make changes in genre\nEnter Value: '))
clear_screen()
global p
if n==1:
p=input('Enter the new Author\'s name: ')
cur.execute('update shelf set author_name=%s where book_name=
%s',(p,s))

elif n==2:
p=input('Enter the new Book\'s name: ')
cur.execute('update shelf set book_name=%s where book_name=
%s',(p,s))

elif n==3:
p=input('Enter the new Publication: ')
cur.execute('update shelf set publication=%s where book_name=
%s',(p,s))

elif n==4:
p=input('Enter the new name of the Genre')

19
cur.execute('update shelf set genre=%s where book_name=%s',
(p,s))

else:
print('Invalid Choice')
print("the changes made is as shown" )
cur.execute("select * from shelf where book_name=%s",(p,))
data=cur.fetchone()
print(data)
con.commit()

def goneout():
clear_screen()
cur.execute("select book_name,godate,duedate,user from shelf where
status in('lending')")
data=cur.fetchall()
heading=PrettyTable(['book name','date of lending','due date','user
phone no.'])
heading.add_rows(data)
heading.add_autoindex('Sr.no')
print(heading)

20
def update():
clear_screen()
n=int(input("press 1 for return of book \npress 2 for lending of book\
nresponse:"))
seetable()
if n==1:
b=int(input('chosen e no.'))
cur.execute("update shelf set
status='available',user=0,godate=NULL,duedate=NULL where e_no=
%s",(b,))
print("record updated")
con.commit()
elif n==2:
b=int(input('chosen serial no.'))
c=int(input('enter user phone no.'))
d=input('enter lending date in dd-mm-yyyy')
gof=datetime.strptime(d,'%d-%m-%Y')
fog=gof + timedelta(days=15)
dday=fog.strftime('%Y-%m-%d')
cur.execute("update shelf set status='lending',user=%s,godate=
%s,duedate=%s where e_no=%s",(c,d,dday,b))
print("record updated")
con.commit()
else:

21
print('invalid choice')

def seetable():
clear_screen()
heading=PrettyTable(['e.no','authors name','book
name','publication','genre','status','date of lending','due date','user
ph.no.'])
cur.execute('select * from shelf')
b = cur.fetchall()
heading.add_rows(b)
heading.add_autoindex('Sr.no')
print(heading)

def menu():
clear_screen()
while True:
print('-'*18)
print("MAIN MENU")
print('-'*18)
s=int(input("1.for user related formatting,press 1\n2.for table
related formatting,press 2\
\n3.to access the tables,press 3\
\n4.to clear database,press 4\n5.to exit press 5\nenter value:"))
clear_screen()

22
if s==1:
print('*'*18)
print("SUB MENU")
print('*'*18)
User()
elif s==2:
print('*'*18)
print("SUB MENU")
print('*'*18)
n=int(input('1.enter new book information\n2.delete book
information\n3.change book related information\
\n4.change book status(available or lending)\n5.to return to
main menu\nenter value:'))
if n==1:
enterarow()
elif n==2:
deleterow()
elif n==3:
replace()
elif n==4:
update()
elif n==5:
break
else:

23
print("invalid option")
elif s==3:
print('*'*18)
print("SUB MENU")
print('*'*18)
n=int(input('1.sort the table in desire way\n2.find information in
the table\
\n3.to see the books that have gone for lending\n4.to see all
the books and their data\
\n5.to see all the users and their data\n6.to return to main
menu\n enter value:'))
if n==1:
sort()
elif n==2:
find()
elif n==3:
goneout()
elif n==4:
seetable()
elif n==5:
seeuser()
elif n==6:
break
else:

24
print("invalid option")
elif s==4:
clear()
print('database cleared')
elif s==5:
print('EXITING...')
quit()
else:
print('Invalid Choice')
print('_'*60)
n=input('\nTo exit the menu press 1,press any no. to continue')
print('_'*60)
if n==1:
quit()
else:
continue

con.commit()

def User():
clear_screen()
n=int(input("to enter user information press 1\nto delete user
information press 2\

25
\nto find user specific information press 3\nto exit sub menu
press 4\nenter value:"))
clear_screen()
if n==1:
v=int(input("enter the no. users to add"))
for i in range(v):
E=int(input('Enter membership no. of the member'))
an=input('Enter name of the member ')
bn=input('Enter the address(only the area):')
pn=input('enter the name of the books lent,if no book lent
enter :no books lent:')
g=input('Enter The phone number:')
l=[E,an,bn,pn,g]
cur.execute("insert into
user(member_no,name,address,lent_books,ph_no)values\
(%s,%s,%s,%s,%s)",(E,an,bn,pn,g))
con.commit()
print('Row Added')

menu()
elif n==2:

v=int(input("enter no. of users to delete"))


for i in range(v):

26
a=input('Enter the name of the user whose info you want to
delete')
cur.execute('delete from user where name=%s',(a,))
print('Row Deleted')

menu()

elif n==3:
n=int(input('Type 1 to find using membership no\nType 2 to find
using name\nType 3 to find using address\
\ntype 4 to find using lent books\ntype 5 to find using phone
no.\nEnter Value: '))
if n==1:
s=int(input('Enter the member no.you want to find'))
cur.execute('select * from user where member_no=%s ',(s,))
data=cur.fetchall()
for x in data:
print(x)

elif n==2:
s=(input('Enter the name of the person you want to find'))
cur.execute('select * from user where name=%s ',(s,))
data=cur.fetchall()
for x in data:

27
print(x)

elif n==3:
s=(input('Enter the address of the person you want to find'))
cur.execute('select * from user where address=%s',(s,) )
data=cur.fetchall()
for x in data:
print(x)
elif n==4:
s=(input('Enter the lent books to be searched'))
cur.execute('select * from user where lent_books=%s',(s,))
data=cur.fetchall()
for x in data:
print(x)
elif n==5:
s=(input('Enter the phone no.of the person you want to find'))
cur.execute('select * from user where ph_no=%s',(s,) )
data=cur.fetchall()
for x in data:
print(x)

else:
print('Please Enter Valid Data')

28
elif n==4:
menu()
menu()
con.commit()

def seeuser():

clear_screen()
cur.execute("select * from user")
x=cur.fetchall()
heading=PrettyTable(['membership no.','name','address','books
lent','phone no.'])
heading.add_rows(x)
heading.add_autoindex('Sr.no')
print(heading)

#main prg
print("*****************************************")
print('project name-library management system')
print('*****************************************')
admin="1234"
con=mysql.connector.connect(host='localhost',user='root',password='idri
s',charset='utf8')
cur=con.cursor()

29
cur.execute("create database if not exists library")
cur.execute("use library")
cur.execute("create table if not exists shelf(e_no integer primary
key ,author_name varchar(150),\
book_name varchar(150) unique,publication varchar(150),genre
varchar(150),status varchar(100),godate varchar(10)default NULL,\
duedate varchar(100),user integer default NULL)")
cur.execute("create table if not exists user(member_no integer
unique,name varchar(150),\
address varchar(150),lent_books varchar(150),ph_no integer primary
key)")
while True:
s=int(input("press 1 to sign in\npress 2 to sign up as a new user\nenter
response:"))
clear_screen()
if s==1:
user,password=input("enter username and password").split()
file=open("happy.txt","r")
user_data=file.readline()
x=user_data.split()
for i in x:
if i ==user+password:
print("signed in succesfully")
time.sleep(3)
menu()

30
else:
print("incorrect password or username,try again")
time.sleep(3)
clear_screen()

elif s==2:
a=input("enter the admin code")
if a==admin:
user=input("enter username")
password=input("enter password")
user_data=user+password+" "
file=open("happy.txt","a")
file.write(user_data)
file.close()
print("signed up succesfully")
time.sleep(2)
clear_screen()
else:
print("incorrect admin code,try again")
time.sleep(3)
clear_screen()

31
#end of program

SAMPLE OUTPUTS

32
33
34
35
36
37
38
39
40
CONCLUSION

The Library Management System (LMS) is a comprehensive tool


designed to simplify and streamline the management of library
resources. This project efficiently addresses the core functions of a
library, including cataloging books, managing user information, and
handling book lending processes. By leveraging a structured database
and intuitive command-line interface, the system ensures accurate
record-keeping and easy retrieval of information.
The LMS provides librarians with essential features such as adding new
books, updating book details, deleting records, and tracking the lending
status of books. The user management module allows for the addition,
deletion, and searching of user records, making it easier to maintain up-
to-date membership information. Furthermore, the system includes
functionalities to sort and view books based on various criteria, ensuring
that librarians can quickly access the information they need.
One of the standout features of this system is its ability to manage book
lending efficiently. The system records the date of lending, calculates
due dates, and maintains a record of which user has borrowed which
book. This reduces the likelihood of errors and ensures that books are
returned on time, improving the overall efficiency of library operations.
Overall, the LMS is a robust and reliable solution for managing library
resources. It reduces the administrative burden on librarians, improves
data accuracy, and enhances the user experience by providing quick and
easy access to information. The system’s design and functionality
demonstrate a thoughtful approach to solving common challenges faced
by libraries, making it a valuable tool for any library setting.

41
FUTURE ENHANCEMENTS

To further improve the LMS, consider the following enhancements:


Graphical User Interface (GUI):
Develop a user-friendly GUI to replace the command-line interface,
making the system more accessible and easier to use.
Automated Notifications:
Add functionality to send automated reminders to users about due dates
and overdue books via email or SMS.
Barcode/RFID Scanning:
Incorporate barcode or RFID scanning to automate the check-in and
check-out process, reducing manual entry errors and speeding up
transactions.
Mobile Application:
Develop a companion mobile app for users to browse the catalog, check
their borrowing history, and receive notifications directly on their
smartphones.
Detailed Reporting:
Enhance the reporting capabilities to provide detailed analytics on book
circulation, popular genres, user activity, and other key metrics.
By implementing these enhancements, the LMS can become even more
effective, offering advanced features and greater convenience to both
librarians and users.

42
BIBLIOGRAPHY
● https://www.tutorialspoint.com/python/python_overview.htm
● Computer Science with Python by Sumita Arora
● https://www.digitalocean.com/community/tutorials/python-string-to-datetime-strptime
● https://www.youtube.com/watch?v=dvf9xuK5g5A&t=271s
● Stack Overflow

43

You might also like