12 Practical - Python
12 Practical - Python
DATA FILE
6. Write a python program to create data file “story.txt” and enter the following text And then
count the number occurrence of word “to” in the data file.
“ Early to bed and early to rise”
7. Write a python program to create data file “school.txt”. Then take the backup to another text file
“schbkp.txt”. Show the content of both the data file after taking backup
8. Write a program in python to create a data file “Biodata.txt” and Remove all the lines that start with
the character 'm' in a file and write it to another data file “biobkp.txt”.
BINARY FILE
9. Write a python program to create binary file student.rec which store the name of 5 students
and then read and print the name of students
10. Write a python program to create a binary file “Center.lst” to enter exam cities and then search
for exam center inputted by the user.
CSV FILE
11. Write a program in Python to create a csv file name “item.csv” and write Itemname and price to
csv file and then display all records.
12. Write a program in Python to create a csv file name “items.csv” and write Itemname and price
to csv file and then search for the price of item name inputted by the user.
13. Write a program in Python to create a csv file name “sports.csv” and write sports code, sport
name, number of player and type of sports (indoor or outdoor) to csv file and then display all
records.
STACKS
14. Write a program in Python to create a stack Stack. Push elements in stack S and then show all
the lements in the newly created stack S (PUSH Operation)
15. Write a program in Python to create a stack Stack. Insert few elements in stack. Then ask the
user to remove (POP) the element in stack S and then show all the lements in the newly created
stack S (POP Operation)
16. Write a program in Python to create stack name “employee” having employee code and name
of 03 employees. Then Insert the details of one more employee to the stack. (PUSH Operation)
17. Write a program in Python to create stack name “employee” having employee code and name
of 03 employees. Then remove the details of one more employee to the stack. (POP Operation)
18. Write a program in python to create stack BOOK having details of 02 books. Write the code to
insert a new book (PUSH) and delete a book (POP) and show the details of book at the end.
1. Write a Python program to create a function DiceThrow( ) that generate generates random
numbers between 1 and 6 (simulates a dice) using a user defined function.
# DiceThrow( ) Function
import random
def DiceThrow():
x = rnum = random.randrange(6)
return x
num = DiceThrow()
print("Dice digit is : ",num)
2. Write a python program to create a function ChangeNum( ) to pass a list to a function and
double the odd values and half even values of a list and display list elements after changing.
#Even Half Odd Double
MyList = [5, 17, 22, 12, 13, 44, 6, 7]
print("Original List is : \t",MyList)
def changenum(L1):
t = len(L1)
for x in range(0,t):
if(L1[x] % 2 == 0):
L1[x] = L1[x] // 2
else:
L1[x] = L1[x] * 2
changenum(MyList)
print("Converted List is :\t",MyList)
3. Write a Python program to create a function ConvertStr( ) that receives an string and convert
‘a’ with ‘@’ and ‘s’ with ‘$’ sign and print the converted string.
#Convert String Character replace ‘a’ with ‘@’ and ‘s’ with ‘$’
def ConvertStr(S1):
#for x in range(0, len(S1)) :
S1 = S1.replace("a", "@")
S1 = S1.replace("s", "$")
return S1
nm = input("Enter your name : ")
nm= ConvertStr(nm)
print("After Conversion")
print(nm)
4. Write a Python program to create a function VowelCount( ) and pass a string to a function and
count how many vowels, upper case characters and lower case characters present in the string.
#Function to count no. of vowels, upper and lower case
def VowelCount(Str):
vc = 0
L=0
U=0
for m in Str:
if(m.islower()):
L=L+1
if(m.isupper()):
U=U+1
m = m.lower()
if(m == "a" or m == "e" or m == "i" or m == "o" or m == "u"):
vc = vc + 1
return vc, L, U
MyStr = input("Enter your name : ")
VowC, Lc, Uc = VowelCount(MyStr)
print("\nTotal Vowel : ", VowC)
print("Lower Case : ", Lc)
print("Upper Case : ", Uc)
6. Write a python program to create data file “story.txt” and enter the following text And then
count the number occurrence of word “to” in the data file.
“ Early to bed and early to rise”
#Count of occurrence of work my in text file
cto = 0
f = open("story.txt", "w")
str = input("Enter String : ")
f.write(str)
print("File Created successfully")
f.close()
f2 = open("story.txt","r")
D1 = f2.read()
S1 = D1.split()
for x in S1:
if(x == "to"):
cto = cto + 1
7. Write a python program to create data file “school.txt”. Then take the backup to another text
file “schbkp.txt”. Show the content of both the data file after taking backup
# create school.txt and schbkp.txt except starting with character "a"
fw = open("school.txt", "w")
x = int(input("Enter no. of lines you want to add : "))
for m in range(0,x):
mystr = input("Enter String : ")
s1 = mystr + '\n'
fw.write(s1)
fw.close()
fr = open("school.txt", "r")
fb = open("schbkp.txt", "w")
for m in range(0,3):
D1 = fr.readline()
d2 = D1
print(d2)
fb.write(d2)
fr.close()
fb.close()
print("Data in School.txt")
print("---------------------------------")
f1 = open("school.txt", "r")
for m in range(0,x):
D1 = f1.readline()
print(D1)
f1.close()
print("Data in schbkp.txt")
print("---------------------------------")
f2 = open("schbkp.txt", "r")
for m in range(0,x):
D1 = f2.readline()
print(D1)
f2.close()
8. Write a program in python to create a data file “Biodata.txt” and Remove all the lines that start
with the character 'm' in a file and write it to another data file “biobkp.txt”.
# create school.txt and biodata.txt except starting with character "m"
fw = open("biodata.txt", "w")
x = int(input("Enter no. of lines you want to add : "))
for m in range(0,x):
mystr = input("Enter String : ")
s1 = mystr + '\n'
fw.write(s1)
fw.close()
fr = open("biodata.txt", "r")
fb = open("biobkp.txt", "w")
for m in range(0,x):
d2 = fr.readline()
if(d2[0] != "m"):
print(d2)
fb.write(d2)
fr.close()
fb.close()
print("Data in biobkp.txt")
print("---------------------------------")
f2 = open("biobkp.txt", "r")
for m in range(0,x):
D1 = f2.readline()
print(D1)
f2.close()
9. Write a python program to create binary file student.rec which store the name of 5 students and
then read and print the name of students
#Binary file write & read name of 5 students
import pickle
Stud = ['aman','vimal','raman', 'arun', 'shiv']
f = open("student.rec","wb")
pickle.dump(Stud, f)
print("Binary file successfully created")
f.close()
import pickle
f = open("student.rec","rb")
D =pickle.load(f)
print("Data in Binary File")
print(D)
f.close()
10. Write a python program to create a binary file “Center.lst” to enter exam cities and then search
for exam center inputted by the user.
#Binary file write & read name of 5 students
import pickle
CenterLst = []
num = int(input("Enter number of centers to add : "))
for x in range(0,num):
center = input("Enter Center name : ")
CenterLst.append(center)
cf = open("center.lst","wb")
pickle.dump(CenterLst, cf)
print("Center List Binary file successfully created")
cf.close()
f = open("center.lst","rb")
flag = 0
D =pickle.load(f)
mycent = input("Enter center to be searched : ")
for x in D:
if(mycent == x):
print("Your Center is available : ",x)
flag=1
if(flag ==0):
print("Center you are seeking is NOT AVAILABLE")
f.close()
11. Write a program in Python to create a csv file name “item.csv” and write Itemname and
price to csv file and then display all records.
#Read and write in a csv file - item.csv
import csv
F = open("item.csv", "w", newline ='')
W = csv.writer(F)
N = int(input("How many no. of records to enter : "))
for i in range(N):
iname= input("Enter Item Name : ")
price = int(input("Enter price of item : "))
ItemLst = [iname, price]
W.writerow(ItemLst)
print("Records of Items successfully added\n")
F.close()
F = open("item.csv","r")
rec = csv.reader(F)
print("\nRecords Items in file")
print("\nItem Name\tPrice")
print("---------------------------------------------------")
for i in rec:
L=i
print(L[0],"\t", L[1])
F.close()
12. Write a program in Python to create a csv file name “items.csv” and write Itemname and
price to csv file and then search for the price of item name inputted by the user.
#Read and write in a csv file - items.csv
import csv
'''F = open("items.csv", "w", newline ='')
W = csv.writer(F)
N = int(input("How many no. of records to enter : "))
for i in range(N):
iname= input("Enter Item Name : ")
price = int(input("Enter price of item : "))
ItemLst = [iname, price]
W.writerow(ItemLst)
print("Records of Items successfully added\n")
F.close()'''
F = open("items.csv","r")
flag = 0
rec = csv.reader(F)
inm = input("Enter the items name : ")
for i in rec:
L=i
if(L[0] == inm):
print("Price of ",L[0]," is ", L[1])
flag = 1
if(flag == 0):
print("Searched item not available")
F.close()
13. Write a program in Python to create a csv file name “sports.csv” and write sports code, sport
name, number of player and type of sports (indoor or outdoor) to csv file and then display all
records.
#Read and write in a single csv file
import csv
F = open("sports.csv", "w", newline ='')
W = csv.writer(F)
N = int(input("How many no. of records to enter : "))
for i in range(N):
scode = int(input("Enter Sports Code No. : "))
sname= input("Enter Sport Name : ")
nop = int(input("Enter number of player : "))
stype = input("Type of Sport (Indoor / Outdoor) : ")
L = [scode, sname, nop,stype]
W.writerow(L)
print("Records of sports successfully added\n")
F.close()
F = open("sports.csv","r")
rec = csv.reader(F)
print("\nRecords in file")
print("\nCode\tName\tPlayer\tType of sport")
print("---------------------------------------------------------")
for i in rec:
L=i
print(L[0],"\t", L[1],"\t", L[2],"\t", L[3])
F.close()
14. Write a program in Python to create a stack Stack. Push elements in stack S and then show all
the lements in the newly created stack S (PUSH Operation)
15. Write a program in Python to create a stack Stack. Insert 05 elements in stack. Then ask the
user to remove (POP) the element in stack S and then show all the lements in the newly created
stack S (POP Operation)
#POP and SHOW Stack
S = [25, 75, 33, 85] #Stack with 5 elements
ans = "y"
while(ans == "y"):
if(S == []):
print("\n\tEmpty Stack Underflow!!!!!!")
break
else:
print("\nPopped item is ",S.pop())
print("Stack after POP ",S)
push_emp()
17. Write a program in Python to create stack name “employee” having employee code and name
of 03 employees. Then remove the details of one employee from the stack. (POP Operation)
employee = [ ]
def pop_emp():
employee = [('101', 'Kamal'), ('102', 'Rajesh'),('103','kush')]
print("Original Stack\n",employee)
if(employee == []):
print("No employee Exists - Empty Stack")
else:
empid, ename = employee.pop()
print("Deleted items : ",empid, ename)
print()
pop_emp()
18. Write a program in python to create stack BOOK having details of 02 books. Write the
code to insert a new book (PUSH) and delete a book (POP) and show the details of book
after PUSH and POP operation.
def pop_book():
print("\nPresent Stack elements \n", BOOK)
if(BOOK == []):
print("No Book exists - Empty Stack")
else:
Bname, Bprice = BOOK.pop()
print("Deleted items : ",Bname, Bprice)
print()
print("Book after POP")
x = len(BOOK)
while(x>0):
print(BOOK[x-1])
x = x -1
push_book()
pop_book()