Computer Science Program File
Computer Science Program File
DHAULA KUAN
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”.
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)
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)
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)
for i in range(n):
print(d)
(Output)
h=input("Enter class")
if(h in
d.keys()):
else:
(Output)
Enter class12-E
Enter class12-A
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()
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(a)
f1=open("lower.txt","a")
f2=open("upper.txt","a")
f3=open("others.txt","a")
r=f.read()
for i in r:
f1.write(i)
f2.write(i)
else:
f3.write(i)
f.close()
f1.close()
f2.close()
f3.close()
c=0
for i in f.readline():
if(i[0]=='F'):
c=c+1
print(c)
(Output)
t=f.read()
c=0
for i in t.split():
if(i=='firewall')or (i=='is'):
c=c+1
print(c)
(OUTPUT
10
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)
f=open("items.dat","wb")
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 id 1
enter cost 45
record added
enter id 2
record added
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"))
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()
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()
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)
if(len(st)>0):
r=st.pop()
return r
else:
print("stack empty")
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:
cursor.close()
connection.close()
get_employees_with_high_salary()
db=m.connect(host="localhost",user="root",passwd="1234",
database="Em")
c=db.cursor()
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