Practical 2
Practical 2
S.NO PROGRAM
1. Program for hospital dedicated to covid cases
2. Program for payroll management
3. Program for plant database management
4. Program for registration and login form
5. Program to display each word of a text file separated by #
6. Program to display no. of vowels, consonants uppercase,
lowercase in a text file
7. Program to create a binary file with name and roll no. and
search name with roll no.
8. Program to create a binary file with roll no., name, marks. Input
a roll no. and update marks
9. To remove all lines in text file that contain ‘a’ and write it onto
other files
10. Program to generate a random number between 1 and 6
11. Program for stack implementation using a list
12. Program to find most recurring word in a text
13. Check whether a number is a perfect square, Armstrong no. or
palindrome
14. Program to create a CSV file by entering a user ID and password
and searching for the password for the given user ID.
1
15. Program to check whether an inputted string is a palindrome or
not and swap the case of characters in a string
16. Program to find maximum and minimum no. in a list/tuple.
17. Program to input a list of numbers and swap elements at even
locations with those at odd locations.
18. Program to input a list/tuple and search for the elements in list/tuple
19. Input list of no. and check if the no. is sum of the cubes of their
digits and to find the smallest and largest number in list.
20. Program to create dictionary with roll no., name, marks and
display name of students with marks more than 75.
2
1. Program for covid dedicated hospital
Code:
import mysql.connector as con
mydb=con.connect(host='localhost',user='root',password='1234',database='hospi
tal',charset='utf8')
mycur=mydb.cursor()
while True:
a=int(input("""HOSPTAL PATIENT DETAIL
MENU
1. ADD RECORD
3. SEARCH RECORD
4. DELETE RECORD
5. EXIT"""))
if a==1:
3
e=int(input("Enter patient id:"))
n=input("Enter patient Name:")
s=int(input("enter room number:"))
p=int(input("Enter phone number:"))
d=input("Enter doctor's name:")
query='insert into hospital values({},"{}",{},{},"{}")'
mycur.execute(query.format(e,n,s,p,d))
mydb.commit()
if a==2:
l = int(input("Enter patient id: "))
m = int(input("Enter the updated room no.: "))
query = 'UPDATE hospital SET room = {} WHERE id = {}'
mycur.execute(query.format(m, l))
mydb.commit()
if a==3:
n=int(input("Enter id:"))
query='select * from hospital where id={}'
mycur.execute(query.format(n))
for i in mycur:
print(i)
if a==4:
r=int(input("Enter the id to be deleted:"))
query='Delete from hospital where id={}'
mycur.execute(query.format(r))
4
if a==5:
break
mydb.commit()
Output:
5
Table:
6
2. Program for payroll management
Code:
import mysql.connector as con
mydb=con.connect(host='localhost',user='root',password='1234',database='payro
ll',charset='utf8')
mycur=mydb.cursor()
while True:
a=int(input("""PAYROLL MANAGEMENT
MENU
1. ADD RECORD
2. UPDATE RECORD
3. EXIT"""))
if a==1:
e=int(input("Enter employee id"))
n=input("Enter employee Name")
s=int(input("enter salary"))
p=int(input("Enter pf"))
query='insert into cash values({},"{}",{},{})'
mycur.execute(query.format(e,n,s,p,id))
mydb.commit()
if a==2:
n=int(input("""DO YOU WANT TO UPDATE
7
1.Name
2.Salary
3.pf
"""))
if n==1:
l=int(input("Enter eployee id to be updated"))
i=input("Enter the name you want to updated to")
query='update cash set name="{}" where id={}'
mycur.execute(query.format(i,l))
mydb.commit()
if n==2:
l=int(input("Enter eployee id to be updated"))
i=input("Enter the salary you want to updated to")
query='update cash set salary={} where id={}'
mycur.execute(query.format(i,l))
mydb.commit()
if n==3:
l=int(input("Enter eployee id to be updated"))
i=input("Enter the pf you want to updated to")
query='update cash set name="{}" where id={}'
mycur.execute(query.format(i,l))
if n==4:
break
mydb.commit()
8
Output:
Table:
9
3. Program for plant Database management
Code:
import mysql.connector as con
mydb=con.connect(host='localhost',user='root',password='',database='plant',char
set='utf8')
mycur=mydb.cursor()
while True:
a=int(input("""PLANT DATABASE
MENU
1. ADD RECORD
2. UPDATE NAME
3. SEARCH RECORD
4. DELETE RECORD"""))
if a==1:
e=int(input("Enter plant id"))
n=input("Enter plant Name")
s=input("enter biological name")
p=input("Enter origin")
query='insert into Plant values({},"{}","{}","{}")'
mycur.execute(query.format(e,n,s,p))
mydb.commit()
if a==2:
l = int(input("Enter plant id: "))
m = input("Enter the updated name ")
10
query = 'UPDATE plant SET name = "{}" WHERE id = {}'
mycur.execute(query.format(m, l))
mydb.commit()
if a==3:
n=int(input("Enter id:"))
query='select * from plant where id={}'
mycur.execute(query.format(n))
for i in mycur:
print(i)
if a==4:
r=int(input("Enter the id to be deleted:"))
query='Delete from plant where id={}'
mycur.execute(query.format(r))
mydb.commit()
11
Output:
12
13
4. Program for registration and login form
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",password="1234",
database="login")
mycursor=mydb.cursor()
def register():
u=input("enter username")
p=input("enter password")
n=int(input("enter phone number"))
mycursor.execute("insert into log values('{}','{}',{}".format(u,p,n))
mydb.commit()
print("registered succesfully")
def login:
u=input("enter username")
e=input("enter password")
mycursor.execute("select * from log where username='{}'".format(u))
14
data=mycursor.fetchall()
for i in data:
if i[0]==u or i[1]==e:
print("login succesfully")
else:
print("invalid username or password")
#menu
while True:
print("press 1 to
register") print("press 2
to login") ch=input("enter
choice") if ch=="1":
register()
elif ch=="2":
login()
else:
print("wrong choice, try again")
Output:
15
Table:
16
5. Display each word of text file separated by ‘#’
Text file:
Code:
f=open('text.txt','r')
a=f.readlines()
for i in a:
x=i.split()
for j in x:
print(j,end='#')
f.close()
Output
17
6. Program to display no. of vowels, consonants
uppercase, lowercase in a text file
Text file:
Code:
f=open('text.txt')
x=f.read()
v=c=u=l=0
for i in x:
if i.isalpha():
if i.isupper():
u=u+1
else:
l=l+1
if i in "AEIOUaeiou":
v=v+1
else:
c=c+1
print(x)
print("total no. of vowels:", v)
print("total no. of consonants:",c)
18
print("total no. of uppercase:", u)
Output:
19
7. Program to create a binary file with name and roll no.
and search name with roll no.
Code:
import pickle as pk
f=open("student.dat","wb")
lst=[]
while True:
r=int(input("enter roll no.:"))
n=input("enter name:")
lst=[r,n]
pk.dump(lst,f)
ch=input("add more?")
if ch=='no':
break
f.close()
f=open("student.dat","rb")
r=int(input("enter roll no. to search:"))
while True:
x=pk.load(f)
if x[0]==r:
print(x)
break
20
Output:
21
8. Program to create a binary file with roll no., name,
marks. Input a roll no. and update marks
Code:
import pickle as pk
f=open("school.dat","wb")
lst=[]
while True:
r=int(input("enter the roll no.:"))
n=input("enter the name:")
m=int(input("enter marks:"))
lst=[r,n,m]
pk.dump(lst,f)
ch=input("add more?")
if ch=='no':
break
f.close()
f=open("school.dat","rb+")
m=int(input("enter the roll no. to update marks:"))
while True:
a=f.tell()
x=pk.load(f)
if x[0]==m:
m=int(input("enter updated marks:"))
22
x[2]=m
f.seek(a)
pk.dump(x,f)
print("record updated")
break
f.close()
f=open("school.dat","rb")
try:
while True:
x=pk.load(f)
print(x)
except:
f.close()
Output:
23
24
9. To remove all lines in text file that contain ‘a’ and write
it onto other files
Text file:
Code:
f=open('poem.txt','w+')
c=f.readlines()
for i in c:
print(i)
a=open('poem.txt','w')
b=open('copy.txt','w')
for i in c:
if 'a' in i:
b.write(i)
else:
a.write(i)
a.close()
b.close()
25
Output:
26
10. Program to generate a random number between
1 and 6
Code: import
random
a=random.randint(1,6)
print(a)
Output:
27
11. Program for stack implementation using a list
Code:
stk=[]
def push():
x=input("enter name:")
stk.append(x)
def pop():
if len(stk)==0:
print("underflow")
else:
a=stk.pop()
print("record deleted of",a)
def display():
if stk==[]:
print("empty stack")
else:
for i in range(len(stk)-1,-1,-1):
print(stk[i])
while True:
print("""1. add
2. delete
3. display
4. exit""")
28
x=int(input("enter choice:"))
if x==1:
push()
display()
elif x==2:
pop()
elif x==3:
display()
elif x==4:
exit()
Output:
29
30
12. Program to find most recurring word in a text
Text file:
Code:
f=open('text.txt','r')
a=f.read()
x=a.split()
d={}
a=None
mc=0
for i in x:
for i in d:
d[i]=d[i]+1
else:
d[i]=1
if d[i]>mc:
a=i
mc=d[i]
print("most common word is:",a)
Output:
31
32
13. Check whether a number is a perfect square,
Armstrong no. or palindrome
Code:
x=int(input("enter number to check"))
p=0
for i in range(1,x):
if i*i==x:
p=1
break
if p==1:
print(x,"is a perfect number")
else:
print(x,"is not a perfect number")
m=x
a=0
while x>0:
r=x%10
x=x//10
a=a+r**len(str(m))
if m==a:
print("number is an armstrong number")
else:
print("number is not an armstrong number")
33
s=str(m)
if s==s[::-1]:
print("number is a pallindrome number")
else:
print("number is not a pallindrome number")
Output:
34
35
14. Program to create a CSV file by entering a user ID and
password and searching for the password for the given
user ID.
Code:
import csv
def write():
f=open("details.csv","w",newline='')
wo=csv.writer(f)
wo.writerow(["UserId","Password"])
while True:
u_id=input("Enter User Id:")
pswd=input("Enter Password:")
data=[u_id,pswd]
wo.writerow(data)
ch=input("Do you want to enter more records (Y/N):")
if ch=='n':
break
f.close()
def read():
f=open("details.csv","r")
ro=csv.reader(f)
for i in ro:
print(i)
36
f.close()
def search():
f=open("details.csv","r")
Found=0
u=input("Enter user - id to search:")
ro=csv.reader(f)
next(ro)
for i in ro:
if i[0]==u:
print(i[1])
Found=1
f.close()
if Found==0:
print("Sorry...No record found..")
write()
read()
search()
Output:
37
38
15. Program to check whether an inputted string is a
palindrome or not and swap the case of characters in a
string.
Code:
s=input("enter a string:")
a=""
s1=s[::-1]
if s==s1:
print(s,"is a pallindrome")
else:
print(s,"is not a pallindrome")
for i in range(len(s)):
if s[i].isupper():
a=a+s[i].lower()
elif s[i].islower():
a=a+s[i].upper()
print("case converted string is:",a)
OUTPUT:
39
40
16. Program to find maximum and minimum no. in a
list/tuple.
Code:
lst=[]
while True:
x=int(input("enter an integer:"))
lst.append(x)
ch=input("add more?")
if ch=='no':
break
print(lst)
print("largest value of list is:",max(lst))
print("smallest value of list is:",min(lst))
tup=()
while True:
x=int(input("enter an integer:"))
tup=tup+(x,)
ch=input("Add more?")
if ch=='no':
break
print(tup)
print("largest value of tuple is:",max(tup))
print("smallest value of tuple is:",min(tup))
41
Output:
42
17. Program to input a list of numbers and swap
elements at even locations with those at odd locations.
Code:
Ist=[]
x=int(input("Enter number of elements in the list: "))
for i in range(x):
v=input("Enter list element: ")
Ist.append(v)
print("Original list: ",Ist)
for i in range(0, len(Ist)-1, 2):
Ist[i], Ist[i+1] = Ist[i+1], Ist[i]
print("List after swapping elements at even and odd locations:", Ist)
Output:
43
18. Program to input a list/tuple and search for the elements in
list/tuple
Ist=0
x=int(input("Enter the number of elements in list you want to
add: "))
for i in range(x):
a=input("Enter element: ")
Ist.append(a)
print(Ist)
b=input("Enter element you want to search: ")
for i in Ist:
if i==b:
print("The element",b," is present at index:
",Ist.index(b))
elif i not in Ist:
print("Element not found in list")
Output:
44
45
19. Input list of no. and check if the no. is sum of the
cubes of their digits and to find the smallest and
largest number in list.
Code:
n=int(input("how many numbers do you want to add?"))
l=[]
for i in range(n):
nu=int(input("enter a number:"))
l.append(nu)
for i in l:
an=0
for d in str(i):
an+=int(d)**3
if an==i:
print(i,"is equal to the sum of the cube of the digits")
ln=sn=l[0]
for i in l:
if i>ln:
ln=i
elif i<sn:
sn=i
print("the largest number is:",ln)
print("the smallest number is:",sn)
46
Output:
47
20. Program to create dictionary with roll no., name,
marks and display name of students with marks
more than 75.
Code:
d={}
n=int(input("enter how many records to enter:"))
for i in range(n):
r=int(input("enter roll no:"))
n=input("enter name")
m=int(input("enter marks"))
rec=[n,m]
d[r]=rec
print('dictionary:',d)
for i in d.keys():
if d[i][1]>75:
print("student with marks more than 75 are:",d[i][0])
Output:
48
49
MYSQL QUERIES
Q1. Consider the table teacher. Write the command in
SQL for (1)to(6) and output in (7) to (10)
create table teacher(ID integer primary key,NAME varchar(30),DEPARTMENT
varchar(30),HIRE DATE date,CATEGORY varchar(5),GENDER char(1),SALARY
integer);
50
2. To list the name and salary of male teachers of
English department
51
5. Add a new column Aadhar No. of the table
52
9. Select count(*) from teacher where category =’PGT’
and salary>25000;
53
Q2. Consider the table flights and fares. Write SQL
commands from the statement (1) to (4) and give
outputs for SQL queries (5)to(7)
create table flight(FNO varchar(10) primary key,SOURCE
varchar(30),DESTINATION varchar(30),NO_OF_FL integer,NO_OF_STOP
integer);
create table fares(FNO varchar(10) primary key,AIRLINES varchar(30),FARE
integer,TAX_PERCENT integer);
insert into flight values('AM812','LUCKNOW','DELHI',4,0);
insert into fares values('IC301','INDIAN AIRLINES',9425,5);
54
1. Display flight number and number of flights from
Mumbai from the flight table
55
3. Increase tax percentage by 2% from flights starting
from delhi
56
7. Select flight.FNO,NO_OF_FL.AIRLINES from
flights,fares, where source=’DELHI’ and
flights.FNO=fares.FNO;
57
Q3. Write SQL command for (1)to(4) and output for (5)
on the basis of table LAB
create table lab (NO integer primary key,ITEM_NAME varchar(30),COST
integer,QTY integer,DATEOFPURCHASE date,WARRANTY integer,OPERATIONAL
integer);
insert into lab values(1,'COMPUTER',45000,9,'1996-05-21',2,7);
58
3. Query to list the name in descending order of date
of purchase where quantity is more than 3
5. Give Output
1. Select min(distinct QTY)from lab;
59
2. Select min(WARRANTY) from lab where QTY=2;
60
4. Select avg(COST) from lab where
DATEOFPURCHASE<’1/1/1999’;
61
Q4. Consider the following tables faculty and courses
Write SQL queries for (1) to (4) and output for queries (5)
and (6):
create table faculty(F_ID integer primary key,FNAME varchar(30),LNAME
varchar(30),HIRE_DATE date,SALARY integer);
create table courses(C_ID varchar(5) primary key,F_ID integer,CNAME
varchar(30),FEES integer);
insert into faculty values(102,'AMIT','MISHRA','1998-10-12',12000);
insert into courses values('C21',102,'GRID COMPUTING',40000);
62
1. To display details of those faculties whose salary is
greater than 12000
63
2. To display the details of the courses whose fees is in
the range of 15000 to 50000(both included)
64
6. Select min(SALARY) from faculty;
65
8. Select FNAME,INAME from faculty where INAME like
“M%”;
66
Q5. Consider the following tables dress and material
Write SQL queries for (1) to (4) and output for queries (5)
to (8)
67
1. To display DCODE and DESCRIPTION of each dress
in ascending order of DCODE
68
3. To display the average price of all the dresses which
are made up of material with Mcode as M003
69
6. Select description,type,from dress, material where
Dress code=material.code and dress.price>=1250;
70