CS Practical2
CS Practical2
NO.
OUTPUT:
2. Write a program to display ASCII code of a character and vice versa.
var=True while var: choice=int(input("Press-1 to find the ordinal value of a
character \nPress-2 to find a character of a value\n")) if choice==1:
ch=input("Enter a character : ")
print(ord(ch)) elif choice==2:
val=int(input("Enter an integer value: "))
print(chr(val)) else:
print("You entered wrong choice")
sum=1 while
i<=n:
f=fact(i)
sum=sum+1/f
i+=1
print(sum)
enter the number : 6
OUTPUT:
7.0
4. Write a program to calculate the factorial of an integer using recursion.
def factorial(n):
if n == 1:
return n else:
return n*factorial(n-1)
if y==True:
Text in file:
hello how are you? python is
OUTPUT:
case-sensitive language.
10. Write a program to count the number of vowels present in a text file.
fin=open("D:\\python programs\\MyBook.txt",'r')
str=fin.read( ) count=0 for i in str: if i=='a' or
i=='e' or i=='i' or i=='o' or i=='u':
SOURCE count=count+1
CODE:
print(count)
OUTPUT: 9
11. Write a program to count number of words in a file.
fin=open("D:\\python programs\\Story.txt",'r')
str=fin.read( ) L=str.split()
count_words=0 for
SOURCE
CODE:
i in L:
count_words=count_words+1 print(count_words)
OUTPUT: 16
Write a program to count the number of times the occurrence of 'is' word in a
12.
text file.
SOURCE fin=open("D:\\python programs\\Book.txt",'r') str=fin.read(
CODE:
)
L=str.split( )
count=0 for
i in L: if
i=='is':
count=count+1
print(count) fin.close(
)
OUTPUT: 3
Write a program to write those lines which have the character 'p' from one text
13.
file to another text file.
fin=open("E:\\book.txt","r")
fout=open("E:\\story.txt","a")
s=fin.readlines( ) for j in s:
if 'p' in j:
SOURCE
CODE: fout.write(j)
fin.close() fout.close()
file = open("student.dat","wb")
pickle.dump(list,file)
SOURCE
CODE:
file.close( )
list =
pickle.load(file)
file.close( ) for x in
list: if roll in
x['roll']:
print("Name of student is:",
x['name']) break else:
print("Record not found")
Enter roll number that you want to search in binary file :1202
OUTPUT:
Name of student is: Divya
Write a program to update the name of student by using its roll number in a
16.
binary file.
import pickle
roll = input('Enter roll number whose name you want to update in binary file :')
file = open("student.dat", "rb+")
list = pickle.load(file)
found = 0
SOURCE
CODE:
lst = [ ] for
x in list:
if roll in x['roll']:
found = 1
x['name'] = input('Enter new name: ')
lst.append(x)
if found == 1: file.seek(0)
pickle.dump(lst, file)
print("Record Updated") else:
print('roll number does not exist')
file.close( )
OUTPUT: Enter roll number whose name you want to update in binary file :1202
Enter new name: Harish Record
Updated
17. Write a program to delete a record from binary file.
import pickle
roll = input('Enter roll number whose record you want to delete:') file
= open("student.dat", "rb+")
list = pickle.load(file)
found = 0
SOURCE
CODE: lst = [] for
x in list:
if roll not in x['roll']:
lst.append(x)
else:
found = 1
if found == 1: file.seek(0)
pickle.dump(lst, file)
print("Record Deleted ") else:
print('Roll Number does not exist')
file.close()
Enter roll number whose record you want to delete:1201 Record
OUTPUT:
Deleted
18. Write a program to perform read and write operation with .csv file.
import csv def
readcsv():
with open('C:\\Users\\ViNi\\Downloads\\data.csv','rt')as f: data =
csv.reader(f) #reader function to generate a reader object for
row in data:
SOURCE
CODE:
print(row)
#Tri.py class
Triangle: def
__init__(self):
print("Trinagle")
OUTPUT: Rectangle
Area of Rectangle is : 200
Square
Area of square is : 100
Trinagle
Area of Triangle is : 24.0
21. Write a program for linear search.
L=eval(input("Enter the elements: ")) n=len(L)
item=eval(input("Enter the element that you want to search :
")) for i in range(n): if L[i]==item: print("Element
found at the position :", i+1) break else:
SOURCE
CODE: print("Element not Found")
def size(self): # Size of the stack i.e. total no. of elements in stack
return len(self.items)
s = Stack( )
print("MENU BASED STACK")
c
d
=
T
r
u
e
w
h
i
l
e
c
d
:
print(" 1.
Push ")
print(" 2.
Pop ")
print(" 3.
Display ")
print(" 4.
Size of
Stack ")
print(" 5.
Value at Top
")
choice=int(input("Enter your
choice (1-5) : "))
if choice==1:
val=input("Enter the
element: ")
s.push(val) elif
choice==2: if
s.items==[ ]:
print("Stack
is empty")
else:
print("Deleted
element is :", s.pop( )) elif
choice==3:
print(s.items) elif
choice==4:
def size(Q): # Size of the queue i.e. total no. of elements in queue
return len(Q.items)
q = Queue( )
print("MENU BASED QUEUE")
cd=True while cd:
print(" 1. ENQUEUE ")
print(" 2. DEQUEUE ")
print(" 3. Display ")
print(" 4. Size of Queue ")
print(" 5. Value at rear ")
if choice==1:
val=input("Enter the element:
") q.Enqueue(val) elif
choice==2: if q.items==[ ]:
print("Queue is empty") else:
print("Deleted element is :", q.Dequeue( ))
elif choice==3: print(q.items) elif
choice==4:
print("Size of the queue is :", q.size( ))
elif choice==5: print("Value of rear
element is :", q.peek( )) else:
print("You enetered wrong choice ")
for k in L: key=k if
key not in d:
count=L.count(key)
d[key]=count
word_counter = collections.Counter(d)
fin.close()
the : 505
OUTPUT:
a : 297 is
: 247 in :
231 to :
214
Find the name and salary of those employees whose salary is between 35000
B.
and 40000.
SELECT Ename, salary
SOLUTION
FROM EMPLOYEE
WHERE salary BETWEEN 35000 and 40000;
C. Find the name of those employees who live in guwahati, surat or jaipur city.
SELECT Ename, city
SOLUTION FROM EMPLOYEE
WHERE city IN(‘Guwahati’,’Surat’,’Jaipur’);
D. Display the name of those employees whose name starts with ‘M’.
SELECT Ename
SOLUTION
FROM EMPLOYEE
WHERE Ename LIKE ‘M%’;
33. Write a program to connect Python with MySQL using database connectivity
and perform the following operations on data in database: Fetch, Update and
delete the data.
A. CREATE A TABLE
import mysql.connector demodb =
mysql.connector.connect(host="localhost", user="root",
passwd="computer", database="EDUCATION")
SOLUTION
democursor=demodb.cursor( )
democursor.execute("CREATE TABLE STUDENT (admn_no int primary key,
sname varchar(30), gender char(1), DOB date, stream varchar(15), marks
float(4,2))")