Class 12 computer science report file
Class 12 computer science report file
Title Teacher’s
Remarks
1. Python Program
• File Handling
• Working with Functions
• Exception Handling
• Data Structure
2. SQL Query
3. SQL - Python Connectivity
PYTHON PROGRAM
▪ File Handling
# PROGRAM 1: -
@ to calculate number of lines in a text file .
def calc_lines():
f=open("diary.txt","r")
b=f.readlines()
c=len(b)
print(c)
f.close
calc_lines()
# PROGRAM 2: -
@ to enter multiple records in binary file.
import pickle
def write():
f=open("bin.dat","wb")
record=[]
while True:
roll=int(input("Enter Roll:"))
name=input("Enter Name:")
marks=int(input("Enter Marks:"))
data=[roll,name,marks]
record.append(data)
if ch=="n":
break
pickle.dump(record,f)
f.close()
def read():
f=open("bin.dat","rb")
s=pickle.load(f)
for i in s:
roll=i[0]
name=i[1]
marks=i[2]
print(roll,name,marks)
f.close()
write()
read()
# PROGRAM 3: -
@ Create a CSV File.
import csv
f=open("student.csv","w")
s_write=csv.writer(f)
rec=[]
while True:
r=int(input("Enter Roll:"))
n=input("Enter Name:")
m=int(input("Enter Marks:"))
data=[r,n,m]
rec.append(data)
if ch=="n":
break
for i in rec:
s_write.writerow(i)
f.close()
f=open("student.csv","r",newline="\r\n")
s_reader=csv.reader(f)
for i in s_reader:
print(i)
f.close()
# PROGRAM 4: -
@ to open and read any text file.
def readfile():
f=open("diary.txt","r")
b=f.readline()
print(b)
f.close()
readfile()
# PROGRAM 5: -
@ To search for a record in a binary file.
def search():
f=open("bin.dat","rb")
s=pickle.load(f)
f=0
for i in s:
if i[0]==r:
print(i)
f=1
if(f==1):
print("Record is found")
else:
write()
search()
# PROGRAM 1: -
@ To find sum of two numbers.
def sum(x,y):
s=x+y
print(s)
return(s)
sum(x,y)
# PROGRAM 2: -
@ To write table of any two numbers.
def table():
a=int(input("Enter No:"))
for i in range(1,11):
b=a*i
print(a,"X",i,"=",b)
table()
# PROGRAM 3: -
@ To read a line and print its statistics.
line=input("Enter a line:")
lowercount=uppercount=0
digitcount=alphacount=0
for a in line:
if a.islower():
lowercount+=1
elif a.isupper():
uppercount+=1
elif a.isdigit():
digitcount+=1
if a.isalpha():
alphacount+=1
print("Number of alphabets:",alphacount)
print("Number of digits:",digitcount)
# PROGRAM 4: -
@ To find factorial of any number.
def fact():
n=int(input("Enter No:"))
f=1
for i in range(1,n+1):
f=i*f
print(f)
fact()
# PROGRAM 5: -
@ To calculate simple interest.
def Loop_SI(x,y,z):
s=(x*y*z)/100
ch="yes"
while ch=="yes":
a=int(input("Enter Amount:"))
b=int(input("Enter Rate:"))
c=int(input("Enter Time:"))
Loop_SI(a,b,c)
if ch=="no":
break
▪ EXCEPTION HANDLING
# PROGRAM 1: -
@ Handle exception while opening a file.
try:
my_file=open("myfile.txt","r")
print(my_file.read())
except:
# PROGRAM 2: -
@ To handle multiple exceptions.
try:
my_file=open("myfile.txt")
my_line=my_file.readline()
my_int=int(s.strip())
my_calculated_value=101/my_int
except IOError:
print("IOError occurred")
except ValueError:
except ZeroDivisionError:
except:
print("Unexpected error:")
else:
print("Hurray! No exception!")
▪ DATA STRUCTURE
# PROGRAM 1: -
@ To push an item in a stack.
def Push(stk,item):
stk.append(item)
top = len(stk)-1
#PROGRAM 2: -
@ To pop an item in a stack.
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
# PROGRAM 3: -
@ To check if stack is empty.
def isempty(stk):
if stk == []:
return True
else:
return False
SQL QUERY
SQL - PYTHON CONNECTIVITY
# PROGRAM1: -
@ Code for connecting to a MySQL database and
retrieve data from a table.
import mysql.connector
con=mysql.connector.connect(host='localhost',user='root',
password= 'mysql',database='xyz')
cur = con.cursor()
data = cur.fetchall()
for i in data:
print(i)
# PROGRAM 2: -
@ Code for connecting to a MySQL database and perform a
DELETE operation on a table.
import mysql.connector
con=mysql.connector.connect(host='localhost',user='root',
password= 'mysql',database='xyz')
cur = con.cursor()
s = int(input("Enter code"))
cur.execute(query)
con.commit()
# PROGRAM 3: -
@ Code for connecting to a MySQL database and perform an
INSERT operation to add a new record to a table.
import mysql.connector
con=mysql.connector.connect(host='localhost',user='root',
password= 'mysql',database='xyz')
cur = con.cursor()
c = int(input("Enter code"))
n = input("Enter name")
d = input("Enter dept")
s = int(input("Enter sal"))
cur.execute(query)
con.commit()
# PROGRAM 4: -
@ Code for connecting to a MySQL database and perform an
UPDATE operation on a table.
import mysql.connector
con=mysql.connector.connect(host='localhost',user='root',
password= 'mysql',database='xyz')
cur = con.cursor()
#query = "select * from emp where sal > {} and dept = '{}'".format(m,s)
cur.execute(query)
con.commit()
XXXXXXXXXXXXXXXX