Class-Xii: Subject - Computer Science (083) Practical File Solution 2020-21 Objective & Solution 1
Class-Xii: Subject - Computer Science (083) Practical File Solution 2020-21 Objective & Solution 1
OUTPUT:
OUTPUT:
CI= p*x-p
print("Compound interest is : ", round(CI,2))
OUTPUT:
print(count)
OUTPUT: 9
Write a program to write those lines which have the character 'p' from one text
7.
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
CODE: if 'p' in j:
fout.write(j)
fin.close()
fout.close()
OUTPUT: **Write contents of book.txt and story.txt
8. Write a program to count number of words in a file.
fin=open("D:\\python programs\\Book.txt",'r')
str=fin.read( )
L=str.split()
SOURCE
CODE: count_words=0
for i in L:
count_words=count_words+1
print(count_words)
OUTPUT: 16
Write a python function sin(x,n) to calculate the value of sin(x) using its taylor
9. series expansion up to n terms.
import math
def fact(k):
if k<=1:
return 1
else:
return k*fact(k-1)
SOURCE
CODE: step=int(input("How many terms : "))
x=int(input("Enter the value of x :"))
sum=0
for i in range(step+1):
sum+=(math.pow(-1,i)*math.pow(x,2*i+1))/fact(2*i+1)
OUTPUT:
Enter a number between 1 to 6 : 4
Sorry, Try again, The lucky number was : 1
11. Write a program to create a library in python and import it in a program.
#Rect.py
class Rectangle:
def init (self):
print("Rectangle")
SOURCE
CODE: def Area(self, length, width):
self.l=length
self.w=width
print("Area of Rectangle is : ", self.l*self.w)
#Sq.py
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
12. Write a program for linear search.
L=eval(input("Enter the elements: "))
SOURCE n=len(L)
CODE: 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:
print("Element not Found")
OUTPUT:
Enter the elements:[67,13,89,34,65,8,74,19]
The sorted list is : [8, 13, 19, 34, 65, 67, 74, 89]
14. Write a menu based program to perform the operation on stack in python.
def main():
s=[]
ch="y"
while True:
print( "1. PUSH" )
print( "2. POP " )
print( "3. Display")
choice=int(input("Enter your choice: "))
if (choice==1):
a=input("Enter any number :")
SOURCE
s.append(a)
CODE: elif (choice==2):
if (s==[]):
print( "Stack Empty")
else:
print( "Deleted element is : ",s.pop())
elif (choice==3):
l=len(s)
# To display elements from last element to first
for i in range(l-1,-1,-1):
print(s[i])
else:
print("Wrong Input")
ch=input("Do yo want to continue (y/n) ?,N to exit ")
if ch=='N' or ch=='n':
break
OUTPUT:
15. Write a program to find the most common words in a file.
import collections
SOURCE fin = open('E:\\email.txt','r')
CODE: a= fin.read()
d={ }
L=a.lower().split()
for word in L:
word = word.replace(".","")
word = word.replace(",","")
word = word.replace(":","")
word = word.replace("\"","")
word = word.replace("!","")
word = word.replace("&","")
word = word.replace("*","")
for k in L:
key=k
if key not in d:
count=L.count(key)
d[key]=count
fin.close()
#bcbDemo22.py
#Create and Display
SOURCE
CODE:
from pickle import load, dump
import os
import sys
def bfileCreate():
#Creating the dictionary
sd = {'Rollno':10,'Name':'Tom','Marks':550}
fname = input("Enter filename: ")
fout=open(fname,'wb')
choice='N'
while True :
rollno = int(input('Enter roll number: '))
name = input('Enter Name:')
marks = int(input('Enter Marks '))
sd['Rollno'] = rollno
sd['Name']=name
sd['Marks']=marks
dump(sd,fout)
choice =input("want to enter more data Y / N: ")
if choice == 'N' or choice=='n':
break
fout.close()
def bfileDisplay():
import pickle
fname = input("Enter filename you searched for: ")
if not os.path.isfile(fname):
print( "file does not exist")
else:
fin= open(fname,'rb')
try :
while True:
sd = {}
sd = pickle.load(fin)
print(sd)
except EOFError:
pass
fin.close()
pickle.dump(x,file)
file.close()
OUTPUT:
20. Write a program to perform read and write operation with .csv file.
import csv
SOURCE
def readcsv():
CODE: 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:
print(row)
def writecsv( ):
with open('C:\\Users\\ViNi\\Downloads\\data.csv', mode='a', newline='') as
file:
writer = csv.writer(file, delimiter=',', quotechar='"')
MYSQL -COMMANDS
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%’;
SELECT Ename
SOLUTION
FROM EMPLOYEE
WHERE Dept IS NULL;
Find maximum salary of each department and display the name of that department
H. which has maximum salary more than 39000.
22. Queries for Aggregate functions- SUM( ), AVG( ), MIN( ), MAX( ), COUNT( )
a. Find the average salary of the employees in employee table.
Solution:- SELECT avg(salary)
FROM EMPLOYEE;
b. Find the minimum salary of a female employee in EMPLOYEE table.
Solution:- SELECT Ename, min(salary)
FROM EMPLOYEE
WHERE sex=’F’;
c. Find the maximum salary of a male employee in EMPLOYEE table.
Solution:- SELECT Ename, max(salary)
FROM EMPLOYEE
WHERE sex=’M’;
d. Find the total salary of those employees who work in Guwahati city.
Solution:- SELECT sum(salary)
FROM EMPLOYEE
WHERE city=’Guwahati’;
e. Find the number of tuples in the EMPLOYEE relation.
Solution:- SELECT count(*)
FROM EMPLOYEE;
23. 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))")