2. KENDRIYA VIDHYALAYA JANAKPURI
ACADEMIC YEAR :- 2022-2023
STUDENT DETAIL
ROLL NO. :- 26633855
NAME :- ARYAN NAGLOT
CLASS :- XII-B
SUBJECT AND CODE :- COMPUTER SCIENCE
3. CERTIFICATE
NAME :- ARYAN NAGLOT
CLASS :- XII-B
ROLL NO. :- 26633855
This is certified to be he Bonafede work of the student in the
Kendriya Vidyalaya Janakpuri during the academic year
2022-2023.
___________________________
SUBJECT TEACHER SIGNATURE
_____________________
EXAMINER’S SIGNATURE
____________________
PRINCIPAL SIGNATURE
4. ACKNOWLEDGEMENT
I would like to thank my informatics practices teacher,
Sonia Sharma mam, whose valuable guidance has been the
ones that have helped me patch this project.
Her suggestions and instructions have served as the major
contributions towards the completion of the project.
Then I would like to thank my parents and friends who
have helped me with their valuable suggestions and
guidance that has been helpful in various phase of the
completion of this project.
~ ARYAN NAGLOT
5. QUESTION BASED PROGRAMS
1. WAP to show functionality of a basic calculator using functions.
def calculator(n1,n2,oper):
if oper=='+':
return n1+n2
elif oper=='-':
return n1-n2
elif oper=='*':
return n1*n2
elif oper=='/':
return n1/n2
elif oper=='//':
return n1//n2
elif oper=='%':
return n1%n2
else:
print('invalid operator..')
return 0
num1=float(input('enter first number:'))
num2=float(input('enter second number:'))
op=input('input operator among +,-,*,/,//,%:')
print(calculator(num1,num2,op))
****************************************************************************************************
2. Write a function in python which accept a number from user to return True, if the number is a prime
number else return False. Use this function to print all prime numbers from 1 to 100.
def checkPrime(n):
for i in range (2, n):
if n%i==0:
return False
else:
return True
l=[]
for i in range(1,101):
ans=checkPrime(i)
if ans==True:
l.append(i)
print('Prime numbers between 1 to 100=',l)
****************************************************************************************************
3. Write a function in python which accept a list of marks of students and return the minimum mark,
maximum mark and the average marks. Use the same function to test.
def checkList(l):
return min(l),max(l),(sum(l)/len(l))
marks=eval(input('enter the marks of students:'))
minList,maxList,avgList=checkList(marks)
print('MAXIMUM MARKS:',maxList)
6. print('MINIMUM MARKS:',minList)
print('AVERAGE MARKS:',avgList)
****************************************************************************************************
4. WAP to read a text file “myfile.txt” line by line and display each word separated by a #.
f=open(r'C:UsersComp_Lab_IIDesktopmyfile.txt')
def show():
try:
while True:
data=f.readline()
data=data.split()
for i in data:
print(i,'#',end='')
except:
f.close()
show()
****************************************************************************************************
5. WAP to read a text file “myfile.txt” and display the number of vowels/ consonants/ uppercase/ lowercase
characters in the file.
def show():
f=open(r'C:UsersComp_Lab_IIDesktopmyfile.txt')
data=f.read()
vowal=0
cons=0
uper=0
low=0
vowalList=['A','E','I','O','U','a','e','i','o','u']
for i in data:
if i.isalpha()==True:
if i in vowalList:
vowal+=1
else:
cons+=1
if i.isupper():
uper+=1
else:
low+=1
print('number of vowels:',vowal)
print('number of consonants:',cons)
print('number of Capital letters:',uper)
print('number of small letters:',low)
show()
****************************************************************************************************
6. Remove all the lines that contain the character `a' in a file and write it to another file.
def copyData():
f=open(r'C:UsersComp_Lab_IIDesktopmyfile.txt')
f2=open(r'C:UsersComp_Lab_IIDesktopmyfile2.txt','w+')
data=f.readlines()
7. for line in data:
if 'A' not in line:
if 'a' not in line:
f2.write(line)
f2.flush()
f2.seek(0)
data2=f2.read()
print(data2)
f.close()
f2.close()
copyData()
OR
def copyData():
f=open(r'C:UsersComp_Lab_IIDesktopmyfile.txt')
f2=open(r'C:UsersComp_Lab_IIDesktopmyfile3.txt','w+')
data=f.readlines()
for line in data:
if 'A' not in line and 'a' not in line:
f2.write(line)
f2.flush()
f2.seek(0)
data2=f2.read()
print(data2)
f.close()
f2.close()
copyData()
****************************************************************************************************
7. Write a program to create a text file and print the lines starting with ‘T’ or ‘P’. (Both uppercase and
lowercase).
def printData():
f=open(r'C:UsersComp_Lab_IIDesktopmyfile.txt')
data=f.readlines()
for line in data:
L1=['t','T','P','p']
if line[0] in L1:
print(line)
f.close()
printData()
****************************************************************************************************
8. Read a text file to print the frequency of the word ‘He’ and ‘She’ found in the file.
def readHeShe():
file=open(r'C:UsersComp_Lab_IIDesktopmyfile.txt')
data=file.read()
cntHe=0
cntShe=0
data=data.split()
8. for i in data:
i=i.lower()
if i=='he':
cntHe+=1
elif i=='she':
cntShe+=1
else:
continue
print('Frequency of "He" in file=',cntHe)
print('Frequency of "She" in file=',cntShe)
readHeShe()
****************************************************************************************************
9. Create a binary file with name and roll number. Search for a given roll number and display the name, if not found
display appropriate message.
import pickle
file=open(r'D:myBinaryFile.bin','ab+')
def inputData():
ans='y'
while ans=='y':
data=eval(input('enter the name and roll number of student:'))
pickle.dump(data,file)
file.flush()
ans=input('want to enter more data? Press Y or N')
ans=ans.lower()
def searchData():
file.seek(0)
try:
found=False
rno=int(input('enter the roll number to search:'))
while True:
data=pickle.load(file)
if data['roll number']==rno:
found=True
nm=data['name']
print('ROLL NUMBER=',rno,' AND NAME=',data['name'])
except:
if found==True:
print('data found..')
else:
print('data not found..')
file.close()
inputData()
searchData()
****************************************************************************************************
10. Create a binary file with roll number, name and marks. Input a roll number and update the marks.
import pickle
file=open(r'D:myBinaryFile2.bin','wb+')
def inputData():
9. ans='y'
while ans=='y':
data=eval(input('enter the name, roll number and marks of student:'))
pickle.dump(data,file)
file.flush()
ans=input('want to enter more data? Press Y or N')
ans=ans.lower()
def updateMarks():
file.seek(0)
try:
rno=int(input('enter the roll number to search:'))
newMarks=int(input('enter new marks'))
while True:
pos=file.tell()
data=pickle.load(file)
if data['roll number']==rno:
data['marks']=newMarks
file.seek(pos)
pickle.dump(data,file)
file.flush()
print('ROLL NUMBER=',data['roll number'],' AND NEW MARKS=',data['marks'])
except:
file.close()
def show():
file=open(r'D:myBinaryFile2.bin','rb+')
try:
while True:
data=pickle.load(file)
print(data)
except:
file.close()
inputData()
updateMarks()
show()
****************************************************************************************************
11. Read a CSV file from hard disc and print all the details on the screen.
def readCSV():
file=open(r'D:saurav.csv',newline='n')
readerObj=csv.reader(file)
for i in readerObj:
print(i)
file.close()
readCSV()
****************************************************************************************************
12. Read a CSV file (containing item no, name, rate, QOH) from hard disc and print all the items whose rate is
between Rs 500 and Rs 1000.
def readCSV():
file=open(r'D:saurav2.csv',newline='n')
10. readerObj=csv.reader(file)
for i in readerObj:
if i[2].isalpha()==True:
continue
else:
if float(i[2])>=500 and float(i[2])<=1000:
print(i[1])
file.close()
readCSV()
****************************************************************************************************
13. Create a CSV file by entering user-id and password, read and search the password for given userid.
file=open(r'D:saurav3.csv','a+',newline='n')
def writeCSV():
writerObj=csv.writer(file)
writerObj.writerow(['user-id','password'])
ans='y'
while ans=='y':
uid=input('enter user id:')
pwd=input('enter password:')
writerObj.writerow([uid,pwd])
file.flush()
ans=input('want to add more info? y or n..')
ans=ans.lower()
print('data saved successfully..')
def search():
file.seek(0)
uid=input('enter the id to search:')
readerObj=csv.reader(file)
for i in readerObj:
if i[0]==uid:
print('password=',i[1])
writeCSV()
search()
file.close()
****************************************************************************************************
14. Write a random number generator that generates random numbers between 1 and 6 (simulates a dice).
Throw the two dices for 10 times and print their total.
def rolladice():
counter = 1
myList = []
while counter <= 10:
randomNumber = random.randint(1,6)
#print('value of 1 dice ',counter,' time=',randomNumber)
randomNumber2=random.randint(1,6)
#print('value of 2 dice ',counter,' time=',randomNumber2)
myList.append(randomNumber+randomNumber2)
counter = counter + 1
11. return myList
# Take user input here
print(rolladice())
****************************************************************************************************
15. WAP in Python to demonstrate linear search.
def LinearSearch():
list1=eval(input('enter the list of elements:'))
element=eval(input('enter the element to search:'))
found=0
for i in list1:
if i==element:
found=1
position=list1.index(i)+1
else:
continue
if found==1:
print('element found in ',position,' position')
else:
print('element not found..')
LinearSearch()
****************************************************************************************************
16. Write a Python program to implement a stack using a list data-structure.
status=[]
def Push_element(list1):
status.append(list1)
def Pop_element():
while len(status)>0:
print(status.pop())
else:
print('Stack Empty..')
for i in range(5):
list1=eval(input('enter the list:'))
Push_element(list1)
Pop_element()
****************************************************************************************************
17. WAP to pass an integer list as stack to a function and push only those elements in the stack which are divisible
by 7.
status=[]
def Push_element(list1):
for i in list1:
if i% 7==0:
status.append(i)
print(status)
list1=eval(input('enter the integer list:'))
Push_element(list1)
****************************************************************************************************
12.
13. CSV TABLE PROGRAM
# CSV TABLES:
import pandas as pd
import matplotlib as pyplot
from datetime import date
print("Welcome to ABC Library")
def addNewMember():
mid=int(input('Enter member id :'))
m_name=input('Enter member name :')
phoneno=int(input('Enter phone number :'))
noofbooksissued=0
mdf=pd.read_csv('CLIBRARYexcelfilesmember.csv')
n=mdf['mid'].count()
mdf.at[n]=[mid,m_name,phoneno,noofbooksissued]
mdf.to_csv('CLIBRARYexcel filesmember.csv,'index=False)
print('New member added')
print(mdf)
def searchMember():
m_name=input('Enter member name:')
bdf=pd.read_csv('C:LIBRARYexcelfilesmember.csv')
df=bdf.loc[bdf['mname']--m_name]
if df.empty:
print('No member found with this name')
else:
print('Members details:')
print(df)
def deleteMember():
mid = float(input('Enter member id :'))
bdf=pd.read_csv('CLIBRARYexcelfilesmembers.csv')
bdf=bdf.drop(bdf[bdf['mid']==mid].index)
bdf.to_csv('C:LIBRARYexcel filesmember.csv',index=False)
print('Member deleted')
print(bdf)
def showMembers():
bdf=pd.read_csv('C:LIBRARYexcelfilesmember.csv')
print(bdf)
def addNewBook():
bookid=int(input('Enter a bok id:'))
title=input('Enter book title:')
author=input('Enter author of the book:')
publisher=input('Enter book publisher:')
14. edition=input('Enter edition of the book:')
cost=input('Enter cost of the book:')
category=input('Enter category of the book:')
bdf=pd.read_csv("C:LIBRARYexcelfilesbook.csv")
n=bdf['bookid'].count()
bdf.at[n]=[bookid,title,author,publisher,edition,cost,category]
bdf.to_csv(r"C:LIBRARYexcelfilesbook.csv",index=False)
print('Book added successfully')
print(bdf)
def searchBook():
title=input('Enter a book name : ')
bdf=pd.read_csv('C:LIBRARYexcelfilesbook.csv')
df=bdf.loc[bdf['title']==title]
if df.empty:
print('no book found with this code')
else:
print('Book details:')
print(df)
def deleteBook():
bookid=float(input('Enter book id :'))
bdf=pd.read_csv(r'C:LIBRARYexcel filesbook.csv')
bdf=bdf.drop(bdf[bdf['bookid']--bookid].index)
bdf.to_csv('C:LIBRARYexcelfilesbook.csv',index=False)
print("Book deleted")
print(bdf)
def showBooks():
bdf=pd.read_csv('C:LIBRARYexcelfilesbook.csv')
print(bdf)
def issueBooks():
book_name=input('Enter book name:')
bdf=pd.read_csv('C:LIBRARYexcelfilesbook.csv')
bdf=bdf.loc[bdf['title']==book_name]
if bdf.empty:
print('No book found in the library')
return
m_name=input('Enter member name :')
mdf=pd.raed_csv('C:LIBRARYexcelfilesmember.csv')
mdf=mdf.loc[mdf['m_name']==m_name]
if mdf.empty:
print('No such member found')
return
dateofissue=int(input('Enter date of issue :'))
noofbookissued=int(input('Enter number of book issued :'))
bdf=pd.read_csv('C:LIBRARYexcelfilesissuebooks.csv')
n=bdf['book_name'].count()
bdf.at[n]=['book_name,m_name',date.today(),noofbookissued,'']
bdf.to_csv('C:LIBRARYexcelfilesissuebooks.csv',index=False)
15. print('Books issued successfully')
print(bdf)
def returnBook():
m_name=input('Enter member name :')
book_name=input('Enter book name :')
idf=pd.read_csv('C:LIBRARYexcelfilesissuebooks.csv')
idf=idf.loc[idf['book_name']==book_name]
if idf.empty:
print('The book is not issued in the records')
else:
idf=idf.loc[idf['m_name']==m_name]
if idf.empty:
print('the book is not issued to the member')
else:
print('Book can be retured')
ans=input('are you sure you want to return the book?')
if ans.lower()=='yes':
idf=pd.read_csv('CLIBRARYexelfilesissuebooks.csv')
idf=idf.drop(idf[idf['book_name']==book_name].index)
idf.to_csv('CLIBRARYexcelfilesissuebooks.csv',index=False)
print('Book Returned')
else:
print('Return operation cancelled')
def showissuedBooks():
idf=pd.read_csv('C:LIBRARYexcelfilesissuebooks.csv')
print(idf)
def deleteissuedBooks():
book_name=input('enter a book name :')
bdf=pd.read_csv('C:LIBRARYexcelfilesissuebooks.csv')
bdf=bdf.drop(bdf[bdf['book_name']==book_name].index)
bdf.to_csv('C:LIBRARYexcelfilesissuebooks.csv',index=False)
print('Delated issued book successfully')
print(bdf)
def showCharts():
print('Press 1- Book and its cost')
print('Press 2- Number of books isssued by members')
ch=int(input('Enter your choice:'))
if ch==1:
df=pd.read_csv('C:LIBRARYexcelfilesbook.csv')
df=df[['title','cost']]
df.plot('title','cost','kind='bar')
plt.xlabel('---title--- ')
plt.ylabel('---cost---')
plt.show()
if ch==2:
df=pd.read_csv('C:LIBRARYexcelfilesissuebooks.csv')
df=df[['noofbooksissued','m_name']]
df.plot(kind='bar',color='red')
plt.show()
16. def login():
uname=input('Enter username :')
pwd=input('Enter password :')
df=pd.read_csv('C:LIBRARYexcelfilesusers.csv')
df=df.loc[df['username']==uname]
if df.empty:
print('Invalid username')
return False
else:
df=df.loc[df['password']==pwd]
if df.empty:
print('Invalid password')
return False
else:
print(':Username and password matched:')
return True
def showMenu():
print('Press 1-Add a new book')
print('Press 2-Search for a book')
print('Press 3-Delete a book')
print('Press 4-Show all books')
print('Press 5-Add a new member')
print('Press 6-Search for a member')
print('Press 7-Delete a book')
print('Press 8-Show all members')
print('Press 9-Isuue a book')
print('Press 10-Return a book')
print('Press 11-Show all isuued book')
print('Press 12-Delete an issued book')
print('Press 13-To view charts')
print('Press 14-To exit')
choice=int(input('Enter your choice: '))
return choice
if login():
while True:
ch=showMenu()
if ch==1:
addNewBook()
elif ch==2:
serchBook()
elif ch==3:
deleteBook()
elif ch==4:
showBooks()
elif ch==5:
addNewMember()
elif ch==6:
searchMember()
elif ch==7:
deleteMember()