ANUSHKA - Program File
ANUSHKA - Program File
DHAULA KUAN
COMPUTER SCIENCE
PRACTICAL FILE
SUBMITTED BY:
Anushka
12E
Roll No. 9
SNO. TOPIC T.SIGN
1. Display all Records using MySQL Connector
2. Add new records using MySQL Connector
3. Delete a particular record using MySQL Connector
4. Update a record using MySQL Connector
5. Search a record using MySQL Connector
6. Write addscore(game) and delscore(game) methods in
python to add and remove score from a list "game",
considering these methods to act as push and pop
operations of data structure stack.
7. Write a function pop() which remove namefrom stack
named "MyStack".
pid,name,cost,quantity
p1, pencil, 5,200
p2, pen,20,150
p3, scale, 10,300
p4, eraser, 10,500
p5, sharpener, 5,250
20. WAP to find how many 'f' and 's' present in a text file
PROGRAM
SCRIPT:
import mysql.connector as m
c=m.connect(host='localhost',user='root',passwd='123',database='d')
q=c.cursor()
q.execute("select * from stationary order by sno")
R=q.fetchall()
print(' '+'_'*32)
print("%s%-6s%-19s%-4s%2s"%('|','sno.','|item','|price','|'))
print('|'+'_'*6+'|'+'_'*18+'|'+'_'*6+'|')
for i in R:
print("%s%-6s%-19s%-4s%4s"%('|',str(i[0]),'|'+str(i[1]),'|'+str(i[2]),'|'))
print('|'+'_'*6+'|'+'_'*18+'|'+'_'*6+'|')
OUTPUT:
SCRIPT:
import mysql.connector as m
c=m.connect(host='localhost',user='root',passwd='123',database='d')
q=c.cursor()
i=[]
r1=int(input("enter sno "))
i.append(r1)
r2=input("enter item ")
i.append(r2)
r3=int(input("enter amount "))
i.append(r3)
R=("insert into stationary values(%s,%s,%s)")
q.execute(R,i)
c.commit()
SCRIPT:
import mysql.connector as m
c=m.connect(host='localhost',user='root',passwd='123',database='d')
q=c.cursor()
i=input("name of item u want to remove ")
R=("delete from stationary where item='{}'".format(i))
q.execute(R)
c.commit()
SCRIPT:
import mysql.connector as m
c=m.connect(host='localhost',user='root',passwd='123',database='d')
q=c.cursor()
i=input("enter the item which u want to update ")
r1=input("enter the new name of item ")
r2=int(input("enter the price "))
R=("update stationary set item='{}',price={} where item='{}'".format(r1,r2,i))
q.execute(R)
c.commit()
5) Search a record using MySQL Connector
SCRIPT:
import mysql.connector as m
c=m.connect(host='localhost',user='root',passwd='123',database='d')
q=c.cursor()
i=input("enter the item name u want to search ")
R=("select*from stationary where item=%s")
v=(i,)
q.execute(R,v)
r=q.fetchall()
for i in r:
print(i)
OUTPUT:
SCRIPT:
game = []
def addscore(game):
n=int(input("enter no.of entries "))
for i in range(n):
sc=int(input("Enter Score"))
game.append(sc)
def pop(s):
if(len(s)>0):
s.pop()
else:
print("Stack is empty")
addscore(game)
print(game)
pop(game)
print(game)
OUTPUT:
SCRIPT:
def Pop(MyStack):
if len(MyStack) > 0:
MyStack.pop()
else:
print("Stack is empty.")
q=[]
def enqueue():
age=int(input(""))
name=input()
l=[age,name]
q.append(l)
def dequeue(q):
if(q==[]):
print("queue empty")
else:
age,name=q.pop(0)
print("element deleted")
print(q)
OUTPUT:
>>> enqueue()
23
riya
>>> enqueue()
65
Preeti
>>> dequeue(q)
element deleted
[[65, 'preeti']]
SCRIPT:
clientdetail=[]
def addclient(clientname):
clientdetail.append(clientname)
def remove():
if(len(clientdetail)>0):
clientdetail.pop()
else:
print("Stack is empty")
SCRIPT:
sal = []
def addsal(s):
sal.append(s)
def removesal():
if(len(sal) > 0):
sal.pop()
else:
print("Stack is empty")
OUTPUT:
>>> addsal(40)
>>> addsal(42)
>>> sal
[40, 42]
>>> removesal()
>>> sal
[40]
SCRIPT:
def SWAP2BEST(l,size):
for i in range(size):
if(l[i]%10==0):
l[i]=l[i+1]
else:
continue
return l
l=list(map(int, input().split(" ")))
print("actual list",l)
r=len(l)
print("after swapping",SWAP2BEST(l,r))
OUTPUT:
30 56 50 80 67
actual list [30, 56, 50, 80, 67]
after swapping [56, 56, 80, 67, 67]
12) Write a function CHANGEO ,which accepts an list of integer and its size
as parameters and divide all those list elements by 7 which are divisible by
7 and multiply list elements by 3.
SCRIPT:
def CHANGEO(l,size):
for i in range(size):
if(l[i]%7==0):
a=l[i]//7
l[i]=a*3
else:
l[i]=l[i]*3
return l
l=list(map(int, input().split(" ")))
print("original list",l)
r=len(l)
print(CHANGEO(l,r))
OUTPUT:
35 7 4 63 30
original list [35, 7, 4, 63, 30]
[15, 3, 12, 27, 90]
INPUT:
65 78 6
OUTPUT:
Original list [65, 78, 6]
List after Alteration [1, 0, 0]
14) Write a program using function which accept two integers as an
argument and return its sum. Call this function and print the results in main(
)
SCRIPT:
def fun(a,b):
return a+b
a=int(input("no.1"))
b=int(input("no.2"))
a=fun(a,b) #calling fun(a,b)
def main():
print(a)
main()
OUTPUT:
no.13
no.24
7
15) Write a function which accepts an integer array and its size as
parameters and rearranges the array in reverse.
SCRIPT:
def fun(a,size):
for i in range(size-1,-1,-1):
print(a[i],end=" ")
OUTPUT:
4789356
original list [4, 7, 8, 9, 3, 5, 6]
6539874
16)Write a definition for function Itemadd () to insert record into the binary
file ITEMS.DAT,
(items.dat- id,gift,cost). info should stored in the form of list .
SCRIPT:
def Itemadd():
import pickle as p
f=open("iITEMS.dat","wb")
n=int(input("no.of entries"))
for i in range(n):
q=int(input("id"))
d=input("gift")
r=float(input("cost"))
h=[q,d,r]
p.dump(h,f)
print("items added")
OUTPUT:
>>> Itemadd()
no.of entries3
id1
giftchocolate hamper
cost300
id2
giftteddy bear
cost500
id3
giftcarom board
cost1200
items added
17) Write a program which creats a csv file contries.csv with the following
data
country,capital,code
india,newdelhi,ii
us,washington,uu
malaysia,ualaumpur,mm
france,paris,ff
SCRIPT:
import csv
f=open("countries.csv","w",newline='')
r=csv.writer(f)
r.writerow(['country','capital','code'])
r.writerow(['india','newdelhi','ii'])
r.writerow(['us','washington','uu'])
r.writerow(['malaysia','ualaumpur','mm'])
r.writerow(['france','paris','ff'])
f.close()
print("added")
SCRIPT:
def fun():
import csv
with open("countries.csv","r") as f:
r=csv.reader(f)
next(r)
for i in r:
if(len(i[1]) > 6):
print(i)
else:
continue
INPUT:
>>> fun()
OUTPUT:
19) Write a python function to search and display the total cost of products
from the file PRODUCT.CSV taking one item of each type. Also calculate the
total value of every item that the seller has.
Products.csv:
pid,name,cost,quantity
p1, pencil, 5,200
p2, pen,20,150
p3, scale, 10,300
p4, eraser, 10,500
p5, sharpener, 5,250
SCRIPT:
f=open("product.csv","r")
r=csv.reader(f)
s=0
t=0
for i in r:
s=s+int(i[2])
t=t+(int(i[2]))*(int(i[3]))
OUTPUT:
20) WAP to find how many 'f' and 's' present in a text file
SCRIPT:
with open("g.txt") as f:
a=f.read()
c=0
s=0
for i in a:
if(i=='F' or i=='f'):
c+=1
elif(i=='S' or i=='s'):
s+=1
print("number of f",c)
print("number of s",s)
OUTPUT:
number of f 2
number of s 2