Computer Science With Python (083) Practical File 2021-2022
Computer Science With Python (083) Practical File 2021-2022
PRACTICAL
NO. OBJECTIVE & SOLUTION
1. Write a program in python to check a number whether it is prime or not.
num=int(input("Enter the number: "))
for i in range(2,num):
if num%i==0:
SOURCE print(num, "is not prime number")
CODE: break;
else:
print(num,"is prime number")
OUTPUT:
OUTPUT:
5. Write a program to find the sum of all elements of a list using recursion.
def SumAll(L):
n=len(L)
if n==1:
return L[0]
SOURCE
else:
CODE:
return L[0]+SumAll(L[1:])
# main program
LS=eval(input("Enter the elements of list: "))
total=SumAll(LS)
print("Sum of all elements is: ", total)
# main
if y==True:
print("String is Palindrome")
else:
print("String is Not Palindrome")
print(count)
9
OUTPUT:
Write a program to write those lines which have the character 'p' from
11.
one text file to another text file.
fin=open("E:\\book.txt","r")
fout=open("E:\\story.txt","a")
s=fin.readlines( )
for j in s:
SOURCE if 'p' in j:
CODE: fout.write(j)
fin.close()
fout.close()
OUTPUT: 16
13. Write a python program to write student data in a binary file.
import pickle
list =[ ] # empty list
while True:
roll = input("Enter student Roll No:")
sname = input("Enter student Name :")
student = {"roll":roll,"name":sname}
SOURCE
list.append(student)
CODE:
choice= input("Want to add more record(y/n) :")
if(choice=='n'):
break
file = open("student.dat","wb")
pickle.dump(list, file)
file.close( )
14. Write a python program to read student data from a binary file.
import pickle
file = open("student.dat", "rb")
SOURCE
list = pickle.load(file)
CODE:
print(list)
file.close( )
file.close( )
Enter roll number whose name you want to update in binary file :1202
Enter new name: Harish
OUTPUT:
Record Updated
16. Write a python program to delete student data from a 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
lst = []
for x in list:
if roll not in x['roll']:
lst.append(x)
SOURCE else:
CODE: 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 that you want to search in binary file :1202
OUTPUT: Name of student is: Harish
18. Write a program to perform read and write operation with .csv file.
import csv
def readcsv():
with open('C:\\ data.csv','rt')as f:
data = csv.reader(f)
for row in data:
print(row)
def writecsv( ):
with open('C:\\data.csv', mode='a', newline='') as file:
SOURCE writer = csv.writer(file, delimiter=',', quotechar='"')
CODE:
#write new record in file
writer.writerow(['4', 'Devansh', 'Arts', '404'])
SOURCE #Sq.py
CODE: class Square:
def init (self):
print("Square")
def Area(self, side):
self.a=side
print("Area of square is : ", self.a*self.a)
#Tri.py
class Triangle:
def init (self):
print("Trinagle")
#main.py
from Shape import Rect
from Shape import Sq
from Shape import Tri
Rectangle
Area of Rectangle is : 200
Square
OUTPUT: Area of square is : 100
Trinagle
Area of Triangle is : 24.0
22. Write a menu based program to perform the operation on stack in python.
def push(n):
L.append(n)
print("Element inserted successfully")
def Pop( ):
if len(L)==0:
print("Stack is empty")
else:
print("Deleted element is: ", L.pop( ))
def Display( ):
SOURCE print("The list is : ", L)
CODE:
def size( ):
print("Size of list is: ", len(L))
def Top( ):
if len(L)==0:
print("Stack is empty")
else:
print("Value of top is: ", L[len(L)-1])
#main program
L=[ ]
print("MENU BASED STACK")
cd=True
while cd:
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: ")
push(val)
elif choice==2:
Pop( )
elif choice==3:
Display( )
elif choice==4:
size( )
elif choice==5:
Top( )
else:
print("You enetered wrong choice ")
def Dequeue():
if len(L)==0:
print("Queue is empty")
elif len(L)==1:
print("Deleted element is: ", L.pop(0))
print("Now queue is empty")
SOURCE
else:
CODE: print("Deleted element is: ", L.pop(0))
def Display():
if len(L)==0:
print("Queue is empty")
else:
print(L)
r=L[len(L)-1]
f=L[0]
print("Front is : ", f)
print("Rear is : ", r)
def size():
print("Size of queue is: ",len(L))
def FrontRear():
if len(L)==0:
print("Queue is empty")
else:
r=L[len(L)-1]
f=L[0]
print("Front is : ", f)
print("Rear is : ", r)
#main program
L =[]
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 Front and rear ")
if choice==1:
val=input("Enter the element: ")
Enqueue(val)
elif choice==2:
Dequeue( )
elif choice==3:
Display( )
elif choice==4:
size( )
elif choice==5:
FrontRear()
else:
print("You enetered wrong choice ")
SOLUTION:
#Command to enter in a databse
mysql> USE RAILWAYS;
Database changed
Find the name and salary of those employees whose salary is between
B.
35000 and 40000.
SELECT Ename, salary
FROM EMPLOYEE
SOLUTION:
WHERE salary BETWEEN 35000 and 40000;
Find the name of those employees who live in guwahati, surat or jaipur
C.
city.
SELECT Ename, city
FROM EMPLOYEE
SOLUTION:
WHERE city IN(„Guwahati‟,‟Surat‟,‟Jaipur‟);
D. Display the name of those employees whose name starts with „M‟.
SELECT Ename
FROM EMPLOYEE
SOLUTION:
WHERE Ename LIKE „M%‟;
SELECT Ename
FROM EMPLOYEE
SOLUTION:
WHERE Dept IS NULL;
Find maximum salary of each department and display the name of that
H. department which has maximum salary more than 39000.
27. 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")
democursor=demodb.cursor( )
SOLUTION: 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))")
import mysql.connector
SOLUTION:
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( )