0% found this document useful (0 votes)
17 views9 pages

XXSC

The document contains a series of Python programming tasks, each requiring the implementation of specific functions for file handling, data manipulation, and stack operations. Examples include reading from text files to filter words, counting vowels, managing employee records in CSV format, and handling binary files for book and student records. Each task is accompanied by sample code and expected outcomes.

Uploaded by

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

XXSC

The document contains a series of Python programming tasks, each requiring the implementation of specific functions for file handling, data manipulation, and stack operations. Examples include reading from text files to filter words, counting vowels, managing employee records in CSV format, and handling binary files for book and student records. Each task is accompanied by sample code and expected outcomes.

Uploaded by

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

1. A pre-exis ng text file data.txt has some words wri en in it.

Write a
python func on displaywords() that will print all the words that are
having length greater than 3.
Example:
For the fie content:
A man always wants to strive higher in his life He wants to be perfect.
The output a er execu ng displayword() will be: Always wants strive
higher life wants perfect

def displaywords():
f = open('data.txt','r')
s = f.read()
lst = s.split()
for x in lst:
if len(x)>3: print(x, end=" ")
f.close()
displaywords()

2. A pre-exis ng text file info.txt has some text wri en in it. Write a python
func on countvowel() that reads the contents of the file and counts the
occurrence of vowels(A,E,I,O,U) in the file.

def countvowels():
f -=open('info.txt', 'r')
s = f.read()
count = 0
for x in s:
if x in 'AEIOU':
count+=1
print(count)
f.close()
countvowels()
3. A dic onary contains the names of some ci es and their popula on in
crore. Write a python func on push(stack, data), that accepts an empty
list, which is the stack and data, which is the dic onary and pushes the
names of those countries onto the stack whose popula on is greater
than 25 crores.
For example :
The data is having the contents {'India':140, 'USA':50, 'Russia':25,
'Japan':10} then the execu on of the func on push() should push India
and USA on the stack.

data={'India':140, 'USA':50, 'Russia':25, 'Japan':10}


stack=[]
def push(stack, data):
for x in data:
if data[x]>25:
stack.append(x)
push(stack, data)
print(stack)

4. A list of numbers is used to populate the contents of a stack using a


func on push(stack, data) where stack is an empty list and data is the
list of numbers. The func on should push all the numbers that are even
to the stack. Also write the func on pop(stack) that removes the top
element of the stack on its each call. Also write the func on calls

data = [1,2,3,4,5,6,7,8]
stack = []
def push(stack, data):
for x in data:
if x % 2 == 0:
stack.append(x)
def pop(stack):
if len(stack)==0:
return "stack empty"
else:
return stack.pop()
push(stack, data)
print(pop(stack))
5. Write a Program in Python that defines and calls the following user
defined func ons:
(i) ADD() – To accept and add data of an employee to a CSV file
‘record.csv’. Each record consists of a list with field elements as empid,
name and mobile to store employee id, employee name and employee
salary respec vely.
(ii) COUNTR() – To count the number of records present in the CSV file
named ‘record.csv’.

import csv
def ADDR():
fout=open("record.csv","a",newline="\n")
wr=csv.writer(fout)
rollno=int(input("Enter rollno :: "))
name=input("Enter name :: ")
mobile=int(input("Enter mobile number :: "))
lst=[rollno,name,mobile]
wr.writerow(lst)
fout.close()
def COUNTR():
fin=open("record.csv","r",newline="\n")
data=csv.reader(fin)
d=list(data)
print(len(d))
fin.close()
ADDR()
COUNTR()
6. Write a func on Le Shi (Numlist, n) in Python, which accepts a list
Numlist of numbers and n is a numeric value by which all elements of
the list are shi ed to le .
Sample input data of the list
Numlist = [10, 20, 30, 40, 50, 60, 70], n=2
Output
Numlist = [30, 40, 50, 60, 70, 10, 20]

Ans:
Numlist = [10, 20, 30, 40, 50, 60, 70],
n=2
def Le Shi (numlist, n):
L = len(numlist)
for x in range(0,n):
y = numlist[0]
for i in range(0,L-1):
numlist[i] = numlist[i+1]
numlist[L-1] = y
print(numlist)
Le Shi (numlist, n)

7. Write the full form ofCSV. What is the default delimiter of csv files?
The scores and ranks of three students of a school level programming
compe on is given as:
[Name, Marks, 'Rank']
['Sheela', 450, 1]
['Rohan', 300, 2]
['Akash', 260, 3]
Write a program to do the following:
i. Create a CSV file(result.csv) and write the above data into it.
ii. To display all the records present in the CSV file named ‘result.csv’

