0% found this document useful (0 votes)
11 views19 pages

Yash Raj XII-E Program File

The document outlines a computer science project submitted by Yash Raj Yadav, detailing various Python programming tasks and functions. It includes code snippets for manipulating lists, reading and writing files, and performing database operations using MySQL. The project covers topics such as modifying list values based on conditions, file handling, and implementing basic database queries.

Uploaded by

kazuto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views19 pages

Yash Raj XII-E Program File

The document outlines a computer science project submitted by Yash Raj Yadav, detailing various Python programming tasks and functions. It includes code snippets for manipulating lists, reading and writing files, and performing database operations using MySQL. The project covers topics such as modifying list values based on conditions, file handling, and implementing basic database queries.

Uploaded by

kazuto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Computer

Science
Project

File
Submitted by: Submitted to:
Yash Raj Yadav Mrs. Pallavi Sharma
(XII-E) (C.S. TEACHER)
(40)

Index
Sno. Topic
Write code for a function void oddEven (S,N) in
1. python, to add 5 in all the odd values and 10 in all the
even values of the list S
Write a code in python for a function Convert (T,N) ,
2. which repositions all the elements of array by shifting
each of them to next position and shifting first element
to last position?
Write a function SWAP2BEST ( ARR, Size) in
3. 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. WAP to input ‘n’ classes and names of their class
teacher to store them in dictionary and display the
same?
5. Accept a particular class from the user and display the
name of the class teacher of that class? (Let the
dictionary be same as the above question)
Write function definition for SUCCESS (), to read the
6. content of a text file STORY.TXT, and count the
presence of word STORY and display the number of
occurrences of this word?
A text file “STORY.txt” has the following data written
7. in it: Living a life you can be proud of Doing your best
Spending your time with people and activities that are
important to you standing up for things that are right
even when it’s hard Becoming the best version of you.
Write a user defined function to count and display the
total number of words starting with ‘P’ present in a file?

Write a Program that reads character from the keyboard


8. 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?
Write a Program to find no of lines starting with F in
9. firewall.txt
Write a Program to find how many ‘firewall’ or ‘to’ are
10. present in a file firewall.txt?
Write a python function to search and display the record
11. of that product from the file PRODUCT.CSV which
has maximum cost
Write a definition for a function Itemadd() to insert
12. record into the binary file ITEMS.DAT,(items.dat-
id,gift,cost). info should be stored in the form of list.
write a python function writecsv () to write the
13. information into product.csv. using dictionary.
Write a definition for function COSTLY() to read each
14. record of a binary file ITEMS.DAT, find and display
those items, which are priced more than 50
Write a function SHOW(carNo) in Python which
15. accepts the car number as parameter and display details
of all those cars whose mileage is from 100 to 150
stored in the binary file CAR.dat
Write a function in python PUSH (A), where A is a list
16. of numbers. From this list, push all numbers divisible
by 3 into a stack implemented by using a list.
Write a function in python POP (Arr), where Arr is a
17. stack implemented by a list of numbers. The function
returns the value deleted from the stack
Write a MySQL-Python connectivity code display
18. ename, empno, designation, sal of those employees
whose salary is more than 3000 from the table emp.
Name of the database is “Emgt”
Write a MySQL-Python connectivity code to increase
19. the salary (sal) by 100 of those employees whose
designation (job) is clerk from the table emp.Name of
the database is “Em”.
Consider the tables COMPANY & CUSTOMER. Write
20. SQL commands for the statements (i) to (v)

Q1. Write code for a function void oddEven (S,N) in python, to


add 5 in all the odd values and 10 in all the even values of the list
S?
Ans: (coding)
def oddEven (S,N):
for i in range(N):
if(S[i]%2!=0):
S[i]=S[i]+5
else:
S[i]=S[i]+10
print(S)
S=[2,3,45,68,32,23]
N=len(S)
oddEven(S,N)
(Output)
[12, 8, 50, 78, 42, 28]

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?
Ans: (Coding)
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: 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?
Ans: (Coding)
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]

Q4: WAP to input ‘n’ classes and names of their class teacher to
store them in dictionary and display the same?

