Practical File
Practical File
A
Practical File
of
Python Program and SQL Queries
CERTIFICATE
This is to certify that BHAVIKA NEGI student of Class XII, AVN SCHOOL
HALDUKHATA KOTDWAR has completed the PRACTICAL FILE during the
academic year 2025-26 towards partial fulfillment of credit for the Computer
Science practical evaluation of CBSE and submitted satisfactory report, as
compiled in the following pages, under my supervision.
Total number of practical certified are: 21
S.No Page
Practical
. No
Python Programs
01 WAP to compute x n of given two integers x and n.
02 WAP to accept a number from the user and calculate factorial of a number.
03 WAP to accept any number from user and check it is Prime no. or not.
n
Program -1 WAP to compute x of given two integers x and n.
Code:
Enter a number 2
Enter power 3
8
Code:
import math
num = int(input("Enter any number :"))
isPrime=True
for i in range(2,int(math.sqrt(num))+1):
if num % i == 0:
isPrime=False
if isPrime:
print("## Number is Prime ##")
else:
print("## Number is not Prime ##")
********Output of the program********
Code:
Code:
Code:
Code:
Code:
Program 9: Write a Python Program to search any word in given
string /sentence.
Code:
def countWord(str1,word):
s = str1.split()
count=0
for w in s:
if w==word:
count+=1
return count
str1 = input("Enter any sentence :")
word = input("Enter word to search in sentence :")
count = countWord(str1,word)
if count==0:
print("## Sorry! ",word," not present ")
else:
print("## ",word," occurs ",count," times ## ")
Code:
f = open("file1.txt")
for line in f:
words = line.split()
for w in words:
print(w + '#',end=' ')
print()
f.close()
India#is#my#country#
I#love#python#
Python#learning#is#fun#
Program 11: Write a Python Program to create binary file to store
Rollno, Name and Marks and update marks of entered Rollno.
Code:
import pickle
student=[]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number :"))
name = input("Enter Name :")
marks = int(input("Enter Marks :")) student.append([roll,name,marks])
ans=input("Add More ?(Y)")
pickle.dump(student,f)
f.close()
f=open('student.dat','rb+')
student=[]
while True:
try:
student = pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
r = int(input("Enter Roll number to update :"))
for s in student:
if s[0]==r:
print("## Name is :",s[1], " ##")
print("## Current Marks is :",s[2]," ##")
m = int(input("Enter new marks :"))
s[2]=m
print("## Record Updated ##")
found=True
break
if not found:
print("####Sorry! Roll number not found ####")
ans=input("Update more ?(Y) :")
f.close()
********Output of the program********
Code:
import csv
with open('myfile.csv',mode='a') as csvfile:
mywriter = csv.writer(csvfile,delimiter=',')
ans='y'
while ans.lower()=='y':
eno=int(input("Enter Employee Number "))
name=input("Enter Employee Name ")
salary=int(input("Enter Employee Salary :"))
mywriter.writerow([eno,name,salary])
print("## Data Saved... ##")
ans=input("Add More ?")
ans='y'
with open('myfile.csv',mode='r') as csvfile:
myreader = csv.reader(csvfile,delimiter=',')
while ans=='y':
found=False
e = int(input("Enter Employee Number to search :"))
for row in myreader:
if len(row)!=0:
if int(row[0])==e:
print("=========================")
print("NAME :",row[1])
print("SALARY :",row[2])
found=True
break
if not found:
print("==========================")
print(" EMPNO NOT FOUND")
print("==========================")
ans = input("Search More ? (Y)")
********Output of the program********
Enter Employee Number 1
Enter Employee Name Amit
Enter Employee Salary :90000
## Data Saved... ##
Add More ?y
Enter Employee Number 2
Enter Employee Name Sunil
Enter Employee Salary :80000
## Data Saved... ##
Add More ?y
Enter Employee Number 3
Enter Employee Name Satya
Enter Employee Salary :75000
## Data Saved... ##
Add More ?n
Solution:
(i). Select GameName, GCode from GAMES;
(ii). Select * from GAMES where PeizeMoney > 7000;
(iii). Select * from GAMES order by ScheduleDate;
(iv). Select sum(PeizeMoney), Type from GAMES group by Type;
Program 16: Write SQL queries for (i) to (iv) and find outputs for SQL
queries (v) to (viii),
which are based on the tables “COMPANY” and
“CUSTOMER”.
I. To display those company name which are having prize less than 30000.
II. To display the name of the companies in reverse alphabetical order.
III. To increase the prize by 1000 for those customer whose name starts with S?
IV. To add one more column totalprice with decimal( 10,2) to the table customer
V. SELECT COUNT(*) , CITY FROM COMPANY GROUP BY CITY;
VI. SELECT MIN(PRICE), MAX(PRICE) FROM CUSTOMER WHERE QTY>10;
VII. SELECT AVG(QTY) FROM CUSTOMER WHERE NAME LIKE “%r%;
VIII. SELECT PRODUCTNAME,CITY, PRICE FROM COMPANY, CUSTOMER WHERE
COMPANY. CID=CUSTOMER.CID AND PRODUCTNAME=”MOBILE”;
Solution:
I. To display Name and Price of all the Accessories in ascending order of their Price.
II. To display Id and SName of all Shop located in Nehru Place.
III. To display Minimum and Maximum Price of each Name of Accessories.
IV. To display Name, Price of all Accessories and their respective SName where they are available.
V. SELECT DISTINCT NAME FROM ACCESSORIES WHERE PRICE> =5000;
VI. SELECT AREA, COUNT(*) FROM SHOP GROUP BY AREA;
VII. SELECT COUNT (DISTINCT AREA) FROM SHOP;
VIII. SELECT NAME, PRICE*0.05 as DISCOUNT FROM ACCESSORIES WHERE ID IN (‘SO2‘,’SO3‘);
Solution:
V. VI.
VII. VIII.
Program 18: Program to connect with database and store record of
employee and display
records.
Code:
import mysql.connector as mycon
con = mycon.connect(host="localhost",user="root",password="",database="company")
cur = con.cursor()
cur.execute("create database if not exists company")
cur.execute("use company")
cur.execute("create table if not exists employee(empno int, name varchar(20), dept varchar(20),salary int)")
con.commit()
choice=None
while choice!=0:
print("1. ADD RECORD ")
print("2. DISPLAY RECORD ")
print("0. EXIT")
choice = int(input("Enter Choice :"))
if choice == 1:
e = int(input("Enter Employee Number :"))
n = input("Enter Name :")
d = input("Enter Department :")
s = int(input("Enter Salary :"))
query="insert into employee values({},'{}','{}',{})".format(e,n,d,s)
cur.execute(query)
con.commit()
print("## Data Saved ##")
elif choice == 2:
query="select * from employee"
cur.execute(query)
result = cur.fetchall()
print("%10s"%"EMPNO","%20s"%"NAME","%15s"%"DEPARTMENT", "%10s"%"SALARY")
for row in result:
print("%10s"%row[0],"%20s"%row[1],"%15s"%row[2],"%10s"%row[3])
elif choice==0:
con.close()
print("## Bye!! ##")
else:
print("## INVALID CHOICE ##")
********Output of the program********
1. ADD RECORD
2. DISPLAY RECORD
0. EXIT
Enter Choice :1
Enter Employee Number :1
Enter Name :AMIT
Enter Department :SALES
Enter Salary :9000
## Data Saved ##
1. ADD RECORD
2. DISPLAY RECORD
0. EXIT
Enter Choice :1
Enter Employee Number :2
Enter Name :NITIN
Enter Department :IT
Enter Salary :80000
## Data Saved ##
1. ADD RECORD
2. DISPLAY RECORD
0. EXIT
Enter Choice :2
EMPNO NAME DEPARTMENT SALARY
1 AMIT SALES 9000
2 NITIN IT 80000
1. ADD RECORD
2. DISPLAY RECORD
0. EXIT
Enter Choice :0
## Bye!! ##
Program 19: Program to connect with database and search
employee number in table
employee and display record, if empno not found
display appropriate message.
Code:
import mysql.connector as mycon
con = mycon.connect(host='localhost',user='root',password="", database="company")
cur = con.cursor()
print("#"*40)
print("EMPLOYEE SEARCHING FORM")
print("#"*40)
print("\n\n") ans='y'
while ans.lower()=='y':
eno = int(input("ENTER EMPNO TO SEARCH :"))
query="select * from employee where empno={}".format(eno)
cur.execute(query)
result = cur.fetchall()
if cur.rowcount==0:
print("Sorry! Empno not found ")
else:
print("%10s"%"EMPNO", "%20s"%"NAME","%15s"%"DEPARTMENT", "%10s"%"SALARY")
for row in result:
print("%10s"%row[0],"%20s"%row[1],"%15s"%row[2],"%10s"%row[3])
ans=input("SEARCH MORE (Y) :")