Professional Documents
Culture Documents
DHAULA KUAN
Session 2020-21
PYTHON
PROGRAMMING FILE
Sno. Topic
1. WAP to find and display the sum of all the values
which are ending with 3 from a list.
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. WAP to remove all odd numbers from the given
list.
4. WAP to input ‘n’ classes and names of their class
teacher to store them in dictionary and display
the same?
5. WAP to display those strings which start with ‘A'
from the given list.
6. 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 occurrences of this word?
7. WAP to accept values from a user and create
Tuple.
8. 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?
9. Write a Program to find no of lines starting with
F in firewall.txt
10. Write a Program to find how many ‘firewall’ or
‘to’ are present in a file firewall.txt?
11. 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
L=[33,13,92,99,3,12]
sum=0
x=len(L)
for i in range(0,x):
if type(L[i])==int:
if L[i]%10==3:
sum+=L[i]
print(sum)
OUTPUT
49
OUTPUT
Original List [10, 14, 11, 21]
after conversion [14, 11, 21, 10]
L=[2,7,12,5,10,15,23]
for i in L:
if i%2==0:
L.remove(i)
print(L)
OUTPUT
[7, 5, 15, 23]
d={}
n=int(input("enter number of classes"))
for i in range(n):
k=input("Enter class ")
d[k]=input("Enter name of class teacher")
print(d)
OUTPUT
enter number of classes 2
Enter class 12-E
Enter name of class teacher Mrs Meenakshi sher
Enter class 12-F
Enter name of class teacherMrs Pallavi Sharma
{'12-E': 'Mrs Meenakshi sher', '12-F': 'Mrs Pallavi
Sharma'}
L=['abhishek','Lalit','AKHTAR','Heena','NISHANT',]
count=0
for i in L:
if i[0] in ('A'):
count=count+1
print(i)
print("appearing",count,"times")
OUTPUT
Abhishek
AKHTAR
appearing 2 times
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()
Q7. WAP to accept values from a user and create
Tuple.
t=tuple()
n=int(input("enter limit:"))
for i in range(n):
a=input("enter number:")
t=t+(a,)
print("output is")
print(t)
output
enter limit:3
enter number:2
enter number:3
enter number:4
output is
('2', '3', '4')
f=open(r"C:\Users\user\Desktop\Story.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()
OUTPUT
3
f=open(r"C:\Users\user\Desktop\firewall.txt")
t=f.read()
c=0
for i in t.split():
if(i=='firewall')or (i=='to'):
c=c+1
print(c)
OUTPUT
10
import csv
def searchcsv():
f=open("product.csv","r")
r=csv.reader(f)
next(r)
m=-1
for i in r:
if (int(i[2])>m):
m=int(i[2])
d=i
print(d)
writecsv()
searchcsv()
OUTPUT
['p2', 'toothbrush', '120', '150']
Q12. WAP to find factorial of entered number using
library func FACT().
def fact(n):
if n<2:
return 1
else :
return n*fact(n-1)
import factfunc
x=int(input("Enter value for factorial : "))
ans=factfunc.fact(x)
print (ans)
Q13. Write a python function writecsv () to write the
information into product.csv. using dictionary.
ns: (Coding)
def writecsv():
f=open("product.csv","w",newline="")
h=['pid','pname','cost','qty']
r=csv.DictWriter(f1,fieldnames=h)
r.writeheader()
while True:
i=int(input("enter id"))
n=input("enter product name")
c=int(input("enter cost"))
q=int(input("enter qty"))
v={'pid':i,'pname':n,'cost':c,'qty':q}
r.writerow(v)
ch=input("more records")
if(ch=='n'):
break
f.close()
f1=open("data.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,", ",len(words))
maxC=len(words[0])
minC=len(words[0])
minfinal=""
maxfinal=""
for word in words[1:]:
length=len(word)
if maxC<length:
maxC=length
maxfinal=word
if minC>length:
minC=length
minfinal=word
print("Max word : ",maxfinal,", maxC: ",maxC)
print("Min word : ",minfinal,", minC: ",minC)
output
st=[]
def PUSH(A):
for i in range(0,len(A)):
if(A[i]%3==0):
st.append(A[i])
if(len(st)==0):
print("stack empty")
else:
print(st)
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="Em")
c=db.cursor()
c.execute("update emp set sal=sal+100 where job=”clerk”)
db.commit()
#stack.py
def push(stack,x): #function to add element at the end
of list
stack.append(x)
def pop(stack): #function to remove last element
from list
n = len(stack)
if(n<=0):
print("Stack empty....Pop not possible")
else:
stack.pop()
def display(stack): #function to display stack entry
if len(stack)<=0:
print("Stack empty...........Nothing to display")
for i in stack:
print(i,end=" ")
#main program starts from here
x=[]
choice=0
while (choice!=4):
print("********Stack Menu***********")
print("1. push(INSERT)")
print("2. pop(DELETE)")
print("3. Display ")
print("4. Exit")
choice = int(input("Enter your choice :"))
if(choice==1):
value = int(input("Enter value "))
push(x,value)
if(choice==2):
pop(x)
if(choice==3):
display(x)
if(choice==4):
print("You selected to close this program")