8. A nested list contains the data of visitors in a museum. Each of the inner
listscontains the following data of a visitor:
[V_no (int), Date (string), Name (string), Gender (String M/F), Age (int)]
Write the following user defined func ons to perform given opera ons
on the stack named "status":
(i) Push_element(Visitors) - To Push an object containing Gender of
visitor who are in the age range of 15 to 20.
(ii) Pop_element() - To Pop the objects from the stack and count the
display the number of Male and Female entries in the stack. Also,
display “Done” when there are no elements in the stack.
For example: If the list Visitors contains: [['305', "10/11/2022",
“Geeta”,"F”, 35],
['306', "10/11/2022", “Arham”,"M”, 15], ['307', "11/11/2022",
“David”,"M”, 18], ['308', "11/11/2022", “Madhuri”,"F”, 17], ['309',
"11/11/2022", “Sikandar”,"M”, 13]]
The stack should contain F
MM
The output should be:
Done
Female: 1
Male: 2
Ans:
visitors=[['305', '10/11/2022', 'Geeta','F', 15],['306', '10/11/2022',
'Arham','M', 15],\
['307', "11/11/2022", 'David','M', 18],['308', "11/11/2022", 'Madhuri','F',
17]]
status=[]
def Push_Element(visitors):
global status
m_c=0
f_c=0
for i in visitors:
if i[4]>=15 and i[4]<=20:
status.append(i[3])
if i[3]=='M':
m_c+=1
if i[3]=='F':
f_c+=1
print("Males:",m_c)
print("FeMales:",f_c)
def Pop_Element():
global status
if status!=[]:
return status.pop()
else:
return "Done"
Push_Element(visitors)
for i in range(len(status)+1):
print(Pop_Element())

9. Write a python program to create a csv file dvd.csv and write 10 records
in it Dvdid, dvd name, qty, price.
Display those dvd details whose dvd price is more than 25.

import csv
f=open("pl.csv","w")
cw=csv.writer(f)
ch="Y"
while ch=="Y":
l=[]
pi=int(input("enter dvd id "))
pnm=input("enter dvd name ")
sp=int(input("enter qty "))
p=int(input("enter price(in rupees) "))
l.append(pi)
l.append(pnm)
l.append(sp)
l.append(p)
cw.writerow(l)
ch=input("do you want to enter more rec(Y/N): ").upper()
if ch=="Y":
con nue
else:
break
f.close()
f=open("pl.csv","r+")
cw=list(csv.reader(f))
for i in cw:
if l[3]>25:
print(i)
f.close()
10.Pramod has created a dic onary containing EMPCODE and SALARY as
key value pairs of 5 Employees of Parthivi Construc ons.
Write a program, with separate user defined func ons to perform the
following opera ons:
● Push the keys (Employee code) of the dic onary into a stack, where
the corresponding value (Salary) is less than 25000.
● Pop and display the content of the stack. For example: If the sample
content of the dic onary is as follows:
EMP={"EOP1":16000, "EOP2":28000, "EOP3":19000, "EOP4":15000,
EOP5":30000} The output from the program should be: EOP4 EOP3
EOP1

EMP={"EOP1":16000, "EOP2":28000, "EOP3":19000,


"EOP4":15000,"EOP5":30000} def PUSH(S,N):
S.append(N)
def POP(S):
if S!=[]:
return S.pop()
else:
return None
ST=[]
for k in EMP:
if EMP[k]<25000:
PUSH(ST,k)
while True:
if ST!=[]:
print(POP(ST),end="")
else:
break

11.A binary file “Book.dat” has structure [BookNo, Book_Name, Author,


Price].
i. Write a user defined func on CreateFile() to input data for a record
and add to Book.dat .
ii. Write a func on CountRec(Author) in Python which accepts the
Author name as parameter and count and return number of books by
the given Author are stored in the binary file “Book.dat”.
import pickle
def createFile():
fobj=open("Book.dat","ab")
BookNo=int(input("Book Number : "))
Book_name=input("Name :")
Author = input("Author:" )
Price = int(input("Price : "))
rec=[BookNo,Book_Name,Author,Price]
pickle.dump(rec,fobj)
fobj.close()
def CountRec(Author):
fobj=open("Book.dat","rb")
num = 0
try:
while True:
rec=pickle.load(fobj)
if Author==rec[2]:
num = num + 1
except:
fobj.close()
return num
createFile():
Author = input("Author:" )
CountRec(Author)

12.A binary file “STUDENT.DAT” has structure (admission_number, Name,


Percentage). Write a func on countrec() in Python that would read
contents of the file “STUDENT.DAT” and display the details of those
students whose percentage is above 75. Also display number of students
scoring above 75%.
import pickle
def CountRec():
fobj=open("STUDENT.DAT","rb")
num = 0
try:
while True:
rec=pickle.load(fobj)
if rec[2] > 75:
print(rec[0],rec[1],rec[2],sep="\t")
num = num + 1
except:
fobj.close()
return num
num=CountRec():
print(“number of students scoring above 75% :”,num)

You might also like