Practical File
Practical File
Page 1 of 57
# 6. To check whether the string is palindrome
or not
def pal(x):
y=''
for i in range(len(x)-1,-1,-1):
y=y+x[i]
if x==y:
return "palindrome"
else:
return "not palindrome"
Page 2 of 57
# 9. To convert celcius to fahrenheit
def conv(x):
F=((9/5)*x)+32
return F
Page 3 of 57
# 13. To arrange list in ascending order using
insertion sort
def insert(L):
for i in range (1,len(L)):
t=L[i]
j=i-1
while t<L[j] and j>=0:
L[j+1]=L[j]
j=j-1
L[j+1]=t
return L
Page 6 of 57
# 8. To find sum of series
x=int(input("enter the numerator"))
n=int(input("enter the length"))
print("sum=",practical.sumseries(x,n))
Page 7 of 57
for j in range(c):
y=int(input("enter the number"))
row.append(y)
l.append(row)
s,s1=practical.tdsum(l)
print("sum of diagonal 1=",s)
print("sum of diagonal 2=",s1)
Page 8 of 57
output
Page 9 of 57
Page 10 of 57
Page 11 of 57
# File handling
Notes.txt
Text File: A text file stores information in the form of a
stream of ASCII or Unicode characters. In text file, each line
of text is terminated, with a special character known as end
of line character. In text file some internal translation take
place when EOL character is read or written.
Page 12 of 57
# Text files
# 21. To read a file count number of
alphabets, digits and special characters.
def test():
xf=open("notes.txt","r")
x=xf.read()
a=0
b=0
c=0
for i in x:
if i.isalpha():
a=a+1
elif i.isdigit():
b=b+1
else:
c=c+1
print("number of alphabets",a)
print("number of digit",b)
print("number of special character",c)
xf.close()
test()
Output-
Page 13 of 57
# 22. To read a file count number of words
having only three characters.
def count():
xf=open("notes.txt","r")
x=xf.readlines()
c=0
for i in x:
y=i.split()
for j in y:
if len(j)==3:
c=c+1
print("number of words with three characters",c)
xf.close()
count()
Output-
18
Page 14 of 57
Output-
Output-
Page 15 of 57
# Binary Files
# 25. Binary Files program for Adding,
Modification,Search,Display,Deletion.
import os
import pickle
def cls():
print("\n"*100)
def mainmenu():
while 1:
print( "welcome to FashionHUB".center(45))
print("1. item details")
print("2. exit")
ch= int(input("enter your choice(1-3) : "))
if ch==1:
item_details()
elif ch==2:
print("end of program")
break
else:
print("wrong choice")
def item_details():
while 1:
cls()
print("1. addition")
print("2. modification")
print("3. search")
print("4. deletion")
print("5. display report")
print("6. exit")
ck= int(input("enter your choice"))
if ck==1:
add()
Page 16 of 57
elif ck==2:
update()
elif ck==3:
search()
elif ck==4:
remove()
elif ck==5:
report()
elif ck==6:
return
else:
print("wrong choice")
def add():
cls()
xf=open("lakshay.dat","wb")
d={}
ab='y'
while ab=='y':
no= int(input("enter the item no. : "))
desc= input("enter item description : ")
qty=int(input("enter quantity of item :"))
rate=float(input("enter the price of item"))
amt=qty*rate
d['item no']=no
d['description']=desc
d['quantity']=qty
d['rate']=rate
d['amount']=amt
pickle.dump(d,xf)
ab=input("add more records[y/n] : ")
xf.close()
def update():
cls()
xf=open("lakshay.dat","rb+")
Page 17 of 57
d={}
K=True
n=int(input("enter the item no. to be updated"))
try:
while K:
k=xf.tell()
d=pickle.load(xf)
if n in d.values():
print(d)
print("1. update quantity of item")
print("2. update rate of item")
ab=int(input("enter your choice(1-2):"))
if ab==1:
q=int(input("enter the new quantity of item"))
d['quantity']=q
elif ab==2:
r=int(input("enter new item rate"))
d['rate']=r
else:
print("wrong choice")
xf.seek(k)
amt=d['quantity']*d['rate']
d['amount']=amt
print(d)
pickle.dump(d,xf)
K=False
except EOFError:
xf.close()
def search():
cls()
xf=open("lakshay.dat","rb")
d={}
n=int(input("enter a number"))
try:
while True:
Page 18 of 57
d=pickle.load(xf)
if n in d.values():
print(d)
except EOFError:
xf.close()
def report():
cls()
xf=open("lakshay.dat","rb")
d={}
xf.seek(0)
try:
while True:
d=pickle.load(xf)
print(d)
except EOFError:
xf.close()
def remove():
cls()
xf=open("lakshay.dat","rb")
xy=open("new.dat","wb")
d={}
n=int(input("enter a item no number"))
try:
while True:
d=pickle.load(xf)
if n not in d.values():
pickle.dump(d,xy)
except EOFError:
xf.close()
xy.close()
os.remove("lakshay.dat")
os.rename("new.dat","lakshay.dat")
mainmenu()
Page 19 of 57
Output –
Page 20 of 57
Page 21 of 57
Page 22 of 57
Page 23 of 57
Page 24 of 57
#CSV files
# 26. To create a csv file to store student
data (roll no.,name, marks) obtain data from
user and write five records on the file.
import csv
def create():
xf=open("student.csv","w")
obj= csv.writer(xf,delimiter=':')
obj.writerow(["roll no","Name","marks"])
ab='y'
while ab=='y':
r=int(input("enter roll no."))
n=input("enter name")
p=float(input("enter marks"))
obj.writerow([r,n,p])
ab= input("add more records [y/n] :")
xf.close()
create()
Output-
Page 25 of 57
# 27.To display the contents of csv file
student.csv.
import csv
def read():
xf=open("student.csv","r",newline="\r\n")
rcc=csv.reader(xf,delimiter=':')
for i in rcc:
print(i)
xf.close()
read()
Output-
Page 26 of 57
DATA STRUCTURE
# 28.Write a function lsearch() to pass a list
and a value search the value or item in the list
and display its contents.
def lsearch(l,val):
k=0
for i in l:
if i==val:
k=1
return (k)
l=eval(input("enter the list"))
n=int(input("enter the number to be searched"))
if lsearch(l,n)==1:
print("number is found")
else:
print("number is not found")
Output-
if index:
print("\nelement found at index:",index,",position:",(index+1))
else:
print("\nsorry !! given element could not be found.\n")
Output-
Page 28 of 57
Output-
Output-
Output-
Output-
Page 31 of 57
def pop1(stk):
if stk==[]:
print("underflow")
else:
print("The deleted element",stk.pop())
def display(stk):
if stk==[]:
print("empty stack")
else:
for i in range(len(stk)-1,-1,-1):
print(stk[i])
while True:
print("1. To add a new value to stack")
print("2. To delete a value")
print("3. To display stack")
print("4. Exit")
ch=int(input("enter your choice(1-4)"))
if ch==1:
bno=input("enter book number")
bname=input("enter book name")
value=[bno,bname]
push(stk,value)
elif ch==2:
pop1(stk)
elif ch==3:
display(stk)
elif ch==4:
print("end of program")
break
else:
print("wrong choice")
Output-
Page 32 of 57
Page 33 of 57
# 35.Program to implement queue
operataions
def cls():
print("\n"*100)
def isEmpty(Qu):
if Qu==[]:
return True
else:
return False
def enqueue(Qu,item):
Qu.append(item)
if len(Qu)==1:
front=rear=0
else:
rear=len(Qu)-1
def Dequeue(Qu):
if isEmpty(Qu):
return"underflow"
else:
item=Qu.pop(0)
if len(Qu)==0:
front=rear=None
return item
def peek(Qu):
if isEmpty(Qu):
return"underflow"
else:
front=0
return Qu[front]
def display(Qu):
if isEmpty(Qu):
print("queue empty!")
elif len(Qu)==1:
print(Qu[0],"<==front,rear")
else:
front=0
Page 34 of 57
rear=len(Qu)-1
print(Qu[front],"<-front")
for a in range(1,rear):
print(Qu[rear],"<-rear")
queue=[]
front= None
while True:
cls()
print("queue operations")
print("1.enqueue")
print("2.Dequeue")
print("3.peek")
print("4.Display queue")
print("5.exit")
ch=int(input("enter your choice(1-5):"))
if ch==1:
item=int(input("enter item:"))
enqueue(queue,item)
input("press enter to continue...")
elif ch==2:
item=Dequeue(queue)
if item=="underflow":
print("underflow!Queue is empty!")
else:
print("Dequeue-ed item is",item)
input("press enter to continue....")
elif ch==3:
item=peek(queue)
if item=="underflow":
print("queue is empty!")
else:
print("front most item is",item)
input("press enter to continue....")
elif ch==4:
display(queue)
input("Press enter to continue....")
Page 35 of 57
elif ch==5:
break
else:
print("invalid choice!")
input("press enter to continue....")
Output-
Page 36 of 57
Page 37 of 57
Page 38 of 57
Page 39 of 57
Databases
A database may be defined as a collection of
interrelated data stored together to serve multiple
applications.
Rdms:
In relational data model, the data is organized into
tables (i.e., rows and columns). These are called
relations. A row in a table represents a relationship
among a set of values.
Mysql
SQL, Structured Query Language, was developed in
1970s in an IBM Laboratory. SQL, sometimes also
referred to as SEQUEL is a 4th generation non-
procedural language.
SQl commands:
1.Show databases; It displays all the database, of the
system.
Page 42 of 57
11. Group by; The GROUP BY clause combines all
those records that have identical values in a
particular field or a group of fields.
use practical;
Page 43 of 57
insert into employee_info values(106,'AYUSH VERMA','2005-05-17','2021-12-
24','MDA GAJRAULA','9913979603','MALE','MECHANICAL','CLERK');
Page 44 of 57
insert into salary values(107,45000,00,00,00,00,00);
Page 45 of 57
2.Display the structure of the table employee_info.
➢ describe employee_info;
Page 46 of 57
5.To fetch all the Employees who are managers.
Page 47 of 57
7.To find the maximum, minimum, and average
salary of the employees.
Page 48 of 57
10.show emp_no, emp_name and salary in ascending
order of salary.
Page 49 of 57
13.add column bonus.
Page 50 of 57
16.select emp_id, left(gross_salary,3) from salary;
17. display the names, net salary, gross salary of all the
male employees with their department and
designation.
Page 51 of 57
19.display length of each name.
Page 52 of 57
Interface Python
with MySQL
# 37.Create a database and table through
python mysql interface
import mysql.connector as myc
def create():
if not mycon.is_connected():
print("not connected")
else:
cur=mycon.cursor()
ab='y'
while ab=='y':
Page 53 of 57
ab=input("do you want to add more records [y/n] : ")
mycon.commit()
mycon.close()
create()
Output-
Page 54 of 57
# 38.To display first three rows fetched from
emp table of database employee
import mysql.connector as myc
def display():
mycon=myc.connect (host="localhost", user="root", passwd="1234",
database="employee_details")
if not mycon.is_connected():
print("not connected")
else:
cur=mycon.cursor()
cur.execute("select*from emp")
row=cur.fetchmany(3)
for i in row:
print(i)
mycon.close()
display()
Page 55 of 57
Output-
Output –
Page 56 of 57
# 40. To delete information of a employee
import mysql.connector as myc
def cut():
mycon=myc.connect (host="localhost", user="root", passwd="1234",
database="employee_details")
if not mycon.is_connected():
print("not connected")
else:
cur=mycon.cursor()
a=int(input("enter employee id"))
cur.execute("delete from emp where empid ={}".format(a))
count=cur.rowcount
mycon.commit()
print("No of rows left after deletion of a data :",count)
mycon.close()
cut()
Output –
Page 57 of 57