Abhi Abhi
Abhi Abhi
COMPUTER SCIENCE
PRACTICLE FILE
GSBV BURARI
(2024-2025)
Table Of Content
S.No. Practical Name Page No. Teacher’s
Sign
PYTHON PRACTICALS
1. WAP in Python to find the factorial of a number using
function.
2. WAP in Python to implement default and positional
parameters.
3. Write a program in Python to input the value of x and n and
print the sum of the following series
1+x+x^2+x^3+ ---------------- x^n
4. WAP in Python to read a text file and print the number of
vowels and consonants in the file.
5. WAP in Python to read a text file and print the line or
paragraph starting with the letter ‘S’
6. WAP in Python to read a text file and print the number of
uppercase and lowercase letters in the file.
7. WAP in Python to create a binary file with name and roll
number of the students. Search for a given roll number and
display the name of student.
8. Create a binary file with roll_no, name and marks of some
students and update the marks of specific student.
9. Create a binary file with eid, ename and salary and update
the salary of the employee.
10. Create a text file and remove the lines from the file which
contains letter ‘K’
11. Create a binary file with 10 random numbers from 1 to 40
and print those numbers.
12. Write a program in Python to create a CSV file with the
details of 5 students.
13. WAP in Python to read a CSV file.
MYSQL PRACTICALS
1. Write a SQL query to create a database.
20. Use the Select command to get the details of the students
with marks more than 30.
21. Shiva, a student of class XII, created a table “CLASS”. Grade is
one of the columns of this table. Write the SQL query to find
the details of students whose grade have not been entered.
22. Shiva is using a table with the following details:
Students(Name, Class, Stream_id, Stream_Name)
Write the SQL query to display the names of students who
OUTPUT:
Enter a number: 4
The factorial of 4 is 24
Enter a number: 6
The factorial of 6 is 720
Enter a number: 10
The factorial of 10 is 3628800
Enter a number: 3
The factorial of 3 is 6
Enter a number: 1
The factorial of 1 is 1
Enter a number: 9
The factorial of 9 is 362880
#Default Parameter
def show(a,b,c=8):
print("Sum=",(a+b+c))
show(5,6)
show(5,6,10)
def show1(a,b=9,c=8):
print("Sum1=",(a+b+c))
show1(5) show1(5,6)
show1(5,6,10)
show2(a,b):
print("Sub=",(a-b))
show2(15,6)
show2(6,15)
OUTPUT:
Sum= 19
Sum= 21
Sum1= 22
Sum1= 19
Sum1= 21
Sub= 9
Sub= -9
print("Sum of series=",sum)
OUTPUT:
Sum of series= 85
f=open("Vowel.txt","r")
data=f.read()
V=['A','E','I','O','U','a','e','i','o','u']
C=['B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','W','X', \
'Y','Z','b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w', \
'x','y','z']
cv=0
cc=0
for i in data:
if i in V:
cv=cv+1
elif i in C:
cc=cc+1
ct=0
for i in data:
ct=ct+1
print("Number of Vowels in the file=",cv)
print("Number of Consonants in the file=",cc)
print("Number of Total Chars in the file=",ct)
f.close()
OUTPUT:
lc=0
while line:
if line[0]=='s' or line[0]=='S':
print(line,end="")
lc=lc+1
line=f.readline()
f.close()
OUTPUT:
Sam
SameerSanjay
Sunil
f=open("Vowel.txt","r")
data=f.read()
U=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R', \
'S','T','U','V','W','X','Y','Z',]
L=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r', \
's','t','u','v','w','x','y','z']
cu=0
cl=0
for i in data:
if i in U:
cu=cu+1
elif i in L:
cl=cl+1
ct=0
for i in data:
ct=ct+1
print("Number of Uppercase letters in the file=",cu)
print("Number of Lowercase Letters in the file=",cl)
print("Number of Total Chars in the file=",ct)
f.close()
OUTPUT:
S={}
f=open('stud.dat','wb')
c='y'
S['RollNo']=rno
S['Name']=name
pickle.dump(S,f)
f.close()
f=open('stud.dat','rb')
K={}
m=0
try:
while True:
K=pickle.load(f)
if K["RollNo"] == rno:
print(K)
m=m+1
except EOFError:
f.close()
if m==0:
OUTPUT:
Enter the roll no. of the student : 1
import pickle
S={}
f=open('stud.dat','wb')
c='y'
while c=='y' or c=='Y':
rno=int(input("Enter the roll no. of the student : "))
S['RollNo']=rno
S['Name']=name
S['Marks']=marks
pickle.dump(S,f)
f.close()
f=open('stud.dat','rb+')
f.seek(0,0)
m=0
try:
while True:
pos=f.tell()
S=pickle.load(f)
if S["RollNo"] == rno:
f.seek(pos)
S["Marks"]=marks
pickle.dump(S,f)
m=m+1
except EOFError:
f.close()
if m==0:
else:
f=open('stud.dat','rb')
try:
while True:
S=pickle.load(f)
print(S)
except EOFError:
f.close()
OUTPUT:
E={}
f=open('emp.dat','wb')
c='y'
E['Emp_Id']=eid
E['Emp_Name']=ename
E['Salary']=salary
pickle.dump(E,f)
f.close()
f=open('emp.dat','rb+')
f.seek(0,0)
m=0
try:
while True:
pos=f.tell()
E=pickle.load(f)
if E["Emp_Id"] == eid:
f.seek(pos)
E["Salary"]=salary
pickle.dump(E,f)
m=m+1
except EOFError:
f.close()
if m==0:
else:
f=open('emp.dat','rb')
try:
while True:
E=pickle.load(f)
print(E)
except EOFError:
15 Suraj Kumar GSBV(BURARI)
CS PRACTICLE FILE SESSION 2024-25 CLASS 12
OUTPUT:
Practical No - 10: Create a text file and remove the lines from
the file which contains letter ‘K’
import sys
f=open("sps.txt","w+")
data = sys.stdin.readlines()
for i in data:
f.write(i)
f.close()
print("**********")
f=open("sps.txt","r")
data=f.read()
print(data)
f.close()
f=open("sps.txt","r+")
data=f.readlines()
f.seek(0)
for i in data:
if "K" not in i:
f.write(i)
f.truncate()
f.close()
print("**********")
f=open("sps.txt","r")
data=f.read()
print(data)
OUTPUT:
SUN
HELLO INDIA
KARAN
SAM
RAM
KASHMIR
DELHI
**********
Content of File:
SUN
HELLO INDIA
KARAN
SAM
RAM
KASHMIR
DELH
**********
SUN
HELLO INDIA
SAM
RAM
DELHI
import pickle,random
N=[]
f=open("sps.txt","wb")
for i in range(10):
N.append(random.randint(1,40))
pickle.dump(N,f)
f.close()
print("File Created:")
print("Content of File:")
f=open("sps.txt","rb")
data=pickle.load(f)
for i in data:
print(i)
f.close()
OUTPUT:
File Created:
Content of File:
24
14
18
14
26
33
10
33
import csv
f=open("student.csv","w",newline='')
cw=csv.writer(f)
cw.writerow(['Rollno','Name','Marks'])
for i in range(5):
sr=[rollno,name,marks]
cw.writerow(sr)
f.close()
OUTPUT:
Student Record of 1
Enter Marks: 34
Student Record of 2
Enter Marks: 37
Student Record of 3
Enter Marks: 24
Student Record of 4
Enter Marks: 40
Student Record of 5
Enter Marks: 37
import csv
f=open("student.csv","r")
cr=csv.reader(f)
for r in cr:
print(r)
f.close()
OUTPUT:
Employee=[]
c='y'
while(c=="y" or c=="Y"):
if(choice==1):
emp=(eid,ename,salary)
Employee.append(emp)
elif(choice==2):
if(Employee==[]):
print("Stack Empty")
else:
elif(choice==3):
L=len(Employee)
while(L>0):
print(Employee[L-1])
L=L-1
else:
print("Wrong Input")
OUTPUT:
Book=[]
c='y'
while(c=='y' or c=='Y'):
if(choice==1):
B=(book_id,book_name,price)
Book.append(B)
elif(choice==2):
if(Book==[]):
print("Stack Empty")
else:
elif(choice==3):
L=len(Book)
while(L>0):
print(Book [L-1])
L=L-1
else:
print("Wrong Input")
OUTPUT:
Student=[]
c='y'
while(c=='y' or c=='Y'):
if(choice==1):
Student.append(stu)
elif(choice==2):
if(Student==[]):
print("Stack Empty")
else:
elif(choice==3):
L=len(Student)
while(L>0):
print(Student[L-1])
L=L-1
else:
print("wrong input")
OUTPUT:
movie=[]
c='y'
while(c=='y' or c=='Y'):
if(Choice==1):
mov= (mid,mname,rating)
movie.append(mov)
elif(Choice==2):
if(movie==[]):
print("stack empty")
else:
elif(Choice==3):
L=len(movie)
while(L>0):
print(movie[L-1])
L=L-1
else:
print("wrong input")
OUTPUT:
Product=[ ]
c="y"
while(c=="y" or c=="Y"):
if(choice==1):
Prd=(pid,pname,price)
Product.append(Prd)
elif(choice==2):
if(Product==[ ]):
print("Stack Empty")
else:
elif(choice==3):
L=len(Product)
while(L>0):
print(Product[L-1])
L=L-1
else:
print("Wrong input")
OUTPUT:
if(Club == []):
print(" Stack Empty ")
else:
print("Delete Element is : ", Club.pop())
elif (choice == 3):
L = len(Club)
while (L>0):
print(Club[L-1])
L=L-1
else:
print("Wrong Input")
c= input("Do you want to continue? Press 'y' to continue:")
OUTPUT:
Enter choice: 1
Enter Club Id: 103
Enter Club Name: A3
Enter Club City: Delhi
Do you want to continue? Press 'y' to continue:y
1: Add Club Details:
2: Delete Club Details:
3: Display Club Details:
Enter choice: 3
(103, 'A3', 'Delhi')
(102, 'A2', 'Noida')
(101, 'A1', 'Delhi')
Do you want to continue? Press 'y' to continue:y
1: Add Club Details:
2: Delete Club Details:
3: Display Club Details:
Enter choice: 2
Delete Element is : (103, 'A3', 'Delhi')
Do you want to continue? Press 'y' to continue:y
1: Add Club Details:
2: Delete Club Details:
3: Display Club Details:
Enter choice: 3
(102, 'A2', 'Noida')
(101, 'A1', 'Delhi')
Do you want to continue? Press 'y' to continue:n
import mysql.connector
con=mysql.connector.connect(host="localhost",username="root",passwd="root")
mycursor=con.cursor()
mycursor.execute("create database if not exists spg")
mycursor.execute("use spg")
mycursor.execute("create table if not exists book (bid int primary key,bname varchar(20),bprice
float(5,2))")
c="y"
while(c=="y" or c=="Y"):
print("1. Press 1 for add new book: ")
print("2. Press 2 for Show the details of Books: ")
print("3. Press 3 for Update Book Details: ")
print("4. Press 4 for Delete Book Details: ")
print("5. Press 5 for Exit: ")
choice=int(input("Enter Your Choice 1 or 2 or 3 or 4 or 5: "))
if(choice==1):
bid=int(input("Enter Book Id: "))
bname=input("Enter Book Name: ")
bprice=float(input("Enter Book Price: "))
mycursor.execute("insert into book values(%s,%s,%s)",(bid,bname,bprice))
con.commit()
elif(choice==2):
mycursor.execute("select * from book")
mybooks=mycursor.fetchall()
for x in mybooks:
print(x)
elif(choice==3):
bid=int(input("Enter the book id for update: "))
bname=input("Enter Book New Name: ")
bprice=float(input("Enter Book New Price: "))
mycursor.execute("update book set bname=%s,bprice=%s where
bid=%s",(bname,bprice,bid))
con.commit()
elif(choice==4):
bid=int(input("Enter the book id for delete: "))
mycursor.execute("delete from book where bid=%s",(bid,))
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
c=input("Press 'y' for continue and 'n' for exit: ")
OUTPUT:
c="y"
while(c=="y" or c=="Y"):
print("1. Press 1 for add new product: ")
con.commit()
elif(choice==4):
pid=int(input("Enter the product id for delete: "))
mycursor.execute("delete from product where pid=%s",(pid,))
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
c=input("Press 'y' for continue and 'n' for exit: ")
41 Suraj Kumar GSBV(BURARI)
CS PRACTICLE FILE SESSION 2024-25 CLASS 12
OUTPUT:
import mysql.connector
con=mysql.connector.connect(host="localhost",username="root",passwd="root")
mycursor=con.cursor()
mycursor.execute("create database if not exists spg")
mycursor.execute("use spg")
mycursor.execute("create table if not exists club (cid int primary key, cname
varchar(20),city varchar(20))")
c="y"
while(c=="y" or c=="Y"):
print("1. Press 1 for add new club: ")
print("2. Press 2 for Show the details of club: ")
print("3. Press 3 for Update club Details: ")
print("4. Press 4 for Delete club Details: ")
print("5. Press 5 for Exit: ")
choice=int(input("Enter Your Choice 1 or 2 or 3 or 4 or 5: "))
if(choice==1):
cid=int(input("Enter club Id: "))
cname=input("Enter club Name: ")
city=input("Enter club city: ")
mycursor.execute("insert into club values(%s,%s,%s)",(cid,cname,city))
con.commit()
elif(choice==2):
con.commit()
elif(choice==4):
cid=int(input("Enter the club id for delete: "))
mycursor.execute("delete from club where cid=%s",(cid,))
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
c=input("Press 'y' for continue and 'n' for exit: ")
OUTPUT:
import mysql.connector
con=mysql.connector.connect(host="localhost",username="root",passwd="root")
mycursor=con.cursor()
mycursor.execute("create database if not exists spg")
mycursor.execute("use spg")
mycursor.execute("create table if not exists Student (sid int primary key, sname varchar(20),
course varchar(20))")
c="y"
while(c=="y" or c=="Y"):
print("1. Press 1 for add new Student: ")
print("2. Press 2 for Show the details of Students: ")
print("3. Press 3 for Update Student Details: ")
print("4. Press 4 for Delete Student Details: ")
print("5. Press 5 for Exit: ")
choice=int(input("Enter Your Choice 1 or 2 or 3 or 4 or 5: "))
if(choice==1):
sid=int(input("Enter Student Id: "))
sname=input("Enter Student Name: ")
course=input("Enter Student Course: ")
mycursor.execute("insert into Student values(%s,%s,%s)",(sid,sname,course))
con.commit()
elif(choice==2):
mycursor.execute("select * from Student")
mystudents=mycursor.fetchall()
for x in mystudents:
print(x)
elif(choice==3):
con.commit()
elif(choice==4):
cid=int(input("Enter the Student id for delete: "))
mycursor.execute("delete from Student where sid=%s",(sid,))
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
c=input("Press 'y' for continue and 'n' for exit: ")
OUTPUT:
import mysql.connector
con=mysql.connector.connect(host="localhost",username="root",passwd="root")
mycursor=con.cursor()
mycursor.execute("create database if not exists spg")
mycursor.execute("use spg")
c="y"
while(c=="y" or c=="Y"):
print("1. Press 1 for add new movie: ")
print("2. Press 2 for Show the details of movie: ")
print("3. Press 3 for Update movie Details: ")
print("4. Press 4 for Delete movie Details: ")
print("5. Press 5 for Exit: ")
choice=int(input("Enter Your Choice 1 or 2 or 3 or 4 or 5: "))
if(choice==1):
mid=int(input("Enter movie Id: "))
mname=input("Enter movie Name: ")
rating=float(input("Enter movie rating: "))
mycursor.execute("insert into movie values(%s,%s,%s)",(mid,mname,rating))
con.commit()
elif(choice==2):
mycursor.execute("select * from movie")
mymovies=mycursor.fetchall()
for x in mymovies:
print(x)
elif(choice==3):
con.commit()
elif(choice==4):
mid=int(input("Enter the movie id for delete: "))
mycursor.execute("delete from movie where mid=%s",(mid,):
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
c=input("Press 'y' for continue and 'n' for exit: ")
OUTPUT:
import mysql.connector
con=mysql.connector.connect(host="localhost",username="root",passwd="root")
mycursor=con.cursor()
mycursor.execute("create database if not exists spg")
mycursor.execute("use spg")
mycursor.execute("create table if not exists Employee (eid int primary key, ename varchar(20),
salary float(8,2))")
c="y"
while(c=="y" or c=="Y"):
con.commit()
elif(choice==4):
eid=int(input("Enter the Employee id for delete: "))
mycursor.execute("delete from Employee where eid=%s",(eid,))
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
c=input("Press 'y' for continue and 'n' for exit: ")
OUTPUT:
MYSQL QUERIES
Q-2 - To create a student table with the student id, class, section, gender,name,
dob, and marks as attributes where the student id is the primary key.
Ans - Primary Key:
Primary key is a constraint of an attribute which cannot be NULL or duplicate.
Only one primary key is allowed in a table.
To create a new table we use create table query.
Syntax:
Create table table_name(col_name1 type primary key, col_name2 type, ……..);
Q- 5 - To increase the marks by 5% for those students who are scoring marks
more than 30.
Update:
Update is a SQL query used to update/change/modify the record of a table.
Syntax:
update tablename set col_name=value, col_name=value,... where condition;
Q-7 - To display student_id, name and marks of those students who are scoring
marks more than 30.
Q-9 - To find the number of students, who are from section ‘A’.
Q-10 - To add a new column email in the student table with appropriate data
type.
Q-11 - To add the email id’s of each student in the previously created email
column.
Q-12 - To display the information of all the students, whose name starts with
‘S’
Q-13 - To display the student_id, name, dob of those students who are born
between ‘2005-01-01’ and ‘2005-12-31’.
Q-15 - To display the student_id, gender, name, dob, marks, email of students
in descending order of their marks.
Q-16 - To display the unique section name from the student table.
Q – 17: Create a student table with student id, name and marks as attribute,
where the student id is the primary key.
Q –20: Use the Select command to get the details of the students with marks
more than 30.
Q-21 : Shiva, a student of class XII, created a table “CLASS”. Grade is one of the
columns of this table. Write the SQL query to find the details of students whose
grade have not been entered.
Q-23 : Write the difference between drop, delete and truncate command with
example.
Drop
Drop is a DDL (Data Definition Language) command of SQL.
Drop is used to remove an object completely just like remove database, table, view, index etc.
Syntax:
DROP object object_name
Examples:
DROP TABLE table_name;
table_name: Name of the table to be deleted.
DROP DATABASE database_name;
database_name: Name of the database to be deleted.
Delete
Delete is a DML (Data Manipulation Language) command of SQL.
Delete is used to remove one or more rows from a table, view, index etc. We can use where clause with delete
command. Delete remove one row at a time i.e. it remove rows one by one. It will not delete columns or structure of
table.
Syntax:
delete from table_name where condition;
Examples:
delete from table_name;
table_name: Name of the table from which rows will be deleted.
Delete all rows from the table.
71 Suraj Kumar GSBV(BURARI)
CS PRACTICLE FILE SESSION 2024-25 CLASS 12
Truncate
Truncate is a DDL (Data Defination Language) command of SQL.
Truncate is used to remove all rows from a table, view, index etc. We can not use where clause with truncate
command. Truncate remove all rows at once, but it will not delete structure of the table or any object.
Syntax:
delete from table_name where condition;
Examples:
trucate table_name;
table_name: Name of the table from which rows will be deleted.
Delete all rows from the table at once.
Q-24 : Write the sort notes on group by, having and where clause with example.
WHERE Clause
WHERE Clause is used to filter the records/rows from the table. It can be used with SELECT, UPDATE,
DELETE statements.
Select * from employees where salary>25000;
Group by Clause
The GROUP BY clause is used with aggregate functions (MAX, SUM, AVG, COUNT, MIN) to group the
results by one or more columns i.e. The GROUP BY clause is used with the SELECT statement to arrange
required data into groups.
The GROUP BY statement groups rows that have the same values. This Statement is used after the where
clause. This statement is often used with some aggregate function like SUM, AVG, and COUNT etc. to
group the results by one or more columns.
Having Clause
Having Clause is like the aggregate function with the GROUP BY clause. The HAVING clause is used
instead of WHERE with aggregate functions. While the GROUP BY Clause group rows that have the
same values into rows. The having clause is used with the where clause in order to find rows with certain
conditions. The having clause is always used after the group by clause.
WHERE Clause is used to filter the HAVING Clause is used to filter The group by clause is used to
records from the table based on record from the groups based on group the data according to
the specified condition. the specified condition. particular column or row.
WHERE Clause can be used HAVING Clause cannot be used Group by can be used without
without GROUP BY Clause without GROUP BY Clause having clause with the select
statement.
WHERE Clause implements in row HAVING Clause implements in It groups the output on basis of
operations column operation some rows or columns.
WHERE Clause cannot contain The having clause can contain It cannot contain aggregate
aggregate function aggregate functions. functions.
WHERE Clause can be used with HAVING Clause can only be used The GROUP BY clause is used in
SELECT, UPDATE, DELETE with SELECT statement. the SELECT statement.
statement.
WHERE Clause is used before HAVING Clause is used after Group By is used after the Where
GROUP BY Clause GROUP BY Clause clause and before the HAVING
Clause
Q-25 : Write the SQL query to find out the square root of 26.
Q-26 : Shiva is using a table employee. It has following details: Employee (Code,
Name, Salary, DeptCode). Write the SQL query to display maximum salary
department wise.
Q-27 : Write the SQL Query to display the difference of highest and lowest salary
of each department having maximum salary greater than 4000.
Q-28 : Write the SQL Query to increase 20% salary ofthe employee whose
experience is more than 5 year of the table Emp(id, name, salary, exp)
Q-29 : Write the SQL Query for inner join of two tables Emp(eid, ename, salary,
dept_id) and Dept(dept_id, dname)
Q-30 : Write the SQL Query for full outer join of two tables Emp(eid,
ename,salary,dept_id) and Dept(dept_id, dname)
Q-31 : Write a SQL to Enter 5 Employee data in a single query in the table Emp
(eid, ename, salary, city, dob).
Q-32 : Display 4 characters extracted from 5th right character onwards from string
‘ABCDEFG’.
Q-33 : Write a query to create a string from the ASCII values 65, 67.3, ‘68.3’.
Q-34 : Display names ‘MR. MODI’ and ‘MR. Sharma’ into lowercase.