Practical Programs Class Xii
Practical Programs Class Xii
CLASS-XII-2021
SUBJECT- COMPUTER SCIENCE [083]
PRACTICAL FILE
# 1. Write a function to search an element in a list using Binary search
method.
def binarysearch(l2,n2,item2):
ans=0
beg=0
end=n2-1
while(beg<=end):
mid=int((beg+end)/2)
if(l2[mid]==item2):
ans=1
break
if(l2[mid]<item2):
end=mid-1
if(l2[mid]>item2):
beg=mid+1
return(ans)
# main program
l1=[]
n1=int(input("Enter number of elements you want to store:"))
for i in range(n1):
a=int(input("Enter element:"))
l1.append(a)
item1=int(input("Enter any item you want to search:"))
ans1=binarysearch(l1,n1,item1)
if(ans1==0):
print("Search is not success")
else:
print("Search is success")
OUTPUT OF PROGRAM-1
Enter number of elements you want to store:10
Enter element:1
Enter element:2
Enter element:3
Enter element:4
Enter element:5
Enter element:6
Enter element:7
Enter element:8
Enter element:9
Enter element:10
Search is success
#2. Write a function to calculate the sum of all digits of an entered number.
def calsum(n1):
sum1=0
while(n1>0):
rem=n1%10
sum1=sum1+rem
n1=int(n1/10)
return(sum1)
#MAIN PROGRAM
ans1=calsum(n)
# main program
l1=[]
n=int(input("Enter how many number of elements you want to store:"))
for i in range(n):
a=int(input("Enter element:"))
l1.append(a)
ans1=largest(l1)
print("Largest element:",ans1)
OUTPUT OF PRACTICAL-3
Enter how many number of elements you want to store:5
Enter element:1
Enter element:5
Enter element:6
Enter element:2
Enter element:3
Largest element: 6
# 4.Write a function to count total number of upper case, lower case letter,
digits and special symbol in a string
def trial(str2):
lwr=upr=dig=ss=0
for i in str2:
n=ord(i)
upr=upr+1
lwr=lwr+1
dig=dig+1
else:
ss=ss+1
ans1,ans2,ans3,ans4=trial(str1)
def prime(x):
for i in range(2,x+1):
if (i% j ==0):
break
if(j+1==i):
print(i)
prime(n)
OUTPUT OF THE PROGRAM-5
Enter any number:15
11
13
# 6. Read a text file line by line and display each line starting with letter
with ‘T’
f=open("first.txt","r")
text=f.read()
print(text)
n=0
for i in text:
if(i[0]=='T'):
n=n+1
print("Total lines:",n)
OUTPUT OF THE PROGRAM-6
The aim here is to find a real world problem that is worthwhile to solve.
Students are encouraged to visit local businesses and ask them about the problems that
they are facing.
Total lines: 3
# 7. program to count total vowel and consonant in a string
f=open("first.txt","r")
a=f.read()
print(a)
vow=cons=upr=lwr=0
for i in a:
vow=vow+1
else:
cons=cons+1
upr=upr+1
lwr=lwr+1
print("Total vowels=",vow)
print("Total consonant=",cons)
The aim here is to find a real world problem that is worthwhile to solve.
Students are encouraged to visit local businesses and ask them about the problems that
they are facing.
f1=open("student.txt","r")
f2=open("temp.txt","w")
print("Original records:")
for i in f1:
print(i)
ans=0
f1.seek(0)
for i in f1:
rec=i.split()
cnt=rec.count(rn1)
if(cnt==1):
ans=1
f2.write(rec1)
else:
f2.write(i)
if(ans==0):
f1.close()
output of program-8
Original records:
print("Original records:")
for i in f:
rec=i.decode()
print(rec)
f.seek(0)
ans=0
for i in f:
rec=i.decode()
list1=rec.split()
cnt=list1.count(rn)
if(cnt==1):
print(rec)
ans=1
if(ans==0):
f.close()
Output of program-9
Original records:
while(1):
print("\t= = = = = = = = = = == = = = = = = = = = = = = = = = = = = = = = = = = = = =")
print("\t\t\t2. PUSH")
print("\t\t\t3. POP")
print("\t\t\t4. Exit.")
print("\t= = = = = = = = = = == = = = = = = = = = = = = = = = = = = = = = = = = = = =")
print("\t\t")
if(ch==1):
len1=len(s)
print("length=",len1)
if(len1==0):
print("STACK is empty")
else:
print(s)
if(ch==2):
n=int(input("Enter an item:"))
s.append(n)
print("Element inserted:",n)
print("Top Element=",n)
if(ch==3):
len1=len(s)
print("length=",len1)
if(len1==0):
print("STACK is empty")
else:
print("Element deleted:",s.pop())
if(ch==4):
print("\n"*50)
input()
break
Output of program-13 :
STACK Operation
= = = = = = = = = = == = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Display STACK
2. PUSH
3. POP
4. Exit.
= = = = = = = = = = == = = = = = = = = = = = = = = = = = = = = = = = = = = =
length= 0
STACK is empty
STACK Operation
= = = = = = = = = = == = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Display STACK
2. PUSH
3. POP
4. Exit.
= = = = = = = = = = == = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter your choice:2
Enter an item:10
Element inserted: 10
Top Element= 10
STACK Operation
= = = = = = = = = = == = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Display STACK
2. PUSH
3. POP
4. Exit.
= = = = = = = = = = == = = = = = = = = = = = = = = = = = = = = = = = = = = =
length= 1
[10]
#15. Write a method/function SHOWWORDS() in python to read lines
#from a text file lesson.txt, and display those words, which are more than 10 characters.
Def fileread():
f=open("first.txt","r")
a=f.read()
print(a)
l1=a.split()
for i in l1:
if(len(i)>=10):
print(i)
f.close()
Output of program-15
The aim here is to find a real world problem that is worthwhile to solve.
Students are encouraged to visit local businesses and ask them about the problems that they
are facing.
submission
worthwhile
encouraged
businesses
# 12. Write a random number generator that generates random numbers between 1 and
6 (simulates a dice).
import random
for i in range(100):
6632655531545663166346221342336523214615133521626214
136345164655363322425621461563345361664356526336