PM SHRI KV AMC,
LUCKNOW
Computer Science
Practical File
Submitted To –
Submitted By –
Roll number –
Class & Section –
Practical number 1:-
Q) Write a program to check a
number whether it is palindrome
or not.
SOURCE CODE:-
num=int(input("Enter a number : "))
n=num
res=0
while num>0:
rem=num%10
res=rem+res*10
num=num//10
if res==n:
print("Number is Palindrome")
else:
print("Number is not Palindrome")
OUTPUT:-
Enter a number : 6556
Number is Palindrome
Practical number 2:-
Q) Write a program to display
ASCII code of a character and
vice versa.
SOURCE CODE:-
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")
print("Do you want to continue? Y/N")
option=input()
if option=='y' or option=='Y':
var=True
else:
var=False
OUTPUT:-
Press-1 to find the ordinal value of a character
Press-2 to find a character of a value
1
Enter a character : a
97
Do you want to continue? Y/N
Y
Press-1 to find the ordinal value of a character
Press-2 to find a character of a value
2
Enter an integer value: 65
A
Do you want to continue? Y/N
Practical number 3:-
Q) Write a python program to
sum the sequence given below.
Take the input n from the user.
1+1/1!+1/2!+1/3!+⋯+1/n!
SOURCE CODE:-
def fact(x):
j=1
res=1
while j<=x:
res=res*j
j=j+1
return res
n=int(input("enter the number : "))
i=1
sum=1
while i<=n:
f=fact(i)
sum=sum+1/f
i+=1
print(sum)
OUTPUT:-
enter the number : 6
7.0
Practical number 4:-
Q) Write a program to calculate
the factorial of an integer using
recursion.
SOURCE CODE:-
def factorial(n):
if n == 1:
return n
else:
return n*factorial(n-1)
num=int(input("enter the number: "))
if num < 0:
print("Sorry, factorial does not exist for negative
numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of ",num," is ", factorial(num))
OUTPUT:-
enter the number: 5
The factorial of 5 is 120
Practical number 5:-
Q) Write a program to find sum
of all elements of a list using
recursion.
SOURCE CODE:-
def Sum_ListEle(LS,n):
if n==0:
return 0
else:
return LS[n-1]+Sum_ListEle(LS,n-1)
L=eval(input("enter a list of numbers : "))
size=len(L)
total=Sum_ListEle(L,size)
print("Sum of list elements is :", total)
OUTPUT:-
enter a list of numbers : [10,2,5,8,1]
Sum of list elements is : 26
Practical number 6:-
Q) Write a program to print
fibonacci series using recursion.
SOURCE CODE:-
def fibonacci(n):
if n<=1:
return n
else:
return(fibonacci(n-1)+fibonacci(n-2))
num=int(input("How many terms you want to display:
"))
for i in range(num):
print(fibonacci(i)," ", end=" ")
OUTPUT:-
How many terms you want to display: 8
0 1 1 2 3 5 8 13
Practical number 7:-
Q) Write a program to read a
text file line by line and display
each word separated by '#'.
SOURCE CODE:-
Text in file:
hello how are you?
python is case-sensitive language.
Output in python shell:
hello#how#are#you?#python#is#case-
sensitive#language.#
Practical number 8:-
Q) Write a program to count the
number of vowels present in a
text file.
SOURCE CODE:-
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':
count=count+1
print(count)
OUTPUT:-
9
Practical number 9:-
Q) Write a program to count
number of words in a file.
SOURCE CODE:-
fin=open("D:\\python programs\\Story.txt",'r')
str=fin.read( )
L=str.split()
count_words=0
for i in L:
count_words=count_words+1
print(count_words)
OUTPUT:-
16
Practical number 10:-
Q) Write a program to count the
number of times the occurrence
of 'is' word in a text file.
SOURCE CODE:-
fin=open("D:\\python programs\\Book.txt",'r')
str=fin.read( )
L=str.split( )
count=0
for i in L:
if i=='is':
count=count+1
print(count)
fin.close( )
OUTPUT:-
3
Practical number 11:-
Q) Write a program to write
those lines which have the
character 'p' from one text file to
another text file.
SOURCE CODE:-
fin=open("E:\\book.txt","r")
fout=open("E:\\story.txt","a")
s=fin.readlines( )
for j in s:
if 'p' in j:
fout.write(j)
fin.close()
fout.close()
OUTPUT:-
**Write contents of book.txt and story.txt
Practical number 12:-
Q) Write a program to update
the name of student by using its
roll number in a binary file.
SOURCE CODE:-
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
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
Practical number 13:-
Q) Write a program to search a
record using its roll number and
display the name of student. If
record not found then display
appropriate message.
SOURCE CODE:-
import pickle
roll = input('Enter roll number that you want to search
in binary file :')
file = open("student.dat", "rb")
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")
OUTPUT:-
Enter roll number that you want to search in binary
file :1202
Name of student is: Divya
Practical number 14:-
Q) Write a program to delete a
record from binary file.
SOURCE CODE:-
import pickle
roll = input('Enter roll number whose record you want
to delete:')
file = open("student.dat", "rb+")
list = pickle.load(file)
found = 0
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()
OUTPUT:-
Enter roll number whose record you want to
delete:1201
Record Deleted
Practical number 15:-
Q) Write a program to perform
read and write operation
with .csv file.
SOURCE CODE:-
import csv
def readcsv():
with open('C:\\Users\\ViNi\\Downloads\\
data.csv','rt')as f:
data = csv.reader(f)
for row in data:
print(row)
def writecsv( ):
with open('C:\\Users\\ViNi\\Downloads\\data.csv',
mode='a', newline='') as file:
writer = csv.writer(file, delimiter=',',
quotechar='"')
writer.writerow(['4', 'Devansh', 'Arts', '404'])
print("Press-1 to Read Data and Press-2 to Write data:
")
a=int(input())
if a==1:
readcsv()
elif a==2:
writecsv()
else:
print("Invalid value")
OUTPUT:-
Press-1 to Read Data and Press-2 to Write data:
1
['Roll No.', 'Name of student', 'stream', 'Marks']
['1', 'Anil', 'Arts', '426']
['2', 'Sujata', 'Science', '412']
['3', 'Shivani', 'Commerce', '448']
['4', 'Devansh', 'Arts', '404']
Practical number 16:-
Q) Create a table EMPLOYEE with
constraints.
SOLUTION:-
CREATE DATABASE Bank;
SHOW DATABASES;
Use Bank;
create table Employee(Ecode int primary key,Ename
varchar(20) NOT NULL, Dept varchar(15),City
varchar(15), sex char(1), DOB date, Salary float(12,2));
* Insert data into the table
insert into Employee
values(1001,"Atul","Production","Vadodara","M","199
2- 10-23",23000.50);
* Add a new column in a table.
ALTER TABLE EMPLOYEE ADD address varchar(50);
* Change the data-type and size of an
existing column.
ALTER TABLE EMPLOYEE MODIFY city char(30);
Practical number 17:-
Q) Write SQL queries using
SELECT, FROM, WHERE clause
based on EMPLOYEE table.
SOLUTION:-
* List the name of female employees in
EMPLOYEE table.
SELECT Ename FROM EMPLOYEE WHERE sex=’F’;
* Display the name and department of
those employees who work in surat
and salary is greater than 25000.
SELECT Ename, Dept FROM EMPLOYEE WHERE
city=’surat’ and salary > 25000;
* Display the name of those female
employees who work in Mumbai.
SELECT Ename FROM EMPLOYEE WHERE sex=’F’ and
city=’Mumbai’;
* Display the name of those employees
whose department is marketing or
RND.
SELECT Ename FROM EMPLOYEE WHERE
Dept=’marketing’ OR Dept=’RND’;
* List the name of employees who are
not males.
SELECT Ename, Sex FROM EMPLOYEE WHERE sex!=’M’;
Queries using
DISTINCT, BETWEEN,
IN, LIKE, IS NULL,
ORDER BY, GROUP BY,
HAVING
Practical number 18:-
Q) Display the name of
departments. Each department
should be displayed once.
SOLUTION:-
SELECT DISTINCT(Dept) FROM EMPLOYEE;
Practical number 19:-
Q) Find the name and salary of
those employees whose salary is
between 35000 and 40000.
SOLUTION:-
SELECT Ename, salary FROM EMPLOYEE WHERE salary
BETWEEN 35000 and 40000;
Practical number 20:-
Q) Find the name of those
employees who live in guwahati,
surat or jaipur city.
SOLUTION:-
SELECT Ename, city FROM EMPLOYEE WHERE city
IN(‘Guwahati’,’Surat’,’Jaipur’);
Practical number 21:-
Q) Display the name of those
employees whose name starts
with ‘M’.
SOLUTION:-
SELECT Ename FROM EMPLOYEE WHERE Ename LIKE
‘M%’;
Practical number 22:-
Q) List the name of employees
not assigned to any department.
SOLUTION:-
SELECT Ename FROM EMPLOYEE WHERE Dept IS NULL;
Practical number 23:-
Q) Display the list of employees
in descending order of employee
code.
SOLUTION:-
SELECT * FROM EMPLOYEE ORDER BY ecode DESC;
Practical number 24:-
Q) Find the average salary at
each department.
SOLUTION:-
SELECT Dept, avg(salary) FROM EMPLOYEE group by
Dept;
Practical number 25:-
Q) Find maximum salary of each
department and display the
name of that department which
has maximum salary more than
39000.
SOLUTION:-
SELECT Dept, max(salary) FROM EMPLOYEE group by
Dept HAVING max(salary)>39000;
Practical number 26:-
Q) 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.
SOLUTION:-
* CREATE A TABLE
import mysql.connector
demodb = mysql.connector.connect(host="localhost",
user="root",
passwd="computer", database="EDUCATION")
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))")
* INSERT THE DATA
import mysql.connector
demodb = mysql.connector.connect(host="localhost",
user="root",
passwd="computer", database="EDUCATION")
democursor=demodb.cursor( )
democursor.execute("insert into student values (%s,
%s, %s, %s, %s, %s)",
(1245, 'Arush', 'M', '2003-10-04', 'science', 67.34))
demodb.commit( )
* FETCH THE DATA
import mysql.connector
demodb = mysql.connector.connect(host="localhost",
user="root",
passwd="computer", database="EDUCATION")
democursor=demodb.cursor( )
democursor.execute("select * from student")
for i in democursor:
print(i)
* UPDATE THE RECORD
import mysql.connector
demodb = mysql.connector.connect(host="localhost",
user="root",
passwd="computer", database="EDUCATION")
democursor=demodb.cursor( )
democursor.execute("update student set marks=55.68
where admn_no=1356")
demodb.commit( )
* DELETE THE DATA
import mysql.connector
demodb = mysql.connector.connect(host="localhost",
user="root",
passwd="computer", database="EDUCATION")
democursor=demodb.cursor( )
democursor.execute("delete from student where
admn_no=1356")
demodb.commit( )