Cs Practical
Cs Practical
________________________________________________________________________________________________________________________
1. python prog. to find all prime numbers upto range given by user using function
def prime_test(n,PrimeNo):
#flag=0
for num in range(1,n+1):
for i in range(2,int(num/2)+1):
if(num%i==0):
break
else:
PrimeNo.append(num)
#_main_
choice='y'
while(choice=='y' or choice=='y'):
PrimeNo=[]
x=int(input('enter a number upto which, list of prime number you want to create:'))
flag=prime_test(x,PrimeNo)
print('List of Prime Number upto:',x,'is',PrimeNo)
choice=input(' press y to continue n to exit:')
if(choice!='y'):
print('Good by....')
*************************************************
enter a number upto which, list of prime number you want to create: 50
List of Prime Number upto: 50 is [1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
press y to continue n to exit: y
enter a number upto which, list of prime number you want to create: 1000
List of Prime Number upto: 1000 is [1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73,
79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193,
197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317,
331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457,
461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743,
751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887,
907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]
press y to continue n to exit: n
Good by....
________________________________________________________________________________________________________________________
2. Python program to encrypt & decrypt the message string using split() & join() function
def encrypt(str,key):
return key.join(str)
def decypt(str,key):
return str.split(key)
3. # Write a Python function sin(x, n) to calculate the value of sin(x) using its Taylor series expansion
up to n terms. Compare the values of sin(x) for different values of n with the correct value.
===============================================================================
=================
Output: -
===============================================================================
=================Enter the value of X in degrees45
Value of X in Radians= 0.7853981633974483 and in Degree= 45.0
enter the number of terms25
1.value of sin(X) computed according to tayler series in radian 0.7071067811865475
2.value of sin(X) correct value as math formula= 0.7071067811865475
Now compare the correct value in step 1 with step 2
press y to continue
________________________________________________________________________________________________________________________
4. # Python program for random number generator that generates random numbers between 1 and 6
(simulates a dice).
import random
def Number_gen():
counter = 0
myList = []
while (counter) < 6:
randomNumber = random.randint(1,6)
myList.append(randomNumber)
counter = counter + 1
print(myList)
Number_gen()
===============================================================================
=================
Output: -
===============================================================================
=================
[6, 3, 4, 1, 2, 3]
________________________________________________________________________________________________________________________
5. # Python program to count the word 'to' and 'the' in text file 'STORY.txt'
file=open('ABC.txt','r')
c1=c2=0
lines = file.read()
print(lines)
L=lines.split()
for i in L:
if i=="to":
c1=c1+1
if i=="the":
c2=c2+1
print('No.of \'to\' word in a file',c1)
print('No.of \'the\' word in a file',c2)
file.close()
===============================================================================
=================
Output: -
===============================================================================
=================
A beautiful day to start new journey
for the same time he began in a last year
he visited the sun temple
to his first visit to odisha
No.of 'to' word in a file 3
No.of 'the' word in a file 2
________________________________________________________________________________________________________________________
6. # Python program to count the no. of uppercase, lowercase & digit in a text file 'POPULATION.txt'
digit=0
lower=0
upper=0
alpha=0
file=open('poem.txt','r')
msg=file.read()
for a in msg:
if a.isupper():
upper+=1
if a.islower():
lower+=1
if a.isdigit():
digit+=1
if a.isalpha():
alpha+=1
print('*'*50)
print('The content of files:\n',msg)
print('*'*50)
print('upper case alphabets in file=',upper)
print('lower case alphabets in file=',lower)
print('digits in file=',digit)
print('alphabets in file=',alpha)
===============================================================================
=================
Output: -
===============================================================================
=================
**************************************************
The content of files:
This was year 2015
when I was in London
A joyfull morning in LONDON
with Hourse RIDING and Swimming
make our enjoyment multiples 2222222232345
**************************************************
________________________________________________________________________________________________________________________
7. # Python program to count the no. of lines start with either with 'A' and 'M' and display those lines
def countAM():
c1=c2=0
file=open('delhi.txt','r')
str=' '
msg=file.readlines()
for k in msg:
if(k[0]=='A' or k[0]=='a'):
c1=c1+1
if(k[0]=='M'or k[0]=='m'):
c2=c2+1
file.close()
return c1,c2
# __main__ start here
print('no of lines start with (A/a) and (M/m) are',countAM())
===============================================================================
=================
Output: -
===============================================================================
=================
no of lines start with (A/a) and (M/m) are (3, 2)
>>>
________________________________________________________________________________________________________________________
8. # Python program to write the record of 5 students in a text file having roll_no, name & marks also
read the data from file and display.
file=open("student.txt",'w+')
print('Read the data of 5 students')
for i in range(5):
print('enter data for student',i+1)
roll_no=input('enter the Roll number of student :')
Name=input('enter the Name of student :')
mark=input('enter the Mark of student :')
record=roll_no+","+Name+","+mark+'\n'
file.write(record)
file.seek(0)
for i in range(5):
m=file.readline()
print('Data of student :',i+1)
print(m)
file.close()
===============================================================================
=================
Output: -
===============================================================================
=================
Read the data of 5 students
enter data for student 1
enter the Roll number of student :101
enter the Name of student :jai kumar
enter the Mark of student :95
enter data for student 2
enter the Roll number of student :102
enter the Name of student :ravi rai
enter the Mark of student :99
enter data for student 3
________________________________________________________________________________________________________________________
Data of student : 2
102,ravi rai,99
Data of student : 3
104,vijay singh,85
Data of student : 4
103,k k asiwal,100
Data of student : 5
106,anil,89
________________________________________________________________________________________________________________________
9. # Python program to read text file "REPORT.txt" and write those lines that start with 'A' into
another file "FINAL.txt"
file1=open('report.txt','r+')
file2=open('final.txt','w+')
str=file1.readlines()
for line in str:
if (line[0]=='a' or line[0]=='A'):
file2.write(line)
file2.seek(0)
x=' '
print('The content of source file \n',str)
print('Now the content of final file')
while x:
x=file2.readline()
print(x)
file1.close()
file2.close()
===============================================================================
=================
Output: -
===============================================================================
=================
The content of source file
['Once upon a time\n', 'a tiger frighten the people\n', 'surrounding area of jungle\n', 'A hunter entered into
jungle\n', 'a cat only catch by him\n', 'The people still afraid\n']
Now the content of final file
a tiger frighten the people
**********Output*********
Enter the Roll Number of student :101
Enter the name of student :ram
Enter the marks of student :99
Press y to enter more record :y
Enter the Roll Number of student :102
Enter the name of student :ajit
Enter the marks of student :99
Press y to enter more record :y
Enter the Roll Number of student :3
Enter the name of student :jai
Enter the marks of student :85
Press y to enter more record :y
Enter the Roll Number of student :4
Enter the name of student :krish
Enter the marks of student :95
Press y to enter more record :n
Now data of students inserted into file
*********A*F*T*E*R*****R*E*A*D*I*N*G********
******F*R*O*M**T*H*E**F*I*L*E**************
Roll No- 101 Name- ram Marks- 99.0
________________________________________________________________________________________________________________________
11. python prog. to search a record in binary file according to roll no.
# python prog. to create a binary file first write data into this and search for a record by roll no.
import pickle
def writeBinary():
data=[]
choice='y'
while( choice=='y' or choice=='Y'):
roll_no=int(input('Enter the Roll Number of student :'))
name=input('Enter the name of student :')
marks=float(input('Enter the marks of student :'))
record=[roll_no,name,marks]
data.append(record)
choice=input('Press y to enter more record :')
file=open('stu11.dat','wb')
pickle.dump(data,file)
print('Now data of students inserted into file')
file.close()
def search():
file=open('stu11.dat','rb')
stu_data=pickle.load(file)
choice='y'
while(choice=='y' or choice=='Y'):
roll=int(input('Enter the roll no. of student which record you want to display :'))
for raw in stu_data:
if raw[0]==roll:
print('Search is successful !')
print('Roll No. of student :',raw[0])
print('Name of student :',raw[1])
print('Marks of student :',raw[2])
break
else:
print('Sorry record is not found of rollno :',roll)
choice=input('Press y to search data of another student :')
*********************Output***********************
First we create a binary file and write the record
Enter the Roll Number of student :1
Enter the name of student :ram
Enter the marks of student :56
Press y to enter more record :y
Enter the Roll Number of student :2
Enter the name of student :vijay
Enter the marks of student :78
Press y to enter more record :y
Enter the Roll Number of student :3
Enter the name of student :sumit
Enter the marks of student :95
Press y to enter more record :y
Enter the Roll Number of student :4
Enter the name of student :seema
Enter the marks of student :96
Press y to enter more record :n
Now data of students inserted into file
Enter the roll no. of student which record you want to display :3
Search is successful !
Roll No. of student : 3
Name of student : sumit
Marks of student : 95.0
Press y to search data of another student :y
Enter the roll no. of student which record you want to display :4
Search is successful !
Roll No. of student : 4
Name of student : seema
Marks of student : 96.0
Press y to search data of another student :y
Enter the roll no. of student which record you want to display :1
Search is successful !
Roll No. of student : 1
Name of student : ram
Marks of student : 56.0
Press y to search data of another student :n
________________________________________________________________________________________________________________________
import pickle
def writeBinary():
data=[]
choice='y'
while( choice=='y' or choice=='Y'):
roll_no=int(input('Enter the Roll Number of student :'))
name=input('Enter the name of student :')
marks=float(input('Enter the marks of student :'))
record=[roll_no,name,marks]
data.append(record)
choice=input('Press y to enter more record :')
file=open('stu12.dat','ab+')
pickle.dump(data,file)
print('Now data of students inserted into file')
file.close()
def update():
found=0
file=open('stu12.dat','rb+')
stu_data=pickle.load(file)
print('***Student data before updation***')
print(stu_data)
choice='y'
while(choice=='y' or choice=='Y'):
roll=int(input('Enter the roll no. of student which record you want to update :'))
for raw in stu_data:
if raw[0]==roll: # As list is mutable type so we can update the value of list
raw[1]=input('Enter new name :')
raw[2]=input('Enter new mark :')
found=1
break
else:
print('Sorry record is not found of rollno :',roll)
choice=input('Press y to update data of another student :')
if found==1:
file.seek(0) # before writing new object into file move the file pointer at beginning in file
pickle.dump(stu_data,file) # writing new object into file student
file.seek(0) # file cursor moving to beginning as file is going to be read again do display
updates
stu_data=pickle.load(file)
print('***Student data after updation of file***')
print(stu_data)
else :
print(' No updation in file ')
________________________________________________________________________________________________________________________
**********Output sample-1********
First we create a binary file and write the record
Enter the Roll Number of student :101
Enter the name of student :arun
Enter the marks of student :78
Press y to enter more record :y
Enter the Roll Number of student :102
Enter the name of student :mohan
Enter the marks of student :65
Press y to enter more record :y
Enter the Roll Number of student :103
Enter the name of student :vikash
Enter the marks of student :74
Press y to enter more record :y
Enter the Roll Number of student :104
Enter the name of student :mohan
Enter the marks of student :99
Press y to enter more record :n
Now data of students inserted into file
***Student data before updation***
[[101, 'karan', 85.0], [102, 'divakar', 75.0], [103, 'divya', 69.0], [104, 'rubina', 91.0]]
Enter the roll no. of student which record you want to update :101
Enter new name :karan singh
Enter new mark :98
Press y to update data of another student :y
Enter the roll no. of student which record you want to update :102
Enter new name :divakar agarwal
Enter new mark :75
Press y to update data of another student :y
Enter the roll no. of student which record you want to update :103
Enter new name :divya kumari
Enter new mark :95
Press y to update data of another student :y
Enter the roll no. of student which record you want to update :104
Enter new name :rubina dutt
Enter new mark :94
Press y to update data of another student :y
Enter the roll no. of student which record you want to update :105
Sorry record is not found of rollno : 105
Press y to update data of another student :n
***Student data after updation of file***
[[101, 'karan singh', '98'], [102, 'divakar agarwal', '75'], [103, 'divya kumari', '95'], [104, 'rubina dutt', '94']]
________________________________________________________________________________________________________________________
****************Output****************
First we create a binary file and write the record
Enter the Roll Number of student :1
Enter the name of student :sameer
Enter the marks of student :45
Press y to enter more record :y
Enter the Roll Number of student :2
Enter the name of student :tennis
Enter the marks of student :96
Press y to enter more record :y
Enter the Roll Number of student :3
Enter the name of student :sujit
Enter the marks of student :81
Press y to enter more record :y
Enter the Roll Number of student :4
Enter the name of student :veena
Enter the marks of student :78
Press y to enter more record :y
Enter the Roll Number of student :5
Enter the name of student :archana
Enter the marks of student :57
Press y to enter more record :n
Now data of students inserted into file
***Student data before deletion***
[[1, 'sameer', 45.0], [2, 'tennis', 96.0], [3, 'sujit', 81.0], [4, 'veena', 78.0], [5, 'archana', 57.0]]
Enter the roll no. of student which record you want to delete :2
one record is matched and deleted
press y to delete more recordy
Enter the roll no. of student which record you want to delete :4
one record is matched and deleted
press y to delete more recordn
***Student data after updation of file***
[[1, 'sameer', 45.0], [3, 'sujit', 81.0], [5, 'archana', 57.0]]
>>>
________________________________________________________________________________________________________________________
14. # python prog. to create menu driven program in python to insert and search the employee data in
employee.csv file
import csv
def create_csv():
file=open('employee.csv','a',newline='')
choice='y'
header=['Name','bank','branch_name','salary']
writer_obj=csv.writer(file,delimiter=',')
writer_obj.writerow(header)
while(choice=='y' or choice=='Y'):
per=0
total=0
name=input('Enter name of employee : ')
bank=input('Enter bank name name : ')
branch=input('Enter branch name : ')
salary=float(input('Enter salary of employee : '))
row=[name,bank,branch,salary]
writer_obj.writerow(row)
def displayAll():
file=open('employee.csv','r',newline='')
reader_obj=csv.reader(file,delimiter=',')
next(reader_obj)
for row in reader_obj:
print('*****************Employee Details****************')
print('Employee Name :',row[0])
print('Employee work in :',row[1])
print('Employee branch is :',row[2])
________________________________________________________________________________________________________________________
#_main_
while True:
print('******************************************************')
print('Press 1 to enter details of employee :')
print('Press 2 to search the employee data :')
print('Press 3 to see the details of all employee :')
print('Press 4 to exit :')
print('******************************************************')
ch=int(input('Enter your choice(1 to 3) :'))
print('******************************************************')
if(ch==1):
create_csv()
elif(ch==2):
search()
elif(ch==3):
displayAll()
elif(ch==4):
break
else:
print('wrong input :')
________________________________________________________________________________________________________________________
Output :
******************************************************
Press 1 to enter details of employee :
Press 2 to search the employee data :
Press 3 to see the details of all employee :
Press 4 to exit :
******************************************************
Enter your choice(1 to 3) :3
******************************************************
*****************Employee Details****************
Employee Name : vijay singh
Employee work in : hdfc
Employee branch is : dhenkanal
Employee Salary is : 125000.0
******************************************************
******************************************************
Press 1 to enter details of employee :
Press 2 to search the employee data :
Press 3 to see the details of all employee :
Press 4 to exit :
******************************************************
Enter your choice(1 to 3) :1
******************************************************
Enter name of employee : ajay lal
Enter bank name name : ubi
Enter branch name : talcher
Enter salary of employee : 75000
Enter y to write data of more student into file : n
******************************************************
Press 1 to enter details of employee :
Press 2 to search the employee data :
Press 3 to see the details of all employee :
Press 4 to exit :
******************************************************
Enter your choice(1 to 3) :2
******************************************************
Enter name of employee which record you want to searchvijay singh
Search is successul
Employee Name : vijay
Employee work in : hdfc
Employee branch is : dhenkanal
Employee Salary is : 125000.0
******************************************************
Press 1 to enter details of employee :
Press 2 to search the employee data :
Press 3 to see the details of all employee :
Press 4 to exit :
******************************************************
________________________________________________________________________________________________________________________
*****************Employee Details****************
Employee Name : vijay singh
Employee work in : hdfc
Employee branch is : dhenkanal
Employee Salary is : 125000.0
******************************************************
*****************Employee Details****************
Employee Name : Name
Employee work in : bank
Employee branch is : branch_name
Employee Salary is : salary
******************************************************
*****************Employee Details****************
Employee Name : ajay lal
Employee work in : ubi
Employee branch is : talcher
Employee Salary is : 75000.0
******************************************************
******************************************************
Press 1 to enter details of employee :
Press 2 to search the employee data :
Press 3 to see the details of all employee :
Press 4 to exit :
******************************************************
Enter your choice(1 to 3) :4
******************************************************
________________________________________________________________________________________________________________________
import bisect
L1=eval(input('Enter any sorted List :'))
print("Now List is Sorted :",L1)
item=int(input('Enter new element to be inserted :'))
pos=bisect.bisect(L1,item)
bisect.insort(L1,item)
print(item,"inserted at index",pos)
print('The list after inserting element')
print(L1)
===============================================================================
=================
Output: -
===============================================================================
=================
Enter any sorted List [10,20,30,50,70,80,90,150]
Now List is Sorted : [10, 20, 30, 50, 70, 80, 90, 150]
Enter new element to be inserted :25
25 inserted at index 2
The list after inserting element
[10, 20, 25, 30, 50, 70, 80, 90, 150]