0% found this document useful (0 votes)
69 views

ANUSHKA - Program File

The document contains a computer science practical file submitted by a student. It includes 20 programming tasks related to MySQL, Python functions, file handling, and data structures. The tasks involve topics like displaying, adding, deleting and updating records from a MySQL database, implementing stack and queue data structures using Python lists, reading and writing to CSV files, and defining functions to manipulate lists and arrays. The student has provided sample code and outputs for some of the tasks.

Uploaded by

Ankit Soni
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

ANUSHKA - Program File

The document contains a computer science practical file submitted by a student. It includes 20 programming tasks related to MySQL, Python functions, file handling, and data structures. The tasks involve topics like displaying, adding, deleting and updating records from a MySQL database, implementing stack and queue data structures using Python lists, reading and writing to CSV files, and defining functions to manipulate lists and arrays. The student has provided sample code and outputs for some of the tasks.

Uploaded by

Ankit Soni
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

ARMY PUBLIC SCHOOL

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".

8. Write enqueue() and dequeue() methods in python to add


and remove from the queue

9. Write addclient(clientname) and remove()methods in


python to add new client and delete existing client from a
list "clientdetail" ,considering them to act as push and pop
operations of the stack
10. Write addsal(sal) and removesal(sal) functions in python to
add and remove salary from a list of salary in a list "sal",
considering these methods to act as push and pop
operations of data structure stack.
11. Write a function SWAP2BEST ( ARR, Size) in python to
modify the content of the list in such a way that the
elements,which are multiples of 10 swap with the value
present in the verynext position in the list
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.
13. Write the definition of a function Alter(A, N) in python,
which should change all the multiples of 5 in the list to 5
and rest of the elements as 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( )
15. Write a function which accepts an integer array and its size
as parameters and rearranges the array in reverse.

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.
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
18. A csv file counties.csv contains data in the following order:
country,capital,code
write a python function to read the file counties.csv and
display the names of all those
countries whose no of characters in the capital are more
than 6.
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
20. WAP to find how many 'f' and 's' present in a text file
PROGRAM

1)Display all Records using MySQL Connect

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:

2) Add new records using MySQL Connector

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()

3)Delete a particular 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("name of item u want to remove ")
R=("delete from stationary where item='{}'".format(i))
q.execute(R)
c.commit()

4) Update 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 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:

enter the no. of your choice 4


enter the item name u want to search PEN
(2, 'pen', 10)

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.

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:

enter no.of entries 4


Enter Score90
Enter Score40
Enter Score50
Enter Score30
[90, 40, 50, 30]
[90, 40, 50]

7)Write a function pop() which remove namefrom stack named "MyStack".

SCRIPT:

def Pop(MyStack):
 if len(MyStack) > 0:
   MyStack.pop()
 else:
   print("Stack is empty.")

8) Write enqueue() and dequeue() methods in python to add and remove


from the queue
SCRIPT:

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']]

9) Write addclient(clientname) and remove()methods in python to add new


client and delete existing client from a list "clientdetail" ,considering them
to act as push and pop operations of the stack.

SCRIPT:

clientdetail=[]
def addclient(clientname):
    clientdetail.append(clientname)

def remove():
   if(len(clientdetail)>0):
      clientdetail.pop()
   else:
    print("Stack is empty")

10) Write addsal(sal) and removesal(sal) functions in python to add and


remove salary from a list of salary in a list "sal", considering these methods
to act as push and pop operations of data structure stack.

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]

11)Write a function SWAP2BEST ( ARR, Size) in python to


modify the content of the list in such a way that the elements,which
are multiples of 10 swap with the value present in the very next
position in the list

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]

13)Write the definition of a function Alter(A, N) in python, which should


change all the multiples of 5 in the list to 5 and rest of the elements as 0.
SCRIPT:
def Alter(A,N):
for i in range(N):
if(A[i]%2==0):
A[i]=0
else:
A[i]=1
print("LIst after Alteration", A)
l=list(map(int, input().split(" ")))
print("Original list",l)
r=len(l)
Alter(l,r)

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=" ")

l=list(map(int, input().split(" ")))


print("original list",l)
r=len(l)
fun(l,r)

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")

18)A csv file counties.csv contains data in the following order:


country,capital,code
write a python function to read the file counties.csv and display the names
of all those
countries whose no of characters in the capital are more than 6.

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:

['india', 'newdelhi', 'ii']


['us', 'washington', 'uu']
['malaysia', 'ualaumpur', 'mm']

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]))

print("total value is",t)


print("cost of a bundle is",s)

OUTPUT:

total value is 14500


cost of a bundle is 55

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

You might also like