You are on page 1of 24

ARMY PUBLIC SCHOOL

DHAULA KUAN

Session 2020-21

PYTHON
PROGRAMMING FILE

NAME : Sahil ghildiyal


CLASS : XII-E
ROLL NO :33
INDEX

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

12. WAP to find factorial of entered number using


library func FACT().
13. write a python function writecsv () to write the
information into product.csv. using dictionary.
14. WAP to read data from text file DATA.txt and
display word which have MAXIMUM
/MINIMUM characters.
15. WAP to sort a list of items using BUBBLE SORT.
16. Write a function in python PUSH (A), where A is
a list of numbers. From this list, push all
numbers divisible by 3 into a stack implemented
by using a list.
17. WAP to show Insertion and Deletion operation
using queue.
18. 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”
19. Write a MySQL-Python connectivity code to
increase the salary (sal) by 100 of those
employees whose designation (job) is clerk from
the table emp.Name of the database is “Em”.
20. WAP to show PUSH and POP operation using
STACK.

Q1 WAP to find and display the sum of all the values


which are ending with 3 from a list.

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

Q2. 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?

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]

Q3 WAP to remove all odd numbers from the given


list.

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]

Q4. WAP to input ‘n’ classes and names of their class


teacher to store them in dictionary and display the
same?

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

Q5. WAP to display those strings which start with ‘A'


from the given list.

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

Q6. 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?

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

Q8. 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\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()

Q9. 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[0]=='F'):
c=c+1
print(c)

OUTPUT
3

Q10. Write a Program to find how many ‘firewall’ or


‘to’ are present in a file firewall.txt?

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

Q11. 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():
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()

Q14. WAP to read data from text file DATA.txt and


display word which have MAXIMUM /MINIMUM
characters.

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

Q15. WAP to sort a list of items using BUBBLE


SORT.
import time
n=int(input("Enter Number of items in List: "))
DATA=[]
for i in range(n):
item=int(input("Item :%d: "%(i+1)))
DATA.append(item)
print("Array Before Sorted : ",DATA)
for i in range(1,len(DATA)):
print("*****************(%d)*************************"%i)
c=1
for j in range(0,len(DATA)-i):
if(DATA[j]>DATA[j+1]):
DATA[j],DATA[j+1] = DATA[j+1],DATA[j]
time.sleep(0.200)
print("%2d"%c,":",DATA)
c+=1
print("%2d"%i,":",DATA)
time.sleep(0.900)
print("Array After Sorted : ",DATA)
Q16. Write a function in python PUSH (A), where A is
a list of numbers. From this list, push all numbers
divisible by 3 into a stack implemented by using a list.

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)

Q17. WAP to show Insertion and Deletion operation


using queue.

def add_element(Queue,x): #function to add


element at the end of list
Queue.append(x)

def delete_element(Queue): #function to


remove last element from list
n = len(Queue)
if(n<=0):
print("Queue empty....Deletion not possible")
else:
del(Queue[0])

def display(Queue): #function to


display Queue entry
if len(Queue)<=0:
print("Queue empty...........Nothing to display")
for i in Queue:
print(i,end=" ")

#main program starts from here


x=[]
choice=0
while (choice!=4):
print(" ********Queue menu***********")
print("1. Add Element ")
print("2. Delete Element")
print("3. Display ")
print("4. Exit")
choice = int(input("Enter your choice : "))
if(choice==1):
value = int(input("Enter value : "))
add_element(x,value)
if(choice==2):
delete_element(x)
if(choice==3):
display(x)
if(choice==4):
print("You selected to close this program")
Q18. 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)

Q19. Write a MySQL-Python connectivity code to


increase the salary (sal) by 100 of those employees
whose designation (job) is clerk from the table
emp.Name of the database is “Em”.

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

Q20. WAP to show PUSH and POP operation using


STACK.

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

You might also like