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

Computer Science Program File

Uploaded by

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

Computer Science Program File

Uploaded by

blazeshorts999
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

ARMY PUBLIC SCHOOL

DHAULA KUAN

ACADEMIC YEAR : 2024-25


REPORT FILE’

NAME : ANUJ KUMAR PATHAK


CLASS/SECTION : XII - E
SUBJECT : COMPUTER SCIENCE
SUBJECT CODE : 083

PROJECT GUIDE : Ms. Pallavi Sharma


PGT (Computer Science)
Army Public School’
INDEX

TABLE OF CONTENTS
S.NO INDEX PAGE NO

01 o6
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.

02 07
Write a code in python for a function Convert (T,N) ,
which repositions all the elements of an array by
shifting each of them to next position and shifting the
first element to last position?

03 08
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?
04 09
WAP to input ‘n’ classes and names of their class teacher to
store them in the dictionary and display the same?

05 10
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)

06 10
Write a 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?

07 11
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?

08 12
Write a Program that reads characters from the
keyboard one by one. All lower case characters get
stored inside the file LOWER, all upper case characters
get stored inside the file UPPER and all other
characters get stored inside OTHERS?

09 13
Write a Program to find no of lines starting with F in
firewall.txt.

10 14
Write a Program to find how many ‘firewall’ or ‘to’ are
present in a file firewall.txt?

11 14
Write a python function to search and display the
record of that product from the file PRODUCT.CSV
which has maximum cost.

12 16
Write a definition for a function Itemadd() to insert a
record into the binary file
ITEMS.DAT,(items.dat-id,gift,cost). info should be
stored in the form of a list.

13 17
write a python function writecsv () to write the
information into product.csv. using a dictionary..

14 18
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.

15 19
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.

16 19
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 20
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.

18 21
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 22
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 Write a Python function that that prints out 23


the first n rows of Pascal's triangle.
CODE
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 firstelement 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

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 fileSTORY.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)

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 intoproduct.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: 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,
import mysql.connector

def get_employees_with_high_salary():
connection = mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password",
database="Emgt"
)

cursor = connection.cursor()
cursor.execute("SELECT ename, empno, designation, sal
FROM emp WHERE sal > 3000")
rows = cursor.fetchall()
for row in rows:

print(f"Name: {row[0]}, Emp No: {row[1]}, Designation:


{row[2]}, Salary: {row[3]}")

cursor.close()

connection.close()

get_employees_with_high_salary()

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: Write a Python function that that prints out
the first n rows of Pascal's triangle.
Ans;
def pascal_triangle(n):
trow = [1]
y = [0]
for x in range(max(n,0)):
print(trow)
trow=[l+r for l,r in zip(trow+y, y+trow)]
return n>=1
pascal_triangle(6)
OUTPUT:
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
Thank you

You might also like