Shreya Class Xii CS Practical
Shreya Class Xii CS Practical
SCHOOL
Session :2022-23
CLASS – XII
Computer
SCIENCE(083)
Practical file
Submitted by:
submitted to:
1
Shreya tiwari Mrs. Cinderalla A
Augustine
2
1.User defined Functions to print Fibonacci series
nterms = int(input("How many terms? "))
3
2. User Defined Function for Factorial Number
def fact(n):
f=1
for i in range(1,n+1):
f=f*i
return(f)
#main
r=fact(n)
print("factorial of",n,"=",r)
4
3. User Defined Function for sum of series x0+x1+x2……….xn
def sseries(n,x):
s=0
for i in range(n+1):
s=s+x**i
return(s)
#main
r=sseries(n,x)
5
print("series of",x,"=",r)
f=1
for i in range(1,n+1):
f=f*i
return(f)
def sseries(n,x):
s=0
for i in range(n+1):
s=s+((x**i)/fact(i))
6
return(s)
#main
r=sseries(n,x)
r1=fact(n)
print("series of",n,"=",r)
print("factorial of",n,"=",r1)
7
5. User Defined by using Function of Palindrome Number
def palindrome(n):
s=0
t=n
while(n>0):
r=n%10
s=(s*10)+r
n=n//10
if(s==t):
return(True)
else:
return(False)
#main
res=palindrome(a)
8
if(res==True):
print(a,"is a Palindrome")
else:
9
6. Using UDF to print a pattern
def pattern(n):
for i in range(1,n+1):
for j in range(1,i+1):
print(i*j,end=' ')
print("\n")
#main
pattern(n)
10
7. User defined function for Binary Search
def bsearch(a,s,e,x):
if not s<e:
return(-1)
mid=(s+e)//2
if(a[mid]<x):
return bsearch(a,mid+1,e,x)
elif(a[mid]>x):
return bsearch(a,s,mid,x)
else:
return(mid)
11
#main
a=[]
for i in range(n):
a[i]=int(input())
a.sort()
index=bsearch(a,0,len(a),x)
if index<0:
else:
12
13
8. UDF TO FIND CAB FARE
KM FARE/KM
<=1KM 30
>1 AND <=5KM 25
>5 AND <=15KM 20
>15 AND <=35KM 15
>35KM 10
def bill(n):
b=0
if(n<=1):
b=b+(n*30)
elif(n>1 and n<=5):
b=b+(n*25)
elif(n>5 and n<=15):
b=b+(n*20)
elif(n>15 and n<=35):
b=b+(n*15)
elif(n>35):
b=b+(n*10)
return(b)
#main
n=int(input("enter the kilometers"))
bill=bill(n)
print("total bill=",bill)
14
9. Program to copy the contents where the lines begin by ‘A’
def copy(rfile,wfile):
for1=open(rfile,"r")
fow=open(wfile,"w")
l=[]
l=for1.readlines()
for i in range(len(l)):
if (l[i][0]=='A' or l[i][0]=='a'):
fow.write(l[i]+" ")
for1.close()
fow.close()
copy(rfile,wfile)
15
10. Program to read and write a dictionary to a file
import pickle
d={}
fo=open(r"xy.dat","wb")
n=int(input("Enter the value of n"))
for i in range(n):
k=input("Enter Customer name as key")
d[k]=input("Enter email id as value")
pickle.dump(d,fo)
fo.close()
fo=open(r"xy.dat","rb")
d1={}
for i in range(n):
d1=pickle.load(fo)
print(d1,"\n")
fo.close()
16
11. Program to accept details of employee as a list and insert into a binary file
import pickle
record=[]
while True:
eno=int(input("Enter employee number"))
name=input("Enter employee name")
dno=int(input("Enter dept number"))
salary=float(input("Enter employee salary"))
b=[eno,name,dno,salary]
record.append(b)
ch=input("Do you want to continue [y/n]:")
if ch.upper()=="N":
break
fo=open("emp.dat","wb")
pickle.dump(record,fo)
fo.close()
17
12. Program to read details of employee from the binary file
import pickle
fo=open("emp.dat","rb")
s=pickle.load(fo)
print(s)
for i in s:
eno=i[0]
name=i[1]
dno=i[2]
salary=i[3]
print(eno,name,dno,salary)
fo.close()
18
13. Program to search details of employee from the binary file
import pickle
fo=open("emp.dat","rb")
s=pickle.load(fo)
eno=int(input("Enter the no. to be searched: "))
found=0
for i in s:
if i[0]==eno:
eno=i[0]
name=i[1]
dno=i[2]
salary=i[3]
print(eno,name,dno,salary)
found=1
break
if found==0:
print("Not Found")
fo.close()
19
14. Program to update details of employee from the binary file
import pickle
fo=open("emp.dat","rb+")
emp_rec=pickle.load(fo)
eno=int(input("Enter the employee no. whose details has to be upd
ate "))
found=0
for r in emp_rec:
if (r[0]==eno):
ch1=input("Do you want to change the dept no.[y/n]")
if (ch1.lower()=='y'):
dn=int(input("Enter the new dept no."))
r[2]=dn
ch2=input("Do u want to change the salary[y/n]")
if (ch2.lower()=='y'):
sal=float(input("Enter the new salary[y/n]"))
r[3]=sal
found=1
break
if (found==1):
fo.seek(0)
pickle.dump(emp_rec,fo)
print("Employee details updated")
else:
print("Employee not found")
fo.close()
20
15. Program to read data from CSV file
import csv
fo=open("test.csv","r")
s=csv.reader(fo)
for row in s:
print(row)
fo.close()
21
16. Program to count the no. of records in CSV file
import csv
fo=open("test.csv","r")
s=csv.reader(fo)
c=0
for row in s:
c+=1
print(c)
fo.close()
22
17. Program to count the no. of records in CSV file without header
23
18. Program to write emp details to csv file each row at a time
24
19. Menu Driven program to read and write to CSV file
25
20. Program for Stack
#stack program
def isEmpty(stk):
if stk==[]:
return True
else:
return False
def Push(stk,item):
stk.append(item)
top=len(stk)-1
def Pop(stk):
if isEmpty(stk):
return"Underflow"
else:
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)-1
return item
def Peek(stk):
if isEmpty(stk):
return"Underflow"
26
else:
top=len(stk)-1
return stk[top]
def display(stk):
if isEmpty(stk):
print("Stack Empty")
else:
top=len(stk)-1
print(stk[top],"<-top")
print(stk[a])
#main
stack=[]
top=None
while True:
print("Stack Operations")
print("1.Push")
print("2.Pop")
print("3.Peek")
print("4.Display stack")
print("5.Exit")
27
ch=int(input("enter your choice(1-5):"))
if ch==1:
item=int(input("Enter item:"))
Push(stack,item)
elif ch==2:
item=Pop(stack)
if item=="Underflow":
print("Underflow!stack is empty!")
else:
elif ch==3:
item=Peek(stack)
if item=="Underflow":
print("Underflow!stack is empty!")
else:
elif ch==4:
display(stack)
elif ch==5:
break
else:
print("Invalid choice!")
28
29
21. Program to connect SQL table with Python to store Student details.
22. Menu Driven Program to connect SQL table with Python to store
Item details
30
Consider the table “ITEM” having the following fields
Itemcode varchar
Itemname varchar
Price float
i) Create the table ITEM in the mydb database
ii) Create a menu driven program in python to have
a) function for inserting records in the table
b) function for displaying all the records from the table item
c) Function for searching for a particular record on basis of Itemcode.
import sqlite3
def insert_data():
con=sqlite3.connect('db3.db')
cur=con.cursor()
con.commit()
con.close()
def read_all():
con=sqlite3.connect("db3.db")
31
cur=con.cursor()
d=cur.fetchall()
for i in d:
print(i)
con.close()
def update_data():
con=sqlite3.connect("db3.db")
cur=con.cursor()
print("rows=", cur.rowcount)
if (cur.rowcount==-1):
con.commit()
con.close()
def search_data():
con=sqlite3.connect("db3.db")
32
cur=con.cursor()
d=cur.fetchone()
print("rows=", cur.rowcount)
if (cur.rowcount==1):
print("Searched row=",d)
con.close()
else:
def delete_data():
con=sqlite3.connect("db3.db")
cur=con.cursor()
print("rows=", cur.rowcount)
if (cur.rowcount==1):
print("Deleted row=",code)
con.commit()
con.close()
else:
33
#main
while True:
if (ch==1):
insert_data()
elif(ch==2):
update_data()
elif (ch==3):
delete_data()
elif (ch==4):
read_all()
elif (ch==5):
search_data()
else:
break
34
35
23. Program to connect SQL table with Python to store Employee details.
import MySQLdb
db1 = MySQLdb.connect("localhost","root","","TESTDB" )
cursor.execute(sql)
import MySQLdb
db1 = MySQLdb.connect("localhost","root","","TESTDB" )
cursor = db1.cursor()
Prepareing SQL statement to insert one record with the given values sql =
"INSERT INTO EMP VALUES (1,'ANIL KUMAR',86000);"
try:
cursor.execute(sql)
db1.commit()
36
except:
db1.rollback()
db1.close()
Fetching all the records from EMP table having salary more than 70000.
import MySQLdb
db1 = MySQLdb.connect("localhost","root","","TESTDB" )
cursor = db1.cursor()
try:
cursor.execute(sql)
#using fetchall() function to fetch all records from the table EMP and store
in resultset
resultset = cursor.fetchall()
print (row)
37
except:
print ("Error: unable to fetch data")
db1.close()
Updating record(s) of the table using UPDATE
import MySQLdb
db1 = MySQLdb.connect("localhost","root","","TESTDB" )
cursor = db1.cursor()
#Preparing SQL statement to increase salary of all employees whose salary is
less than 80000
sql = "UPDATE EMP SET salary = salary +1000 WHERE salary<80000;"
try:
cursor.execute(sql)
db1.commit()
except:
db1.rollback()
db1.close()
38
import MySQLdb
db1 = MySQLdb.connect("localhost","root","","TESTDB" )
cursor = db1.cursor()
try:
cursor.execute(sql)
db1.commit()
except:
db1.rollback()
db1.close()
Output:
record(s) deleted
>>>
39
24 SQL Queries
40
SOLUTION :
g) Select count(*) from teacher where salary between 20000 and 30000;
41
Qno. 2
SOLUTION :
a. Select name,game from students s, sports sp
Where s.admno=sp.admno and address is not null;
42
Qno. 3
SOLUTION :
43
Qno.4
Study the above tables STAFF and SALARY and write SQL command for the
questions (i) to (v).
i. Display NAME of all the staff members who are in SALES having more
than 10 years’ experience from the table staff.
44
ii. Display the minimum ALLOWANCE of female staff.
iii. Display the average Basic Salary of all staff members working in
“Finance” department using the tables staff and salary.
SOLUTION :
45