0% found this document useful (0 votes)
3 views

Class 12 computer science report file

The document contains a series of Python programs covering topics such as file handling, functions, exception handling, and data structures. It also includes SQL queries and Python connectivity with MySQL for performing various database operations like SELECT, INSERT, UPDATE, and DELETE. Each section provides example code snippets demonstrating the respective concepts.

Uploaded by

vajom66527
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Class 12 computer science report file

The document contains a series of Python programs covering topics such as file handling, functions, exception handling, and data structures. It also includes SQL queries and Python connectivity with MySQL for performing various database operations like SELECT, INSERT, UPDATE, and DELETE. Each section provides example code snippets demonstrating the respective concepts.

Uploaded by

vajom66527
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

S.No.

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)

ch=input("Do you want to continue? (y/n):")

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)

ch=input("Do you want to continue?(y/n):")

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

r=int(input("Enter roll for search:"))

for i in s:

if i[0]==r:

print(i)

f=1

if(f==1):

print("Record is found")

else:

print("Record is not found")

write()
search()

▪ Working With Functions

# PROGRAM 1: -
@ To find sum of two numbers.

def sum(x,y):

s=x+y

print(s)

return(s)

x=int(input("Enter Number 1:"))

y=int(input("Enter Number 2:"))

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 uppercase letters:",uppercount)

print("Number of lowercase letters:",lowercount)

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

print("Simple interest =",s)

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)

ch=input("Do you want to continue? (yes/no):")

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:

print("Error opening file")

# 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:

print("Could not covert data to an integer.")

except ZeroDivisionError:

print("Division by zero error")

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()

cur.execute('select * from emp order by ename')

data = cur.fetchall()

for i in data:

print(i)

print("Total rows are ",cur.rowcount)

# 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"))

query = "delete from emp where ecode = {}".format(s)

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()

# For insert data

c = int(input("Enter code"))

n = input("Enter name")

d = input("Enter dept")

s = int(input("Enter sal"))

query = "insert into emp values({},'{}','{}',{})".format(c,n,d,s)

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 = "UPDATE EMP SET DEPT = 'ACC' WHERE ENAME = 'YYY'"

#query = "select * from emp where sal > {} and dept = '{}'".format(m,s)

cur.execute(query)

con.commit()

XXXXXXXXXXXXXXXX

You might also like