SSCE Practicals2025
SSCE Practicals2025
Names of
#employees. Implement PUSH(), POP(), PEEK() & DISPLAY() operations.
def isEmpty(stk): #checks whether stack is empty
if stk==[]:
return True
else:
return False
def push(stk,item): #Adds new item in the stack
stk.append(item)
top=len(stk)-1
def pop(stk): # Function is used to remove items from stack
if isEmpty(stk):
return "underflow"
else:
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)-1
return item
def peek(stk): # This function will return the top most item from the stack
if isEmpty(stk):
return "Underflow"
else:
top=len(stk)-1
return stk[top]
def display(stk):
if isEmpty(stk):
print("Stack Empty")
else:
top=len(stk)-1
print(stk[top],"<----TOP")
for a in range(top-1,-1,-1):
print(stk[a])
#___________main________
stack=[]
top=None
while True:
print("*****STACK DEMONSTRATION******")
print("1. PUSH")
print("2. POP")
print("3.PEEK")
print("4.SHOW STACK")
print("5.EXIT")
ch=int(input("Enter your choice:"))
if ch==1:
name=input("Enter Name of the employee to be inserted:")
push(stack,name)
elif ch==2:
val=pop(stack)
if val=="underflow":
print("Underflow!Stack is empty")
else:
print("Popped item is",val)
elif ch==3:
val=peek(stack)
if val=="underflow":
print("Underflow!stack is empty")
else:
print("Topmost item is",val)
elif ch==4:
display(stack)
elif ch==5:
break
else:
print("Invalid choice")
2. def insert(l):
pos=int(input("ENTER POSITION OF ITEM INSERTION:"))
item=int(input("ENTER ITEM TO BE INSERTED"))
l.insert(pos,item)
print("list after insertion",l)
def delete(l):
item=int(input("ENTER ITEM TO BE DELETED"))
if item in l:
l.remove(item)
print(l)
else:
print("ITEM NOT IN LIST")
def display(l):
for i in l:
print(i)
def search(l):
item=int(input("ITEM"))
try:
pos=l.index(item)
print("ITEM FOUND AT INDEX",pos)
print("ITEM FOUND AT POSITION",pos+1)
except:
print("ITEM NOT IN LIST")
l=eval(input("ENTER LIST:"))
while True:
print("""
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH""")
ch=int(input("ENTER CHOICE:"))
if ch==1:
insert(l)
if ch==2:
delete(l)
if ch==3:
display(l)
if ch==4:
search(l)
if input("DO YOU WISH TO CONTINUE[Y/N]").upper()!="Y":
break
3. def lsearch(lst,ele):
for i in range(len(lst)):
if ele==lst[i]:
print("The element is found at index",i)
break
else:
print(ele,"not found in the given list")
def binsearch(lst,ele):
found=0
low=0
high=len(lst)-1
while(low<=high):
mid=(low+high)//2
if lst[mid]==ele:
index=mid
found=1
break
elif lst[mid]>ele:
high=mid-1
else:
low=mid+1
if found==1:
print("Found at position",index+1)
else:
print("Not Found")
#__main__
mylist=[]
while True:
ele=int(input("Enter the element in the list"))
mylist.append(ele)
if input("Do you want to insert more?").upper()!='Y':
break
print("The List is:",mylist)
while True:
print("To Search element in the List\n")
print("1. Linear Search\n2. Binary Search")
ch=int(input("Enter the choice"))
ele=int(input("Enter the element to be searched"))
if ch==1:
lsearch(mylist,ele)
elif ch==2:
binsearch(mylist,ele)
if input("Do you want to continue(Y/N)").upper()!='Y':
break
4. #Write a program in Python to create a text file ‘passage.txt’, find and store the distinct
words and their
#frequencies as elements of a dictionary. Finally print the dictionary elements in a tabular
form.
def write1():
f=open("passage.txt","w")
while True:
line=input("Enter line:")
f.write(line)
f.write('\n')
choice=input("Do u want to insert more lines(Y/N):")
if choice.upper()=='N':
break
f.close()
def freqcnt():
dic={}
file=open('passage.txt','r')
line=file.read()
# print(line)
word=line.split()
# print(word)
for w in word:
if w in dic:
dic[w]+=1
else:
dic[w]=1
print("Word","\t","Frequency")
for key in dic:
print(key,'\t',dic[key])
#__main__
write1()
freqcnt()
5. def lsearch(lst,ele):
for i in range(len(lst)):
if ele==lst[i]:
print("The element is found at index",i)
break
else:
print(ele,"not found in the given list")
def insort(lst):
for i in range(len(lst)):
key=lst[i]
j=i-1
while j>=0 and key<lst[j]:
lst[j+1]=lst[j] #Shift elements to right to make room for key
j=j-1
else:
lst[j+1]=key
print("List after sorting is:",lst)
#__main__
mylist=[]
while True:
ele=int(input("Enter the element in the list"))
mylist.append(ele)
if input("Do you want to insert more?").upper()!='Y':
break
print("The List is:",mylist)
while True:
print("List Operations\n")
print("1. To search using Linear Search\n2. To Sort the list using insertion sort")
ch=int(input("Enter the choice"))
if ch==1:
ele=int(input("Enter the element to be searched"))
lsearch(mylist,ele)
elif ch==2:
insort(mylist)
if input("Do you want to continue(Y/N)").upper()!='Y':
break
6. def binsearch(lst,ele):
found=0
low=0
high=len(lst)-1
while(low<=high):
mid=(low+high)//2
if lst[mid]==ele:
index=mid
found=1
break
elif lst[mid]>ele:
high=mid-1
else:
low=mid+1
if found==1:
print("Found at position",index+1)
else:
print("Not Found")
def bubsort(l):
n=len(l)
for i in range(n):
for j in range(n-i-1):
if l[j]>l[j+1]:
l[j],l[j+1]=l[j+1],l[j]
#__main__
mylist=[]
while True:
ele=int(input("Enter the element in the list"))
mylist.append(ele)
if input("Do you want to insert more?").upper()!='Y':
break
print("The List is:",mylist)
while True:
print("List Operations\n")
print("1. To search using Binary Search\n2. To Sort the list using bubble sort")
ch=int(input("Enter the choice"))
if ch==1:
ele=int(input("Enter the element to be searched"))
binsearch(mylist,ele)
elif ch==2:
print("The orignal List is",mylist)
bubsort(mylist)
print("The List after sorting",mylist)
if input("Do you want to continue(Y/N)").upper()!='Y':
break
7. #program to copy the lines that doesn't contain 'a' into TEMP.txt
def copylines():
fout=open("passage.txt","w")
while True:
str1=input("Enter the string")
fout.write(str1+'\n')
if input("Add more").upper()!='Y':
break
fout.close()
fin=open("passage.txt","r")
fout=open("TEMP.txt","w")
lines=fin.readlines()
for w in lines:
if 'a' not in w:
fout.write(w+'\n')
fout.close()
fin.close()
print("Display the file after copying")
fin=open("TEMP.txt","r")
print(fin.read())
fin.close()
copylines()
8. with open("passage.txt",'w') as fh:
fh.writelines('India is my country\nAll Indians are my brothers and sisters\nI love my
country\nI am proud of its rich and varied heritage')
print("Given passage:")
with open("passage.txt",'r') as fin:
lines=fin.readlines()
print(lines)
tc=uc=lc=dig=spl=0
with open("passage.txt",'r') as fin:
ch=' '
while ch!='':
ch=fin.read(1)
tc+=1
if ch.isupper():
uc+=1
elif ch.islower():
lc+=1
elif ch.isdigit():
dig+=1
else:
spl+=1
print("Total no. of characters:",tc)
print("Total no. of uppercase alphabets:",uc)
print("Total no. of lowercase alphabets:",lc)
print("Total no. of digits:",dig)
print("Total no. of special characters:",spl)
9. import sys
import pickle
import os
#Searching in Binary file
def dispsearch():
while True:
emp={}
f=open('emp.dat','rb')
eno=int(input("\nEnter the Employee No to be searched:"))
found=False
try:
while True:
emp=pickle.load(f) #loading data in emp list
except EOFError:
f.close()
for e in emp.keys():
#print(e)
if e==eno:
print("\nDetails of employee with Employee No:",e,"")
print('\t','Name:',emp[e]['Name'],'\n\t','Designation:',emp[e]['Designation'],'\n\t','Salary:',e
mp[e]['Salary'])
found=True
break
if found==False:
print("## Sorry Employee Number Not Found ##")
f.close()
if input("\nDisplay More? (Y/N): ").upper()!='Y':
break
def delete():
while True:
f1=open('emp.dat','rb')
emp=emp1={}
try:
while True:
emp=pickle.load(f1)
except EOFError:
f1.close()
f2=open('emp1.dat','wb')
eno=int(input('\nEnter Employee No. to be deleted:'))
found=False
for e in emp.keys():
print(emp.keys)
#print(e)
if e==eno:
found=True
continue
else:
emp1[e]=emp[e]
print("\n #Deletion Successful #")
if found==False:
print("## Sorry Employee Number Not Found ##")
pickle.dump(emp1,f2)
f1.close()
f2.close()
os.remove('emp.dat')
os.rename('emp1.dat','emp.dat')
if input("\nDelete More? (Y/N): ").upper()!='Y':
break
9. import pickle
f=open("EMP.dat",'wb+')
n=int(input("ENTER NO OF EMPLOYES:"))
dic={}
for i in range(n):
empno=int(input("ENTER EMPLOYEE NUMBER:"))
ename=input("ENTER EMPLOYEE NAME:").upper()
edes=input("ENTER DESIGNATION").upper()
sal=int(input("ENTER SALARY:"))
dic[empno]=ename,edes,sal
pickle.dump(dic,f)
f.seek(0)
dic1=pickle.load(f)
f.close()
print(dic1)
while True:
print("1.DISPLAY RECORDS\n2.DELETE EMPLOYEE")
print()
ch=int(input("ENTER CHOICE:"))
if ch==1:
found=0
item=int(input("ENTER THE EMPLOYEE NO:"))
for i in dic1:
if i==item:
print("EMPLOYEE NO:",dic[i][0],"\nEMPLOYEE
NAME:",dic[i][1],"\nDESIGNATION:",dic[i][2],"\nSALARY:",dic[i][2])
found=1
break
if found==0:
print("EMPLOYEE NOT FOUND")
if ch==2:
found=0
item=int(input("ENTER THE EMPLOYEE NO:"))
for i in dic1:
if i==item:
found=1
del dic1[i]
f=open("EMP.dat",'wb')
pickle.dump(dic1,f)
break
print(dic1)
if found==0:
print("EMPLOYEE DOES NOT EXIST")
if input("DO YOU WISH TO CONTINUE[Y/N]").upper()!="Y":
break
10. import csv
import os
import sys
def search():
f=open("book.csv","r")
csv_reader=csv.reader(f)
bkno=input("Enter the Book No. to be searched")
for row in csv_reader:
if(row[0]==bkno):
print(row)
break
else:
print("No such Book Exist")
def update():
if os.path.exists("book.csv"):
bkno=int(input("Enter the book no"))
fin=open("book.csv","r")
fout=open("tmp1.csv","w",newline='')
csvreader=csv.reader(fin)
csvwriter=csv.writer(fout)
for row in csvreader:
if row[0]!=str(bkno):
csvwriter.writerow(row)
else:
row[4] = float(row[4])+0.05*float(row[4])
csvwriter.writerow(row)
fout.close()
fin.close()
os.remove("book.csv")
os.rename("tmp1.csv","book.csv")
print("Record after updation")
fin=open("book.csv","r")
csvreader=csv.reader(fin)
next(csvreader)
for row in csvreader:
#print(row)
print("Book No.",row[0])
print("Title",row[1])
print("Author",row[2])
print("Type",row[3])
print("Price",row[4])
print()
else:
print("File doesnt exist")
#__main__
with open("book.csv","w",newline='') as fh:
csvwriter=csv.writer(fh)
csvwriter.writerow(['BookId','Title','Author','Type','Price'])
while True:
bookid=int(input("Enter the book Id:"))
title=input("Enter the Title of the book:")
author=input("Enter the Author Name:")
type1=input("Enter the Type of the book:")
price=float(input("Enter the Price:"))
bk=[bookid,title,author,type1,price,]
csvwriter.writerow(bk)
if input("Do you want to add more customer details(Y/N)?").upper()!='Y':
break
while True:
print("1.Search\n2.Update\n3.Exit")
ch=int(input("Enter the choice"))
if ch==1:
search()
elif ch==2:
update()
#else:
# print("Program Terminating.....")
# sys.exit()
if input("Do you want to do another operation(Y/N)?").upper()!='Y':
break
12. #user defined function that display the number of lines starting
#with 'H' in the file
def write1():
f=open("COVID.txt","w")
while True:
line=input("Enter line:")
f.write(line)
f.write('\n')
choice=input("Do u want to insert more lines(Y/N):")
if choice.upper()=='N':
break
f.close()
def startT():
f=open("COVID.txt","r")
fout=open("TMP.txt","w")
for line in f.readlines():
if line[0]=='T':
fout.write(line)
f.close()
fout.close()
f=open("TMP.txt","r")
print(f.read())
#for str1 in f:
# print(str1)
write1()
startT()
13. import pickle
def insertRec(dic):
while True:
Adno = int(input('\nEnter Admission number:'))
nam = input('Enter name of the student:')
m1=int(input('Enter marks in Physics'))
m2=int(input('Enter marks in Chemistry'))
m3=int(input('Enter marks in Maths'))
m4=int(input('Enter marks in Computer'))
m5=int(input('Enter marks in English'))
t=m1+m2+m3+m4+m5
if t>450:
g='A1'
elif t>400:
g='A2'
elif t>350:
g='B1'
elif t>300:
g='B2'
elif t>250:
g='C1'
else:
g='FAILED'
dic[Adno]=[nam,m1,m2,m3,m4,m5,t,g]
if input("Add more(Y/N)?").upper()!='Y':
break
f = open('studentsear.dat','wb')
pickle.dump(dic,f)
f.close()
def searRec():
f = open('studentsear.dat','rb')
ano=int(input("\nEnter the admission no of the student to be searched:"))
try:
while True:
dic=pickle.load(f)
break
except Exception:
f.close()
f.close()
dic={}
insertRec(dic)
searRec()
14. Study from your practical file
15. import csv
with open("kseb.csv","w",newline='') as fh:
csvwriter=csv.writer(fh)
csvwriter.writerow(['CustNo','Name','Unit','Charge'])
while True:
cno=int(input("Enter the consumer no:"))
name=input("Enter the name")
unit=int(input("Enter the units"))
if unit>600:
charge=380+(unit-600)
elif unit>400:
charge=220+(unit-400)*.80
elif unit>200:
charge=100+(unit-200)*.60
else:
charge=unit*.50
cust=[cno,name,unit,charge]
csvwriter.writerow(cust)
if input("Do you want to add more customer details(Y/N)?").upper()!='Y':
break
with open("kseb.csv",'r') as fin:
csvreader=csv.reader(fin)
next(csvreader)
for row in csvreader:
print("Customer No.",row[0])
print("Customer Name.",row[1])
print("Units.",row[2])
print("Charge.",row[3])
print()