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

Samanyu Kaushal-Program File

The document contains definitions for various Python functions related to lists, stacks, files and SQL queries. Some functions modify lists by changing element values, repositioning elements or swapping elements. Other functions implement stacks using lists and include push and pop operations. Additional functions perform file operations like reading, writing and searching records. SQL queries and connectivity to MySQL database is also demonstrated.

Uploaded by

kunal.1214155669
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Samanyu Kaushal-Program File

The document contains definitions for various Python functions related to lists, stacks, files and SQL queries. Some functions modify lists by changing element values, repositioning elements or swapping elements. Other functions implement stacks using lists and include push and pop operations. Additional functions perform file operations like reading, writing and searching records. SQL queries and connectivity to MySQL database is also demonstrated.

Uploaded by

kunal.1214155669
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

PYTHON PROGRAMMING FILE

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 Itemadd() to insert record into the binary file
ITEMS.DAT,(items.dat-id,gift,cost). info should be stored in the form of list.
8 Write a definition for function SHOWINFO() to read each record of a binary file ITEMS.DAT.
{items.dat-id,gift,cost}. Assume that info is stored in the form of list.
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=[9,12,15,22]
print(“Original list”, d)
r=len(d)
Alter(d,r)

#OUTPUT
Original list [9,12,15,22]
List after alteration [1,0,1,0]
def Convert(T,N):
t=T[0]
for i in range(N-1):
T[i]=T[i+1]
T[N-1]=t
print("List after conversion", T)

d=[12,15,10,25]
print("Original List", d)
r=len(d)
Convert(d,r)

#OUTPUT
Original List [12, 15, 10, 25]
List after conversion [15, 10, 25, 12]
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]
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()
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
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()
def Itemadd(): #OUTPUT
f=open("items.dat","wb") enter how many records 2
n=int(input(“enter how many records”)) enter id 1
for i in range(n): enter giftname pencil
r= int(input('enter id')) enter cost 45
a=input(“enter giftname”) record added
p=float(input(“enter cost”)) enter id 2
v=[r,a,p] enter giftname pen
pickle.dump(v,f) enter cost 120
print(“record added”) record added
f.close()
Itemadd()#function calling
import pickle
def SHOWINFO():
f=open("items.dat","rb")
while True:
try:
g=pickle.load(f)
print(g)
except:
break
f.close()
SHOWINFO()#function calling

#OUTPUT

[1, 'pencil', 45.0]


[2, 'pen', 120.0]
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
def searchcsv():
import csv
f=open("product.csv","r")
def writecsv():
r=csv.reader(f)
f=open("product.csv","w") #OUTPUT
next(r) ['p2', 'toothbrush', '120', '150']
r=csv.writer(f,lineterminator='\n')
m=-1
r.writerow(['pid','pname','cost','qty'])
for i in r:
r.writerow(['p1','brush','50','200'])
if (int(i[2])>m):
r.writerow(['p2','toothbrush','120','150'])
m=int(i[2])
r.writerow(['p3','comb','40','300'])
d=i
r.writerow(['p5','pen','10','250'])
print(d)

writecsv()
searchcsv()
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
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)
#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)
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
def POP(Arr):
if(len(st)>0):
r=st.pop()
return r
else:
print("stack empty")

#OUTPUT
blank
def disp(s):
List=[] #OUTPUT
if(s==[]):
def add(List): Enter roll number 1
print("list is empty")
rno=int(input("Enter roll number")) Enter name reena
else:
name=input("Enter name") Enter roll number 2
top=len(s)-1
item=[rno,name] Enter name teena
print(s[top],"---top")
List.append(item) [2, 'teena'] ---top
for i in range(top-1,-1,-1):
def pop(List): [1, 'reena']
print(s[i])
if len(List)>0: [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)
#ANSWERS

(a) Select * From INTERIORS Where TYPE = “Sofa”;


(b) Select ITEMNAME From INTERIORS Where PRICE &gt; 10000;
(c) Select ITEMNAME, TYPE From INTERIORS
Where DATEOFSTOCK &lt; {22/01/02} Order by ITEMNAME;
(d) Select ITEMNAME, DATEOFSTOCK From INTERIORS Where DISCOUNT &gt;
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};
#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 &lt; 30;
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)
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()

You might also like