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

12 Practical - Python

Uploaded by

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

12 Practical - Python

Uploaded by

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

PRACTICAL FILE 2023-24

CLASS XII- COMPUTER SCIENCE


In Index Page, it shold be like
1. DiceThrow( ) Random number 1-6
2. ChangeNum( ) List elelement Even Half Odd Double
3. ConverStr( ) Convert a to @ and s to $
4. VowelCount( ) to count Vowel, Upper and Lowercase
5. CountMeHe( ) to count word He and Me (without case)
6. Data file “story.txt” and count the occurrence of word “to”
7. Backup text file “school.txt” to “schbkp.txt”
8. Backup text file biodata.txt to biobkp.txt except ‘m”
9. Binary file student.rec to write and read name
10. Binary file “Center.lst” to search center
11. Csv file to show items list – item.csv
12. Csv file search items list – items.csv
13. Csv file to display sport records – sports.csv
14. Stack S – PUSH Operation
15. Stack S – POP Operation
16. Stack employe Push Operation
17. Stack employee POP Operation
18. Stack BOOK – PUSH and POP operation

LIST OF PRACTICALS IN PYTHON FOR CLASS XII


FUNCTIONS
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.
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.
Given list is L1 = [5, 17, 22, 12, 13, 44, 6, 7]
Converted list is L1 = [10, 34, 11, 6, 26, 22, 3, 14]
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.
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.
5. Write a python program to create a function CountMeHe( ) to count the number of occurance
of word He and Me present in the given string

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.

Manish Kumar Gupta, PGT CS


Kendriya Vidyalaya No. 2 Sagar
Region : Jabalpur
SOLUTION

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)

5. Write a python program to create a function CountMeHe( ) to count the number of


occurance of work He and Me present in the given string
#Function to count the occurrence of word He and Me
def CountMeHe(Str):
Cme = 0
Che = 0
word = Str.split()
for m in word:
if(m.lower() == "me"):
Cme = Cme + 1
if(m.lower() == "he"):
Che = Che + 1
return Cme, Che
MyStr = input("Enter String Line : ")
M, H = CountMeHe(MyStr)
print("\nTotal Me : ", M)
print("Total He : ", H)

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

print("Word my occured ",cto," times")


f2.close()

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)

#PUSH and SHOW Stack


S = [] #empty Stack
ans = "y"

print("Insert element in Stack and Show all elements")


while(ans == "y"):
ele = int(input("Enter element to push in stack S : "))
S.append(ele)
print("Stack after PUSH ",S)

ans = input("want to continue (y/n) : ")

print("\n\nAll stack elements")


for x in range(len(S)-1,-1,-1):
print(S[x],end = " ")

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"

print("Elements in Stack S ")


print(S)

while(ans == "y"):

if(S == []):
print("\n\tEmpty Stack Underflow!!!!!!")
break
else:
print("\nPopped item is ",S.pop())
print("Stack after POP ",S)

ans = input("want to delete more item (y/n) : ")

print("All stack elements")


for x in range(len(S)-1,-1,-1):
print(S[x],end = " ")
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).

#Stack - PUSH Employee


employee = [ ]
def push_emp():
employee = [('101', 'Kamal'), ('102', 'Rajesh'),('103','kush')]
print("Original Stack\n",employee)

empid = input("Enter employee id : ")


ename = input("Enter employee name : ")
emp = (empid, ename)
employee.append(emp)
print("Employee successfully added to stack")
print()

print("Stack after adding employee")


x = len(employee)
while(x>0):
print(employee[x-1])
x = x -1

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)

#Stack - POP Employee (Delete employee)

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()

print("Employee after deletion")


x = len(employee)
while(x>0):
print(employee[x-1])
x = x -1

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.

#Stack - PUSH and POP BOOK


BOOK = [('CS', '99'), ('Phy', '84')]
def push_book():
print("Original Stack\n", BOOK)

Bname = input("Enter Book Name : ")


Bprice = input("Enter Book Price : ")
newbook = (Bname, Bprice)
BOOK.append(newbook)
print("New Book successfully added to stack")
print()
print("Book after PUSH")
x = len(BOOK)
while(x>0):
print(BOOK[x-1])
x = x -1

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()

You might also like