Computer Science Practical File: Name - Vivek Bhagat Class - XII-A Roll No - 27

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

COMPUTER SCIENCE PRACTICAL FILE

Name – Vivek Bhagat


Class – XII-A
Roll No – 27
INDEX
 TEXT FILE
 CSV
 BINARY FILE
 STACK
 DICTIONARY
 PYTHON SQL CONNECTIVITY
 LIST
 SQL
 IN BUILT FUNCTIONS
TEXT FILES
# To count number of alphabets in a text file.

# To count number of lines in a text file.


# To count number of digits in a text file

# To print number of uppercase alphabets in a text file


# To count number of words ending with digit in a text file

OUTPUT

 17
 2
 9
 1
 3
CSV
#To add room number ,room status and patient name to
hospital.csv

#To show contents of file hospital.csv


#To search in hospital.csv through room number
#To update contents of hospital.csv
#To get a list of vacant rooms from hospital.csv

import csv import os def add():


f=open('hospital.csv','a',newline='') cw=csv.writer(f)

rno=int(input('Enter Room Number ')) ro=input('Press "Y"


if room occupied and "N" if not ') if ro.upper()=='Y':

name=input('Enter Patient Name ')


cw.writerow([rno,'OCCUPIED',name])
elif ro.upper()=='N': name='N.A'

cw.writerow([rno,'EMPTY',name]) f.close()

def display(): f=open('hospital.csv','r')


cr=csv.reader(f)

for i in cr: print(i[0],i[1],i[2])


f.close() def search():

f=open('hospital.csv','r') cr=csv.reader(f) c=int(input('Enter


Room number to search '))

for i in cr:
print(i[0],i[1],i[2]) flag=1
if flag==0: print('Not found')
f.close()

def update(): f=open('hospital.csv','r')


g=open('h.csv','w',newline='') cr=csv.reader(f)

cw=csv.writer(g)
rn=int(input('Enter room number to update '))

flag=0
for I in cr:
if int(i[0])==rn: flag=1
print('Old record',i)
k=input('Press "Y" if room occupied and "N" if not ')
if k.upper()=='Y': i[1]='OCCUPIED'

i[2]=input('Enter Patient Name ') elif k.upper()=='N':


i[1]='EMPTY'

i[2]='N.A'
cw.writerow(i) else: cw.writerow(i)
f.close() g.close() if flag==0: print('Not
found') else: os.remove('hospital.csv')
os.rename('h.csv','hospital. csv')

def vacant(): f=open('hospital.csv','r')


cr=csv.reader(f)
c=0

for i in cr:
if i[1]=='EMPTY':

c=c+1
print('Room Number',i[0],'is Empty') if c==0:

print('No Room Empty') elif c==1: print('1


Room is Empty') elif c>1: print(c,'Rooms
are Empty')

while True: print('PRESS') print('1.ADD')


print('2.DISPLAY') print('3.SEARCH')
print('4.UPDATE') print('5.LIST OF EMPTY
ROOMS')

print('6.EXIT')

q=int(input('Enter your choice '))


if q==1:
add() elif q==2:
display() elif q==3:
search() elif q==4:
update() elif q==5:
vacant() elif q==6:
break else:
print('INVALID
INPUT')
OUTPUT
BINARY FILE
# Write menu driven program : add,show, search, remove operations on
binary file books.dat
import pickle import os

def add():
f=open('books.dat','ab') while True: bno=int(input('Enter
Book Number ')) bname=input('Enter Book Name ')
n=int(input('Enter Number of book Copies Available

'))
pickle.dump([bno,bname,n],f)
m=input('Want to add more (Y/N) ') if m.upper()=='N':

break f.close()

def show():
f=open('books.dat','rb')
try: while True:
s=pickle.load(f)

print(s[1],'( Book Number',s[0],') has',s[2],'Copies


left') except:

f.close() def search():


f=open('books.dat','rb')
b=int(input("Enter Book Number to Search "))
flag=0

try:
while True:
s=pickle.load(f) if s[0]==b:

flag=1
print(s[1],'( Book Number',s[0],') has',s[2],'Copies left')
except:
if flag==0:
print('Book Not Found') f.close()

def remoove(): f=open('books.dat','rb')


g=open('t.dat','wb') flag=0

k=int(input('Enter Book Number to Remove '))


try:
while True: r=pickle.load(f) if r[0]==k:

print(r[1],'( Book Number',r[0],') has been


removed')
flag=1 else:
pickle.dump(r,g)
except: f.close()

g.close()
if flag==0:
print('Book Not Found')

os.remove("books.dat")
os.rename('t.dat','books.dat')

while True:
print('PRESS') print('1.ADD')
print('2.SHOW')
print('3.SEARCH')
print('4.REMOVE')

print('5.EXIT')

q=int(input('Enter Your Choice'))

if q==1:

add()
elif q==2:
show()
elif q==3:
search()
elif q==4:
remoove()
elif q==5:

break else:

print('Invalid Input')
OUTPUT
STACK
#Write a function in python push(arr), where arr is a list of
numbers.From this list push all numbers divisible by 5 into a stack
implemented by using a list.Display the stack if it has at least one
element otherwise display appropriate error message.Also write
functions for show and pop for the same.

OUTPUT
DICTIONARY
#Write a program to ask roll number and name of n students and print
them

#write a program to ask a string and print occurences of each character


using dictionary
OUTPUT
PYTHON SQL CONNECTIVITY
# Program to insert new record to existing table, display contents
of table and update using python sql connectivity
OUTPUT
LIST
#Write a program to create a list A of N numbers and ask a
value from user. Create another list from list A where
elements are divisible by the value given by user.

#Make changes to a given list by dividing even numbers present in list


by 2
OUTPUT
SQL
DEPT

WORKER

# To display Wno,Name,Gender from the table Worker in descending


order of Wno.
#To display the Name of all the FEMALE workers from the table
WORKER.
#To display the Wno and Name of those workers from the table
WORKER who are born between ‘1987-01-01’ and ‘1991-12-01’.
#To count and display MALE workers who have joined after ‘1986-01-
01’.
OUTPUT
DEPT

WORKER

PREDICT OUTPUT
#SELECT COUNT(*),DCODE FROM WORKER GROUP BY DCODE
HAVING COUNT(*)>1;
#SELECT DISTINCT DEPARTMENT FROM DEPT;
#SELECT NAME, DEPARTMENT,CITY FROM WORKER,DEPT WHERE
W.CODE=D.CODE AND WNO<1003;
#SELECT MAX(DOJ),MIN(DOB) FROM WORKER;
OUTPUT
IN BUILT FUNCTIONS

You might also like