INDEX
S. No. PROGRAM
1 Write the definition of a function Alter (A,N) in python, which should change all the odd numbers in the list to 1 and even
numbers to 0.
2 Write a code in python for a function Convert ( T, N) , which repositions all the elements of array by shifting each of them
to next position and shifting first element to last position.
3 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.
4 Write function definition for SUCCESS(), to read the content of a text file STORY.TXT, and count the presence of word
STORY and display the number of occurences of this word.
5 Write a program to find no of lines starting with ‘F’ in firewall.txt.
6 Write a program that reads character from the keyboard one by one. All lower case characters get store inside the file
LOWER, all upper case characters get stored inside the file UPPER and all other characters get stored inside OTHERS.
7 Write a definition for a function Economic() to read each record of a binary file ITEMS.DAT, find and display those items
which cost less than 2500.
8 Write a function to search for a tablet from a binary file “table.dat”. The user should enter the model no and function
should search and display the details of the tablet.
9 Write a program to find how many 'firewall' or 'to’ are present in a file firewall.txt.
10 Write a python function to search and display the record of that product from the file PRODUCT.CSV which has
maximum cost.
Sample of product.csv is given below:
pid,pname,cost,quantity
p1,brush,50,200
p2,toothbrush,120,150
p3,comb,40,300
p4,sheets,100,500
p5,pen,10,250
11 Write a program to find no of lines starting with F in firewall.txt.
12 Write the definition of a member function PUSH() in python to add a new book in a dynamic stack of BOOKS considering
the following code is already included in the program: ISBN, TITLE.
13 Write a function in python to delete a node containing book’s information, from a dynamically allocated stack of books
implemented with the help of the following structure: BNo, BName.
14 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.
15 Write a function in python POP (Arr), where Arr is a stack implemented by a list of numbers. The function returns the
value deleted from the stack.
16 A linear stack called "List" contains the following information:
a. Roll Number of student
b. Name of student
Write add(List) and pop(List) methods in python to add and remove from the stack.
17 Write SQL command for (a) to (f) and write the outputs for (g) on the basis of tables INTERIORS and NEWONES.
18 Write SQL commands for (a) to (f) and write the outputs for (g) on the basis of table HOSPITAL.
19 Write a MySQL-Python connectivity code display ename, empno, designation, sal of those employees whose salary is
more than 3000 from the table emp . Name of the database is “Emgt”.
20 Write a MySQL-Python connectivity code to increase the salary (sal) by 300 of those employees whose designation (job)
is clerk from the table emp. Name of the database is “Emgt”.
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)
D=[10,13,15,21]
print(“Original list”, d)
r=len(d)
Alter(d,r)
#OUTPUT
Original list [10,13,15,21]
List after alteration [0,1,1,1]
def Convert(T,N):
t=T[0]
for i in range(N-1):
T[i]=T[i+1]
T[N-1]=t
print("after conversion", T)
d=[10,14,11,21]
print("Original List", d)
r=len(d)
Convert(d,r)
#OUTPUT
Original List [10, 14, 11, 21]
after conversion [14, 11, 21, 10]
#Program3: 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.
def SWAP2BEST(A,size):
i=0
while(i<size):
if(A[i]%10==0):
A[i],A[i+1]=A[i+1],A[i]
i=i+2
else:
i=i+1
return(A)
d=[90,56,45,20,34,54]
print("actual list", d)
r=len(d)
print("after swapping", SWAP2BEST(d,r))
#OUTPUT
actual list [90, 56, 45, 20, 34, 54]
after swapping [56, 90, 45, 34, 20, 54]
#Program4: Write function definition for SUCCESS(), to read the content of a text file STORY.TXT,
and count the presence of word STORY and display the number of occurences of this word.
def SUCCESS():
f=open("STORY.txt")
r=f.read()
c=0
for i in r.split():
if(i=="STORY"):
i=i.lower()
c=c+1
print(c)
f.close()
SUCCESS()
#Program5: Write a program to find number of lines starting with ‘F’ in firewall.txt.
f=open(r"C:\Users\hp\Desktop\cs\networking\firewall.txt")
c=0
for i in f.readline():
if(i=='F'):
c=c+1
print(c)
#OUTPUT
1
#Program6: Write a program that reads character from the keyboard one by one. All lower case
characters get store inside the file LOWER, all upper case characters get stored inside the file UPPER
and all other characters get stored inside OTHERS.
f=open(r"C:\Users\user\Desktop\cs\networking\firewall.txt")
f1=open("lower.txt","a")
f2=open("upper.txt","a")
f3=open("others.txt","a")
r=f.read()
for i in r:
if(i>='a' and i<='z'):
f1.write(i)
elif(i>='A' and i<='Z'):
f2.write(i)
else:
f3.write(i)
f.close()
f1.close()
f2.close()
f3.close()
#Program7: Write a definition for a function Economic() to read each record of a binary file
ITEMS.DAT, find and display those items which cost less than 2500.
def Economic():
f1=open("items.dat","ab")
while True:
try:
g=pickle.load(f1)
if(g['cost']<250):
print(g)
except:
break
Economic()
#OUTPUT
enter giftname flowers
enter id 2
enter cost 500
{'giftname': ' flowers', 'id': 2, 'cost': 500.0}
#Program8: Write a function to search for a tablet from a binary file “table.dat”. The user should enter
the model no and function should search and display the details of the tablet.
def search():
f2=open("tablet.dat","rb")
m=int(input("enter the model no which you want to search"))
print("\n records satisfy the condition are \n")
while True:
try:
g=pickle.load(f1)
if g[0]==m:
print(g)
except:
break
f2.close()
search()
#OUTPUT
enter how many records 1
enter modelno 2
enter RAM 94
enter hd 56
details beautiful brand new, gen 6 iPad
{‘how many records’: 1, ‘modelno’: 2, ‘RAM’: 94, ‘hd’: 56, ‘details’: ‘beautiful brand new, gen 6 iPad’}
#Program9: Write a program to find how many ‘firewall’ or ‘to’ are present in a file firewall.txt.
f=open(r"C:\Users\user\Desktop\cs\networking\firewall.txt")
t=f.read()
c=0
for i in t.split():
if(i=='firewall')or (i=='is'):
c=c+1
print(c)
#OUTPUT
9
#Program10: Write a python function to search and display the record of that product from the file PRODUCT.CSV which has
maximum cost.
Sample of product.csv is given below:
pid,pname,cost,quantity; p1,brush,50,200; p2,toothbrush,120,150; p3,comb,40,300; p4,sheets,100,500; p5,pen,10,250
import csv def searchcsv(): #OUTPUT
['p2', 'toothbrush', '120', '150']
def writecsv(): f=open("product.csv","r")
f=open("product.csv","w") r=csv.reader(f)
r=csv.writer(f,lineterminator='\n') next(r)
r.writerow(['pid','pname','cost','qty']) m=-1
r.writerow(['p1','brush','50','200']) for i in r:
r.writerow(['p2','toothbrush','120','150']) if (int(i[2])>m):
r.writerow(['p3','comb','40','300']) m=int(i[2])
r.writerow(['p5','pen','10','250']) d=i
print(d)
writecsv()
searchcsv()
#Program11: Write a program to find no of lines starting with F in firewall.txt.
f=open(r"C:\Users\hp\Desktop\cs\networking\firewall.txt")
c=0
for i in f.readline():
if(i=='F'):
c=c+1
print(c)
#OUTPUT
1
#Program12: Write the definition of a member function PUSH() in python to add a new book in a
dynamic stack of BOOKS considering the following code is already included in the program: ISBN,
TITLE.
s=[] #OUTPUT
def push(): enter ISBN no 2
a=int(input("enter ISBN no")) enter title Encyclopedia
t=input("enter title") [2, ' Encyclopedia’] ---top
l=[a,t]
s.append(l)
def disp(s):
if(s==[]):
print("list is empty")
else:
top=len(s)-1
print(s[top],"---top")
for i in range(top-1,-1,-1):
print(s[i])
push()
disp(s)
#Program13: Write a function in python to delete a node containing book’s information, from a
dynamically allocated stack of books implemented with the help of the following structure: BNo,
BName.
#OUTPUT
s=[] stack is empty
def pop(): list is empty
if(s==[]):
print("stack is empty")
else:
Book_no,Book_title=s.pop()
print("element deleted")
def disp(s):
if(s==[]):
print("list is empty")
else:
top=len(s)-1
print(s[top],"---top")
for i in range(top-1,-1,-1):
print(s[i])
pop()
disp(s)
#Program14: 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.
st=[]
def PUSH(Arr):
for i in Arr:
if(i%5==0):
st.append(i)
if(len(st)<0):
print(st)
else:
print("stack empty")
#OUTPUT
stack empty
#Program15: Write a function in python POP (Arr), where Arr is a stack implemented by a list of
numbers. The function returns the value deleted from the stack.
def POP(Arr):
if(len(st)>0):
r=st.pop()
return r
else:
print("stack empty")
#OUTPUT
blank
#Program16: A linear stack called "List" contains the following information:
a. Roll Number of student
b. Name of student
Write add(List) and pop(List) methods in python to add and remove from the stack.
List=[] def disp(s):
#OUTPUT
def add(List): if(s==[]):
Enter roll number 1
rno=int(input("Enter roll number")) print("list is empty")
Enter name reena
name=input("Enter name") else:
Enter roll number 2
item=[rno,name] top=len(s)-1
Enter name teena
List.append(item) print(s[top],"---top")
[2, 'teena'] ---top
def pop(List): for i in range(top-1,-1,-1):
[1, 'reena']
if len(List)>0: print(s[i])
[1, 'reena'] ---top
List.pop()
#Call add and pop function to verify the code
else:
add(List)
print("Stack is empty")
add(List)
disp(List)
pop(List)
disp(List)
#Program17: Write SQL command for (a) to (f) and write the outputs for (g) on the basis of tables
INTERIORS and NEWONES.
#ANSWERS
(a) Select * From INTERIORS Where TYPE = “Sofa”;
(b) Select ITEMNAME From INTERIORS Where PRICE > 10000;
(c) Select ITEMNAME, TYPE From INTERIORS
Where DATEOFSTOCK < {22/01/02} Order by ITEMNAME;
(d) Select ITEMNAME, DATEOFSTOCK From INTERIORS Where DISCOUNT >
15;
(e) Select Count (*) From INFERIORS Where TYPE = “Double Bed”;
(f) Insert into NEWONES Values
(14, “True Indian”, “Office Table”, {28/03/03}, 15000, 20};
#Program18: Write SQL commands for (a) to (f) and write the outputs for (g) on the basis of table
HOSPITAL.
#ANSWERS
(a) SELECT * FROM Hospital
WHERE Department = “Cardiology;
(b) SELECT Name FROM Hospital
WHERE Department = “ENT” AND Sex = “F”;
(c) SELECT Name, Datofadm FROM Hospital
ORDER BY Datofadm;
(d) SELECT Name, Charges, Age FROM Hospital
WHERE Sex = “F”;
(e) SELECT COUNT (*) FROM Hospital
WHERE Age < 30;
#Program19: Write a MySQL-Python connectivity code display ename, empno, designation, sal of
those employees whose salary is more than 3000 from the table emp. Name of the database is
“Emgt”.
import mysql.connector as m
db=m.connect(host="localhost",user="root",passwd="1234",database="Emgt")
c=db.cursor()
c.execute("select * from emp where sal>3000")
r=c.fetchall()
for i in r:
print(i)
#Program20: Write a MySQL-Python connectivity code to increase the salary (sal) by 300 of those
employees whose designation (job) is clerk from the table emp. Name of the database is “Emgt”.
import mysql.connector as m
db=m.connect(host="localhost",user="root",passwd="1234",database="Emgt")
c=db.cursor()
c.execute("update emp set sal=sal+300 where job=”clerk”)
db.commit()