Ans: (Coding)
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 classes2
Enter class 12-E
Enter name of class teacherMrs Meenakshi sher
Enter class 12-F
Enter name of class teacherMrs Pallavi Sharma
{'12-E': 'Mrs Meenakshi sher', '12-F': 'Mrs Pallavi Sharma'}

Q5: Accept a particular class from the user and display the name of
the class teacher of that class? (Let the dictionary be same as the
above question)
Ans: (Coding)
while True:
h=input("Enter class")
if(h in
d.keys()):
print("Class teacher name is",d[h])
else:
print("Class doesn't exist ")
(Output)
Enter class12-E
Class teacher name is Mrs Meenakshi sher
Enter class12-A
Class doesn't exist
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?
Ans: (Output)
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: A text file “STORY.txt” has the following data written in it:
Living a life you can be proud of Doing your best Spending your
time with people and activities that are important to you standing
up for things that are right even when it’s hard Becoming the best
version of you. Write a user defined function to count and display
the total number of words starting with ‘P’ present in a file?
Ans: (Output)
def count():
f=open(“STORY.txt”, “r”)
r=f.read()
n=0
l=r.split()
a=[]
for i in l:
if(i[0]==‘p’):
n=n+1
a.append(i)
else:
continue
print(“total no. of word starting with P are”, n)
print(a)
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?
Ans: (coding)
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
Ans: (Coding)
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?
Ans: (Coding)
f=open(r"C:\Users\user\Desktop\firewall.txt")
t=f.read()
c=0
for i in t.split():
if(i=='firewall')or (i=='is'):
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
Ans: (Coding)
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: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.
Ans: (Coding)
def Itemadd():
f=open("items.dat","wb")
n=int(input(“enter how many records”))
for i in range(n):
r= int(input('enter id'))
a=input(“enter giftname”)
p=float(input(“enter cost”))
v=[r,a,p]
pickle.dump(v,f)
print(“record added”)
f.close()
Itemadd()#function calling
(Output)
enter how many records 2
enter id 1
enter giftname pencil
enter cost 45
record added
enter id 2
enter giftname pen
enter cost 120
record added

Q13. write a python function writecsv () to write the information


into product.csv. using dictionary. columns of product .csv is as
follows:
pid,pname,cost,quantity

Ans: (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: Write a definition for function COSTLY() to read each record


of a binary file ITEMS.DAT, find and display those items, which
are priced more than 50?
(items.dat- id,gift,cost).Assume that info is stored in the form of
list
Ans: (Coding)
def COSTLY():
f=open("items.dat","rb")
while True:
try:
r=pickle.load(f)
if(r['cost']>50):
print(r)
except:
break
f.close()
Q15:A binary file “CAR.dat” has structure[carNo,carname,milage]
Write a user defined function ADD() to input data for a record and
add to CAR.dat.
Ans: (Coding)
import pickle
def ADD():
f=open(“CAR.dat”, “ab”)
n=int(input(“Enter how many records you want to enter”))
for i in range(n);
cn=int(input(“entercar no.”))
c=int(input(“enter car name”))
m=int(input(“enter car milage”))
car=[cn,c,m]
pickle.dump(car,f)
f.close()

Q15: Write a function SHOW(carNo) in Python which accepts the


car number as parameter and display details of all those cars whose
mileage is from 100 to 150 stored in the binary file CAR.dat?
Ans: (Coding)
def Show(CarNo):
f=open(“CAR.dat”, “rb”)
while True:
Try:
d=pickle.load(f)
if(d[0]==CarNo):
print(d)
except:
continue
f.close()
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. Display the stack if it has at least
one element, otherwise display appropriate error message?
Ans: (Coding)
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: 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?
Ans: (Coding)
def POP(Arr):
if(len(st)>0):
r=st.pop()
return r
else:
print("stack empty")

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”?
Ans: (Coding)
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”.

Ans: (Coding)
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: Consider the tables COMPANY & CUSTOMER. Write SQL


commands for the statements (i) to (v)
Ans: (Coding)
(a)Select name from company where company,cid=customer,id
and price<3000;
(b) Select name from company order by name DESC;
(c) update customer set price=price+1000 where name like ‘%S’;
(d)Alter table customer add totalprice decimal(10,2);
(e)select price,qty,price*qty as Total amount from Customer;

You might also like