Class12-Cs-Practical File (Final)
Class12-Cs-Practical File (Final)
S.NO. PROGRAM
1 Write a program to show entered string is a palindrome or not.
2 Write a program to show statistics of characters in the given line(to counts the number
of alphabets ,digits, uppercase, lowercase, spaces and other characters).
5 Write a program to display those string which are starting with ‘A’ from the given
list.
6 Write a program to find and display the sum of all the values which are ending with 3
from a list.
8 Write a program to swap the content with next value, if it is divisible by 7 so that the
resultant array will look like : 3,5,21,6,8,14,3,14.
10 Write a program to input total number of sections and stream name in 11th class and
display all information on the output screen.
11 Write a program to input name of ‘n’ countries and their capital and currency store, it
in a dictionary and display in tabular form also search and display for a particular
country.
12 Write a program to show elements of a two dimensional list in a 2-d array format.
13 Write a Program to show the sum of diagonal (major and minor) of a 2-d list.
14 Write a program to find factorial of entered number using library function fact().
1
15 Write a program to call great func() to find greater out of entered two numbers, using
import command.
16 Write a program to show all non -prime numbers in the entered range .
19 Write a program to enter the numbers and find Linear Search, Binary Search, Lowest
Number and Selection Sort using array code with user defined functions.
20 Write a program to show and count the number of words in a text file ‘DATA.TXT’
which is starting/ended with an word ‘The’, ‘the’.
21 Write a program to read data from a text file DATA.TXT, and display each words
with number of vowels and consonants.
22 Write a program to read data from a text file DATA.TXT, and display word which
have maximum/minimum characters.
23 Write a program to write a string in the binary file “comp.dat” and count the number
of times a character appears in the given string using a dictionary.
24 Write a program that will write a string in binary file "school.dat" and
display the words of the string in reverse order.
26 Write a program to insert list data in CSV File and print it.
27 Write a Program to enter values in python using dataFrames and show these
values/rows in 4 different excel files .
28 Write a Program to read CSV file and show its data in python using dataFrames and
pandas.
2
29 Write a program that rotates the elements of a list so that the element at the first index
moves to the second index, the element in the second index moves to the third
index, etc., and the element in the last index moves to the first index.
30 Write a program to insert item on selected position in list and print the updated list.
36 Write a Program to show database connectivity of python Data Frames with mysql
database.
3
# Program1:WAP to accept a string and whether it is a palindrome or not.
l=len(str)
p=l-1
index=0
while(index<p):
if(str[index]==str[p]):
index=index+1
p=p-1
else:
break
else:
print("string is palindrome")
4
5
#Program2 : WAP to counts the number of alphabets ,digits, uppercase, lowercase, # spaces
and other characters(status of a string).
n=c=d=s=u=l=o=0
for ch in str1:
if ch.isalnum():
n+=1
if ch.isupper():
u=u+1
elif ch.islower():
l=l+1
elif ch.isalpha():
c=c+1
elif ch.isdigit():
d=d+1
elif ch.isspace():
s=s+1
else:
o=o+1
print("no.of smallalphabet",l)
print("no.of digit",d)
6
print("no. of spaces",s)
7
#Program 3:WAP to remove all odd numbers from the given list.
L=[2,7,12,5,10,15,23]
for i in L:
if i%2==0:
L.remove(i)
print(L)
8
#Program 4 :WAP to display frequencies of all the element of a list.
L=[3,21,5,6,3,8,21,6]
L1=[]
L2=[]
for i in L:
if i not in L2:
x=L.count(i)
L1.append(x)
L2.append(i)
print('element','\t\t\t',"frequency")
print(L2[i],'\t\t\t',L1[i])
9
#Program 5: WAP to display those string which starts with ‘A’ from the given list.
L=['AUSHIM','LEENA','AKHTAR','HIBA','NISHANT','AMAR']
count=0
for i in L:
if i[0] in ('aA'):
count=count+1
print(i)
print("appearing",count,"times")
10
11
‘“Program 6:WAP to find and display the sum of all the values which are ending with 3
from a list.”’
L=[33,13,92,99,3,12]
sum=0
x=len(L)
for i in range(0,x):
if type(L[i])==int:
if L[i]%10==3:
sum+=L[i]
print(sum)
12
#Program 7: Write a program to show sorting of elements of a list step-by-step.
a=[16,10,2,4,9,18]
n=len(a)
for i in range(n):
for j in range(0,n-i-1):
if a[j]>a[j+1]:
a[j],a[j+1]=a[j+1],a[j]
13
14
‘“Program8 : A list num contains the following elements : 3,21,5,6,14,8,14,3 . WAP to swap
the content with next value, if it is divisible by 7 so that the resultant array will look like :
3,5,21,6,8,14,3,14 .”’
num=[3,21,5,6,14,8,14,3]
l=len(num)
i=0
while i<10:
if num[i]%7==0:
num[1],num[i+1]=num[i+1],num[i]
i=i+2
else:
i=i+1
15
16
#Program9: Write a program to accept values from a user and create a tuple.
t=tuple()
n=int(input("enter limit:"))
for i in range(n):
a=input("enter number:")
t=t+(a,)
print("output is")
print(t)
17
18
‘“Program10:WAP to input total number of sections and stream name in 11th class and
display all information on the output screen.”’
classxi=dict()
i=1
while i<=n:
a=input("enter section:")
classxi[a]=b
i=i+1 print("class",'\t',"section",'\
print("xi",'\t',i,'\t',classxi[i])
19
20
‘“Program11:WAP to input name of ‘n’ countries and their capital and currency store, it in
a dictionary and display in tabular form also search and display for a particular country.’”
d1=dict(), i=1
while i<=n:
c=input("enter country:")
cap=input("enter capital:")
d1[c]=[cap,curr]
i=i+1
l=d1.keys()
print("\ncountry\t\t","capital\t\t","currency")
for i in l:
z=d1[i] print(i,'\t\
z:
print(j,'\t\t',end='\t\t')
for i in l:
if i==x:
print("\ncountry\t\t","capital\t\t","currency\t\t")
z=d1[i]
print(i,'\t\t',end=" ")
21
for j in z: print(j,'\t\
t',end="\t")
break
22
“““Program12: Write a Program to show the elements of a nested or two dimensional list in
a 2-d array format.”””
x=[[10,20,30],[40,50,60],[70,80,90]]
for i in range(0,3):
for j in range(0,3):
print (x[i][j],end=' ')
print("\n")
23
“““Program13: Write a Program to show Sum of Diagonals (major and minor) in Two
Dimensional List. ”””
24
25
“““Program14 : Write a program to find factorial of entered number using library function
fact().”””
def fact(n):
if n<2:
return 1
else :
return n*fact(n-1)
import factfunc
x=int(input("Enter value for factorial : "))
ans=factfunc.fact(x)
print (ans)
26
#Program15 : Write a Program to call great function to find greater out of entered 2
numbers, using import command.
import greatfunc
a=int(input("Enter First Number : "))
b=int(input("Enter Second Number : "))
ans=greatfunc.chknos(a, b)
print("GREATEST NUMBER = ",ans)
27
28
#Program16: Write a program to show all non -prime numbers in the entered range
def nprime(lower,upper):
print("“SHOW ALL NUMBERS EXCEPT PRIME NUMBERS WITHIN THE RANGE”")
for i in range(lower, upper+1):
for j in range(2, i):
ans = i % j
if ans==0:
print (i,end=' ')
break
29
30
#Program17 : Write a program to show fabonacci series using recursion.
def faboncci(n):
if n==1:
return 0
elif n==2:
return 1
else:
return(faboncci(n-1)+faboncci(n-2))
#main
limit=int(input("enter the ending number"))
print("he fabonacci series are")
for i in range(1,limit+1):
print(faboncci(i))
31
32
#Program18 : Write a program to show GCD of two positive numbers .
def GCD(x,y):
if y==0:
return x
else:
return GCD(y,x%y)
#main
a=int(input("enter the first number"))
b=int(input("enter the second number"))
ans=GCD(a,b)
print("the GCD of two number is ",ans)
33
34
#Program20 : Write a program to show and count the number of words in a text file
‘DATA.TXT’ which is starting/ended with an word ‘The’, ‘the’, ‘my’, ‘he’, ‘they’.
f1=open("data.txt","r")
s=f1.read()
print("="*30)
count=0
words=s.split()
count+=1
35
#PROGRAM 21 : WAP to read data from a text file DATA.TXT, and display each words
with number of vowels and consonants.
f1=open("data.txt","r")
s=f1.read()
print(s)
countV=0
countC=0
words=s.split()
print(words,", ",len(words))
for word in words:
countV=0
countC=0
for ch in word:
if ch.isalnum()==True:
if ch=='a' or ch=='e' or ch=='i' or ch=='o' or ch=='u':
countV+=1
else:
countC+=1
print("Word : ",word,", V: ",countV,", C= ", countC)
36
37
#PROGRAM 22 : WAP to read data from a text file DATA.TXT, and display word which
have maximum/minimum characters.
f1=open("data.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,", ",len(words))
maxC=len(words[0])
minC=len(words[0])
minfinal=""
maxfinal=""
for word in words[1:]:
length=len(word)
if maxC<length:
maxC=length
maxfinal=word
if minC>length:
minC=length
minfinal=word
print("Max word : ",maxfinal,", maxC: ",maxC)
print("Min word : ",minfinal,", minC: ",minC)
38
39
“““PROGRAM23 : Write a program to write a string in the binary file “comp.dat” and
count the number of times a character appears in the given string using a dictionary.
import pickle
f1=open('comp.dat','wb')
pickle.dump(str,f1)
f1.close()
f1=open('comp.dat','rb')
str=pickle.load(f1)
d={}
for x in str:
if x not in d:
d[x]=1
else:
d[x]=d[x]+1
f1.close()
40
#PROGRAM 24:Write a program that will write a string in binary file "school.dat" and
import pickle
f1=open('school.dat','wb')
pickle.dump(str,f1)
f1.close()
f1=open('school.dat','rb')
str1=pickle.load(f1)
str1=str1.split(" ")
l=list(str1)
l.reverse()
#II METHOD
f1=open('school.dat','rb')
str1=pickle.load(f1)
41
print("\n\nthe string in the binary file is : \n",str1)
str1=str.split(" ")
l=list(str1) print("\
length=len(l)
while length>0:
print(l[length-1],end=" ")
length-=1
42
#Program25: Consider a binary file “Emp.dat” containing details such as empno:
ename:salary(separator‘:’).Write a python function to display
details of those employees who are earning between 20000 and 40000.
fin=open("emp.dat", 'rb')
x=fin.readline()
while x:
data=x.split(":")
if float(data[0])>20000 and float(data[0])<30000:
print(data)
x=fin.readline()
fin.close()
43
#Program26: Write a program to insert list data in CSV File and print it
44
45
46
#Program 29: Write a program that rotates the elements of a list so that the element at the first
index moves to the second index, the element in the second index moves to the third index, etc.,
and the element in the last index moves to the first index.
47
#Program30:Write a program to insert item on selected position in list and print the
updated list.
48
49
#PROGRAM 32: A school wants to make an online application form on website for
registration of students who wants to applied for the various school leaders post. The form
requires firstname, lastname and post.
#Write a Menu driven program that provides functions for :
#a) Selecting only those names entered entries where the first letter of the
firstname and lastname are capitalized
#b) Selecting only the incorrectly entered
names #c) Returning a list with corrected names.
def select_errors(STL):
newlist=[]
for record in STL:
name_surname = record.split(' ')
name = name_surname[0]
surname = name_surname[1]
if name[0].islower() or
surname[0].islower():
newlist.append(record)
return newlist
def select_correct(STL):
newlist = []
for record in STL:
name_surname = record.split(' ')
name = name_surname[0]
surname = name_surname[1]
if not name[0].islower() and not surname[0].islower():
newlsit.append(record)
return newlist
def correct_entries(STL):
newlist = []
for record in STL:
name_surname = record.split(' ')
name = name_surname[0]
50
surname =name_surname[1]
51
newlist.append(name.capitalize()+''+ surname.capitalize())
return newlist
# main
STL = []
ch = 0
while (ch != 4):
print("\t- - -")
print("\tMENU")
print("\t- - -")
print("1.Apply for the School Post")
print("2.List of all applicants")
print("3.Correct the Incorrect Enteries")
print("4.Exit")
ch = int(input("Enter your choice (1-4):"))
if ch == 1:
name = input("Enter your name :")
STL.append(name)
elif ch == 2:
print("\tStudents applied so far:")
print(STL)
elif ch == 3:
ok_enteries = select_correct(STL)
error_enteries = select_errors(STL)
corrected_enterie = correct_enteries(STL)
print("Correctly entered names:", ok_enteries)
print("Incorrectly entered names;",error_enteries)
print("corrected names:", corrected_enterie)
elif ch !=4:
print("valid choices are1..4:")
else:
print("THANK YOU")
52
#PROGRAM 33 : Write a program to show push and pop operation using stack.
#stack.py
def push(stack,x): #function to add element at the end of
list stack.append(x)
def pop(stack): #function to remove last element from
list n = len(stack)
if(n<=0):
print("Stack empty....Pop not possible")
else:
stack.pop()
def display(stack): #function to display stack
entry if len(stack)<=0:
print("Stack empty...........Nothing to display")
for i in stack:
print(i,end=" ")
#main program starts from here
x=[]
choice=0
while (choice!=4):
print("********Stack Menu***********")
print("1. push(INSERT)")
print("2. pop(DELETE)")
print("3. Display ")
print("4. Exit")
choice = int(input("Enter your choice :"))
if(choice==1):
value = int(input("Enter value "))
push(x,value)
if(choice==2):
pop(x)
if(choice==3):
display(x)
if(choice==4):
53
print("You selected to close this program")
54
55