SOMYA-XII A-60-CS PRACTICALS (Final)
SOMYA-XII A-60-CS PRACTICALS (Final)
SOMYA-XII A-60-CS PRACTICALS (Final)
VIDYALAYA
Computer Science
PRACTICAL FILE
` 1
CERTIFICATE:
` 2
CONTENT
4. Write a python function rev (Inp) to reverse elements of an Input list Inp 10-11
5. Write a python program to read contents of a text file and display number of 11-12
vowels, consonants, uppercase and lowercase characters in a file.
7. Remove all the lines that contain the character 'a' in a file and write it to 14-15
another file
8. Write a Python program to read specific columns of a given CSV file and 16-17
print the content of the columns.
9. Create a binary file with name and roll number. Search for a given roll 17-21
number and display the name, if not found display appropriate message.
10. Write a random number generator that generates random numbers between 22-23
1 and 6 (simulates a dice).
12. Write a function in Python ADD (Arr), where Arr is a list of numbers. 24-25
From this list push all numbers divisible by 5 into a stack implemented by
using a list. Display the stack if it has at least one element, otherwise
display appropriate error message.
` 3
Sno. Questions Page no.
. Write a function RShift( Input) in Python, which accepts a list of numbers 26-27
13. and shift all elements of list to one position right
14. Take a sample of ten phishing e-mails (or any text file) and find most 27-28
commonly occurring word(s).
15. Write a python program to create table employee (first name, last name, 29-30
age, sex, income) in MySQL using database connectivity.
16. Write a menu driven program in python to implement database connectivity 31-35
Write input, delete, update and show record function with help of any
suitable example.
17. Write a function manipulate (Inp) in Python, which accepts a list Inp of 36-37
numbers The function s performs following manipulations: The elements
which are multiple of 3 are divided by 3 and elements which are multiple of
5 are doubled.
18. Write a function in python square cube (Inp) in Python, which accepts a list 38-39
Inp of numbers and turn first half elements of the list into its square and
second half of thelist into its cube
19. Write a python program that removes all the occurrences of user entered 39-40
element from list and generates a new list.
20. A binary file Book.dat has a structure [book_no, name, Author, price]. 41-42
Write a function CountRec(Author) in Python which accepts the Author
name as parameter and count and return number of books by the given
Author are stored in the binary file "Book.dat".
21. A. Write SQL commands of the following statements; 43-45
B. Give the output of the following SQL queries :
QUESTION 1:
Write a function LEXCHANGE (Input) in Python, which accepts a list Input.
Function will replace element at odd position with even position elements.
CODE:
def leexchange():
for i in range(0,num-1,2):
first=lst[i]
second=lst[i+1]
lst[i]=second
lst[i+1]=first
print("new list:",lst)
lst=[]
for n in range(num):
lst.append(numbers)
print("original list:",lst)
leexchange()
` 5
OUTPUT:
QUESTION 2:
CODE:
s=[]
c="y"
print("1.PUSH")
print("2.POP")
print("3.DISPLAY")
if choice==1:
` 6
roll=int(input("enter your roll no"))
std=(name,roll)
s.append(std)
elif choice==2:
if s==[]:
print("stack empty")
else:
name,roll =s.pop()
elif choice==3:
i=len(s)
while i>0:
print(s[i-1])
i=i-1
else:
print("wrong input")
` 7
OUTPUT:
` 8
QUESTION 3:
s={}
file2=open("student.dat",'wb')
N=input("Enter name")
M=float(input("Enter marks"))
s['Rollno']=R
s['Name']=N
s['Marks']=M
pickle.dump(s,file2)
file2.close()
OUTPUT:
9
QUESTION 4:
Write a python function rev (Inp) to reverse elements of an Input list Inp
CODE:
def reverse():
for i in range(len(L)):
reversedList.append(L[len(L)-i-1])
print("reversedList",reversedList)
reversedList=[]
L=[]
for i in range(0,n):
L.append(ele)
print("original list",L)
reverse()
` 10
OUTPUT:
CODE:
def cnt():
f=open("test.txt","r")
cont=f.read()
print(cnt)
v=0
lc=0
uc=0
cons=0
for i in cont:
` 11
if(i.islower()):
lc+=1
elif(i.isupper()):
uc+=1
if(i in ['a','e','i','o','u',]):
v+=1
elif(i.isalpha()):
cons+=1
f.close()
print("vowels=",v)
print("consonants=",cons)
print("lowercase=",lc)
print("uppercase=",uc)
cnt()
OUTPUT:
` 12
QUESTION 6:
CODE:
n=int(input("enter no. of dict element"))
d=dict()
for i in range(n):
key=int(input("enter key"))
value=int(input("enter value"))
d[key]=value
n=len(d)
count=0
d=list(d.items())
print(d)
if(ele==d[i][0] or ele==d[i][1]):
count+=1
print("FREQUENCY:", count)
` 13
OUTPUT:
QUESTION 7:
Remove all the lines that contain the character 'a' in a file and write it to
another file.
CODE:
def remove():
fo=open(file1,"r")
fn=open(file2,"w")
l=[]
lines=fo.readlines()
if 'a' in line:
fn.write(line)
else:
l.append(line)
fo=open(file1,"w")
fo.writelines(l)
` 14
fo.close()
fn.close()
remove()
OUTPUT:
QUESTION 8:
Write a Python program to read specific columns of a given CSV file and
print the content of the columns.
` 15
CODE:
import csv
def write():
f=open("departments.csv","w",newline='')
wrt=csv.writer(f)
wrt.writerow(["Department_ID","Department_Name","Manager_ID"])
while True:
data=[d_id,d_name,m_id]
wrt.writerow(data)
if ch in 'Nn':
break
f.close()
def read():
f=open("departments.csv","r",newline='\r\n')
s_reader=csv.reader(f)
for i in s_reader:
print(i)
write()
read()
` 16
OUTPUT:
QUESTION 9:
Create a binary file with name and roll number. Search for a given
roll number and display the name, if not found display appropriate
message.
`17
CODE:
import pickle
def Writerecord(sroll,sname):
srecord={"SROLL":sroll,"SNAME":sname}
pickle.dump(srecord,Myfile)
def Readrecord():
print()
while True:
try:
rec=pickle.load(Myfile)
except EOFError:
break
def Input():
` 18
sroll=int(input("Enter Roll No: "))
Writerecord(sroll,sname)
def SearchRecord(roll):
while True:
try:
rec=pickle.load(Myfile)
if rec['SROLL']==roll:
print("Roll NO:",rec['SROLL'])
print("Name:",rec['SNAME'])
except EOFError:
print("Try Again..............")
break
def main():
while True:
` 19
print('1.Insert Records')
print('2.Dispaly Records')
if ch==1:
Input()
elif ch==2:
Readrecord()
elif ch==3:
SearchRecord(r)
else:
break
main()
` 20
OUTPUT:
` 21
QUESTION 10:
def rolladice():
counter = 0
myList = []
randomNumber = random.randint(1,6)
myList.append(randomNumber)
counter = counter + 1
if (counter)>=6:
pass
else:
return myList
n=1
while(n==1):
print(rolladice())
` 22
OUTPUT:
QUESTION 11:
emp_dict = { }
while True :
choice = input("Do you want to Enter another record Press 'y' if yes : ")
if choice != "y" :
break
print(emp_dict)
` 23
OUTPUT:
QUESTION 12:
s=[]
for i in range(0,len(Arr)):
if Arr[i]%5==0:
s.append(Arr[i])
if len(s)==0:
` 24
print("Stack is empty")
else:
print(s)
Arr =[]
for i in range(0,n):
ele=int(input("Enter element:"))
Arr.append(ele)
print(Arr)
ADD(Arr)
OUTPUT:
` 25
QUESTION 13:
n=len(L)
last=L[n-1]
for i in range(n-2,-1,-1):
L[i+1]=L[i]
L[0]=last
L)
L=[]
L.append(ele)
RShift(L)
` 26
OUTPUT:
QUESTION 14:
Take a sample of ten phishing e-mails (or any text file) and find most
commonly occurring word(s).
CODE:
file=open("email2.txt",'r')
content=file.read()
dict={}
words=content.split()
if word in dict:
dict[word]+=1
else:
` 27
dict[word]=1
fin_max=max(dict,key=dict.get)
OUTPUT:
` 28
QUESTION 15:
CODE:
import mysql.connector
mydb=mysql.connector.connect(host="localhost", user="root",
passwd="mysql", database="real_estate")
mycursor=mydb.cursor()
mycursor.close()
mydb.close()
OUTPUT:
`29
QUESTION 16:
while True:
print("--------CLIENT PROFILE---------")
print("1-Create table")
print("2-Insert details")
print("3-Update details")
print("4-Deletion of records")
print("5-Display details")
print("6-Main menu")
while True:
if choice==1:
mydb=mysql.connector.connect(host="localhost",
user="root",
passwd="mysql",
database="real_estate")
mycursor=mydb.cursor()
` 30
mycursor.execute ="CREATE TABLE client_detail(Id int(11) PRIMARY KEY NOT
NULL,Name VARCHAR(255), Address VARCHAR(255))"
mydb.close()
break
elif choice==2:
mydb=mysql.connector.connect(host="localhost",
user="root",passwd="mysql", database="real_estate")
mycursor=mydb.cursor()
adrs=str(input("Enter address"))
VALUES({},'{}','{}')". format(id,name,adrs)
mycursor.execute(sql)
mydb.commit()
mydb.close()
break
elif choice==3:
mydb=mysql.connector.connect(host="localhost",
user="root",
passwd="mysql",
` 31
database="real_estate")
mycursor=mydb.cursor()
mycursor.execute(query)
mydb.commit()
break
elif choice==4:
mydb=mysql.connector.connect(host="localhost",
mycursor=mydb.cursor()
mycursor.execute(query)
mydb.commit()
print("Deletion sccessfully")
break
elif choice==5:
mydb=mysql.connector.connect(host="localhost",
mycursor=mydb.cursor()
query="SELECT*from client_detail"
mycursor.execute(query)
` 32
myrecords=mycursor.fetchall()
print("Id,Name,Address are:")
for x in myrecords:
print (x)
print(mycursor.rowcount,"record selected")
mycursor.close()
mydb.close()
break
else:
break
OUTPUT:
` 33
`34
QUESTION 17:
Write a function manipulate (Inp) in Python, which accepts a list
Inp of numbers .The function is performs following
manipulations:The elements which are multiple of 3 are divided by
3 and elements which are multiple of 5 are doubled.
CODE:
def manipulate ():
if l1[i]%3==0:
l1[i]=l1[i]//3
elif l1[i]%5==0:
l1[i]=l1[i]*2
l1 = []
for n in range(num):
l1.append(numbers)
manipulate()
print("END OF FUNCTION")
` 35
OUTPUT:
` 36
QUESTION 18:
def square_cube(L,n):
h=int(n/2)
for i in range(0,h):
L[i]=L[i]*L[i]
L[j]=L[j]**3
L=[]
for i in range(0,n):
L.append(ele)
square_cube(L,n)
` 37
OUTPUT:
QUESTION 19:
Write a python program that removes all the occurrences of user entered
element from list and generates a new list.
CODE:
L=[]
for i in range(0,n):
L.append(ele)
print(L)
for i in L:
` 38
if i==x:
L.remove(x)
print(L)
OUTPUT:
QUESTION 20:
import pickle as p
def Createfile():
Author=input("Enter Author:")
Price=float(input("Enter Price"))
l=[BookNo,Book_Name,Author,Price]
f=open("Book.dat","ab")
` 39
p.dump(l,f)
f.close()
def Countrec(Author):
count=0
f=open("Book.dat","rb")
while True:
try:
l=p.load(f)
if l[2]==Author:
count=count+1
except EOFError:
print("EOF reached")
break
return count
OUTPUT:
` 40
QUESTION 21:
Consider the tables EMPLOYEE and SALGRADE given below and
answer (a) and (b) parts of this question.
TABLE : EMPLOYEE
TABLE: SGRADE
` 41
TABLE : EMPLOYEE AND SALGRADE
` 42
(a) Write SQL commands of the following statements;
(i) To display the details of all EMPLOYEE in descending order of DOJ.
ANS. SELECT * FROM EMPLOYEE ORDER BY DOJ DESC;
(iii) To display the content of all the EMPLOYEES table, whose DOJ is in
between ’09-Feb-2006′ and ’08-Aug-2009
` 43
(iv) To add a new row with the following: 109, ‘HarishRoy’, ‘HEAD-IT’,
‘SOX, ’09-Sep-2007′, ’21-Apr-1983’
(v) To display HRA of employees who were born after in year 1982 or 1987
ANS. SELECT HRA FROM EMPLOYEE WHOSE DOB BETWEEN ‘1982-
01-19’and 1987-06-22’
(vi) Display name, designation, salary and HRA of employees who have letter
'a' in their names.
` 44
(b)Give the output of the following SQL queries :
(i)SELECT COUNT(SGRADE), SGRADE FROM EMPLOYEE
GROUP BY SGRADE;
OUTPUT:
COUNT(SGRADE) SGRADE
2 S03
2 S02
1 S01
MIN(DOB) MAX(DOJ)
1980-01-13 2010-02-12
OUTPUT:
SGRADE SALARY+HRA
S02 44000
` 45