BAL BHARTI
SCHOOLBAHADURGARH
PRACTICAL FILE
CLASS XII
SUBJECT – COMPUTER SCIENCE
SUBMITTED TO: SUBMITTED BY:
MR. PUNEET MOHAN KANASU JOON
SECTION – C
INDEX
PAGE NO. PROGRAM T. SIGN
Define a function (with calling statement)
6 Sum() and pass two numbers as arguments
and display their sum.
Define a function (with calling statement)
7 Natural_Sum() to input a number and return
sum of all natural numbers.
Define a function (with calling statement)
8 Factorial() and pass a number as argument
and print its factorial.
Define a function (with calling statement)
Range_Sum() and pass two numbers as
9
arguments and sum of numbers between
them.
Define a function (with calling statement)
10 Table() and print table of a number entered by
user.
Define a function (with calling statement)
11 Prime(a) and check whether the number
passed as an argument is prime or not.
Define a function (with calling statement)
12 Palindrome(a) and return 1 if it is palindrome
else return 0.
Define a function (with calling statement)
13 Read_data() to read a text file line by line and
display each word separated by a $.
Define a function (with calling statement)
Read_vowels to read a text file and display
14-15 the number of
vowels/consonants/uppercase/lowercase
characters in the file.
Define a function (with calling statement)
16-17 Del() to remove all the lines that contain the
character 'a' in a file and display those lines.
Write a program to Create a binary file
18-19 Student.dat containing name, Class, Section,
Gender and Roll number.
Write a program to Create a binary file
Student.dat and define a function (with
20-21 calling statement) Search for a given roll
number and display the name, if not found
display appropriate message.
Write a program to Create a binary file with
roll number, name and marks. Input a roll
22-23 number and Define a function (with calling
statement) update() to update the marks
entered by user.
Define a function (with calling statement)
Write a random number generator that
24
generates random numbers between 1 and 6
(simulates a dice).
Define a function (with calling statement)
Create a CSV file by entering user_id and
25-26
password, read and search the password for
given user_id.
Write a menu driven program to implement a
stack using list (Define functions Push_list()
27-28 to insert record, Pop_list() to delete record,
Display_list() to display record. [Record
contain Rollno and Name].
Create table Teacher having ID int, Name
char(90), Department char(40), Hiredate(30),
29
Category char(4), Gender char(4), Salary
float.
30 Insert 10 Records in the table.
To display all information about teachers of
31
Female PGT Teachers.
To list names, departments, and date of
32 hiring of all the teachers in descending order
of date of joining.
To count the number of teachers and sum of
33
their salary department wise.
To display all information about teachers
34
whose name starts with “A”.
To increase the salary by 500 of those
35
teachers whose category is PGT.
36 Create table Trainer and Course.
37 Insert 10 Records in the table.
Display all details of Trainers who are living in
38
city CHENNAI.
Display the Trainer Name, City & Salary in
39
descending order of their Hiredate.
Count & Display the number of Trainers in
40
each city.
Display the Course details which have Fees
41
more than 12000 and name ends with ‘A’.
Display the Trainer Name & Course Name
42 from both tables where Course Fees is less
than 10000.
43 Create Table Customer and Bank.
44 Insert 10 records in both tables.
To display data for all customers whose
45
transaction is between 8 and 11.
To display data for all customers sorted by
46
their dateofopen.
To count the number of customers with
47
amount
list the minimum and maximum amount from
48
the table bank.
To increase the amount with 1000 of those
49 customers whose transactions is more than
10.
Program 1: Define a function (with calling statement) Sum() and pass two
numbers as arguments and display their sum.
#A function Sum() to display the sum of two numbers as arguments
def Sum():
a=int(input("Enter first number="))
b=int(input("Enter second number="))
Sum=a+b
print("Sum of the two numbers=",Sum)
Sum()
Output:
Program 2: Define a function (with calling statement) Natural_Sum() to input a
number and return sum of all natural numbers.
def Natural_Sum():
n=int(input("Enter a number"))
if n<=0:
print("Enter a positive number only")
else:
sum=(n*(n+1))/2
print("Sum of natural numbers up to",n,"is:",sum)
Natural_Sum()
Output:
Program 3: Define a function (with calling statement) Factorial() and pass a
number as argument and print its factorial.
def Factorial(n):
f=1
for i in range(n,0,-1):
f=f*i
print("Factorial of the number is",f)
m=int(input("Enter a number"))
Factorial(m)
Output:
Program 4: Define a function (with calling statement) Range_Sum() and pass
two numbers as arguments and sum of numbers between them.
def Range_Sum():
a=int(input("Enter starting range:"))
b=int(input("Enter end range:"))
Sum=0
for i in range(a,b+1):
Sum+=1
print("Sum of the numbers in the range:",Sum)
Range_Sum()
Output:
Program 5: Define a function (with calling statement) Table() and print table of
a number entered by user.
def Table():
a=int(input("Enter any number"))
for i in range(1,11):
j=a*i
print("Table of",a,":",j)
Table()
Output:
Program 6: Define a function (with calling statement) Prime(a) and check
whether the number passed as an argument is prime or not.
def Prime(a):
i=2
f=0
while i<a:
if a%i==0:
f=1
break
i+=1
if f==1:
print("Not prime")
else:
print(a,"is a prime number")
a=int(input("Enter a number"))
Result= Prime(a)
Output:
Program 7: Define a function (with calling statement) Palindrome(a) and return
1 if it is palindrome else return 0.
def Palindrome(a):
b = str(a)
c = b[::-1]
if b==c:
return 1
else:
return 0
a=int(input("Enter a number"))
result= Palindrome(a)
print(result)
Output:
Program 8: Define a function (with calling statement) Read_data() to read a text
file line by line and display each word separated by a $.
def Read_Data():
n=int(input("Enter no. of lines to be entered"))
with open("data.txt", "w") as f:
for i in range(n):
k=input("Enter line")
f.write(k)
with open("data.txt",'r') as q:
p=q.read()
words=p.split()
new_words=""
words=p.split()
new_word="$".join(words)
print(new_word)
Read_Data()
Output:
Program 9: Define a function (with calling statement) Read_vowels to read a
text file and display the number of vowels/consonants/uppercase/lowercase
characters in the file.
def Read_vowels():
q=open("file.txt","w")
k=input("Enter a string")
q.write(k)
q.close()
vowels=0
consonants=0
upperc=0
lowerc=0
x="aeiouAEIOU"
n=open("file.txt","r")
for i in k:
if i in x:
vowels+=1
if i not in x:
consonants+=1
if i.isupper():
upperc+=1
if i.islower():
lowerc+=1
print("No. of Vowels=",vowels)
print("No. of consonants=",consonants)
print("No. of uppercase characters=",upperc)
print("No. of lowercase characters=",lowerc)
Read_vowels()
Output:
Program 10: Define a function (with calling statement) Del() to remove all the
lines that contain the character 'a' in a file and display those lines.
def Del():
n=int(input("Enter no. of lines to be entered"))
with open("data.txt", "w") as f:
for i in range(n):
k=input("Enter line")
f.write(k)
with open("data.txt", "r") as file:
lines = file.readlines()
lines_to_keep = []
lines_to_remove = []
for line in lines:
if 'a' in line:
lines_to_remove.append(line)
else:
lines_to_keep.append(line)
with open("data.txt", "w") as file:
file.writelines(lines_to_keep)
print("Lines removed (containing 'a'):")
for line in lines_to_remove:
print(line.strip())
Del()
Output:
Program 11: Write a program to Create a binary file Student.dat containing
name, Class, Section, Gender and Roll number.
import pickle
record=[]
while True:
name=input("Enter name")
Class=int(input("Enter class"))
section=input("Enter section in capital letter only")
gender=input("Enter gender")
roll=int(input("Enter roll number"))
data= [name,Class,section,roll]
record.append(data)
choice= input("Want to enter more data? Y for Yes and N for No")
if choice.upper()=="N":
break
out=open("Student.dat","wb")
pickle.dump(record,out)
print("Record added")
out.close()
Output:
Program 12: Write a program to Create a binary file Student.dat and define a
function (with calling statement) Search for a given roll number and display the
name, if not found display appropriate message.
import pickle
def Search_Roll():
try:
x=int(input("Enter a roll number to search:"))
with open("Student.dat","rb") as inf:
stu=pickle.load(inf)
found= False
for student in stu:
if student[3]==x:
print("Name;",student[0])
found= True
break
if not found:
print("Student data not found")
except FileNotFoundError:
print("File'Student.dat'not found")
except ValueError:
print("Invalid Input. Please enter a valid roll number")
Search_Roll()
Output:
Program 13: Write a program to Create a binary file with roll number, name and
marks. Input a roll number and Define a function (with calling statement)
update() to update the marks entered by user.
import pickle
def write():
f=open("students.dat","wb")
while True:
r=int(input("enter roll number"))
n=input("enter name")
m=int(input("enter marks"))
record=[r,n,m]
pickle.dump(record,f)
ch=input("Do you want to enter more records? Y/N")
if ch=="N":
break
f.close()
def Read():
f=open("students.dat","rb")
try:
while True:
rec=pickle.load(f)
print(rec)
except EOFError:
f.close()
def Update():
f=open("students.dat","rb+")
rollno=int(input("Enter roll no. whose marks you want to update"))
try:
while True:
pos=f.tell()
rec=pickle.load(f)
if rec[0]==rollno:
um=int(input("Enter updated marks:"))
rec[2]=um
f.seek(pos)
pickle.dump(rec,f)
except EOFError:
f.close()
write()
Read()
Update()
Read()
Output:
Program 14: Define a function (with calling statement) Write a random number
generator that generates random numbers between 1 and 6 (simulates a dice).
import pickle
import random
def random_dice():
return random.randint(1,6)
result= random_dice()
print("random number generator/dice simulator",result)
Output:
Program 15: Define a function (with calling statement) Create a CSV file by
entering user_id and password, read and search the password for given
user_id.
import csv
fields= ['user_id','password']
rows= [['helloall12345','abcde'],['youtube234','helloe34'],
['python1234567','language567']]
with open("stu.csv",'w', newline="") as f:
cs=csv.writer(f,delimiter=",")
cs.writerow(fields)
for i in rows:
cs.writerow(i)
print("Record written")
s=input("Enter user id to be searched")
f=open('stu.csv','r')
cs1=csv.reader(f)
for r in cs1:
if r[0]==s:
print("Your password is=",r[1])
f.close()
Output:
Program 16: Write a menu driven program to implement a stack using list
(Define functions Push_list() to insert record, Pop_list() to delete record,
Display_list() to display record. [Record contain Rollno and Name].
stack=[]
def Push_list():
roll_no=int(input("Enter roll no.:"))
name=input("Enter name")
record=[roll_no,name]
stack.append(record)
print("Record added to stack")
def Pop_list():
if not stack:
print("Stack is empty, cannot pop")
else:
popped_record=stack.pop()
print("Popped record:",popped_record)
def Display_list():
if not stack:
print("Stack is empty")
else:
print("Records on your screen:")
for record in stack:
print("Rollno{record[0]},Name{record[1]}")
while True:
print('/nMenu')
print("1. Push Record")
print("2. Pop Record")
print("3. Display Records")
print("4. Exit program")
choice= int(input("Enter your choice"))
if choice==1:
Push_list()
elif choice==2:
Pop_list()
elif choice==3:
Display_list()
elif choice==4:
print("Thank you for using our program")
break
else:
print("Invalid choice. Please select a valid option")
Output:
SQL Commands :
Program 17(i): Create table Teacher having ID int, Name char(90), Department
char(40), Hiredate(30), Category char(4), Gender char(4), Salary float.
Program 17(ii): Insert 10 records into the table.
Program 17(iii): To display all information about teachers of Female PGT
Teachers.
Program 17(iv): To list names, departments, and date of hiring of all the
teachers in descending order of date of joining.
Program 17(v): To count the number of teachers and sum of their salary
department wise.
Program 17(vi): To display all information about teachers whose name starts
with “A”.
Program 17(vii): To increase the salary by 500 of those teachers whose
category is PGT.
Program 18(i): Create Table Trainer and Course.
Program 18(ii): Insert 10 Records in the table.
Program 18(iii): Display all details of Trainers who are living in city CHENNAI.
Program 18(iv): Display the Trainer Name, City & Salary in descending order of
their Hiredate.
Program 18(v): Count & Display the number of Trainers in each city.
Program 18(vi): Display the Course details which have Fees more than 12000
and name ends with ‘A’.
Program 18(vii): Display the Trainer Name & Course Name from both tables
where Course Fees is less than 10000.
Program 19(i): Create Table Customer and Bank.
Program 19(ii): Insert 10 records in both tables.
Program 19(iii): To display data for all customers whose transaction is between
8 and 11.
Program 19(iv): To display data for all customers sorted by their dateofopen.
Program 19(v): To count the number of customers with amount.
Program 19(vi): List the minimum and maximum amount from the table bank.
Program 19(vii): To increase the amount with 1000 of those customers whose
transactions is more than 10.
THANKYOU